-
Notifications
You must be signed in to change notification settings - Fork 221
Evelyn Weather API #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Evelyn Weather API #208
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>Weather Project</title> | ||
| <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" /> | ||
| <link href="./style.css" rel="stylesheet" /> | ||
| </head> | ||
| <body> | ||
| <div class="container"> | ||
| <div class="row"> | ||
| <div class="col-md-8 col-md-offset-2"> | ||
| <div class="page-header text-center"> | ||
| <h1>Weather Project</h1> | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div class="row"> | ||
| <div class="col-md-6 col-md-offset-3"> | ||
| <form class="form-inline" onsubmit="event.preventDefault();"> | ||
| <div class="form-group submit-city"> | ||
| <label class="sr-only" for="location">Enter City Name Here</label> | ||
| <input class="form-control" id="location" type="text" placeholder="Enter City Name Here" /> | ||
| </div> | ||
| <div class="form-group submit-btn"> | ||
| <button id="submit-search" class="btn btn-primary">Search</button> | ||
| </div> | ||
| </form> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div class="row"> | ||
| <div class="col-md-8 col-md-offset-2"> | ||
| <div class="container main-weather text-center"> | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div class="row"> | ||
| <div class="col-md-8 col-md-offset-2 no-padding-left"> | ||
| <div class="container sub-weather text-center no-padding-left"> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| <script src="main.js"></script> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,110 @@ | ||||||||
| const daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; | ||||||||
| const currentDate = new Date(); | ||||||||
|
|
||||||||
| document.getElementById('submit-search').addEventListener('click', function () { | ||||||||
| cityName = document.getElementById('location').value; | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Watch for definition if its const or let |
||||||||
| fetchFromCityLocation(cityName); | ||||||||
| }); | ||||||||
|
|
||||||||
| const fetchWeatherData = lat, lon, city => { | ||||||||
| const fetchUrl = `https://api.openweathermap.org/data/2.5/forecast?lat=${lat}&lon=${lon}&appid=ca90637f0ed0bcfecc469a57ea89c230`; | ||||||||
|
|
||||||||
| fetch(fetchUrl, { | ||||||||
| method: 'GET', | ||||||||
| dataType: 'json' | ||||||||
| }).then(data => data.json()).then(data => readWeatherData(data, city)); | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you know how to use arrow function :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Data is too generic, consider using something specific like weatherData
Suggested change
|
||||||||
| } | ||||||||
|
|
||||||||
| const fetchFromCityLocation = city => { | ||||||||
| const geoUrl = `https://api.openweathermap.org/geo/1.0/direct?q=${city}&limit=1&appid=ca90637f0ed0bcfecc469a57ea89c230`; | ||||||||
|
|
||||||||
| fetch(geoUrl, { | ||||||||
| method: 'GET', | ||||||||
| dataType: 'json' | ||||||||
| }).then(data => data.json()).then(data => fetchWeatherData(data[0].lat, data[0].lon, city)); | ||||||||
| } | ||||||||
|
|
||||||||
| const readWeatherData = city => { | ||||||||
| const weatherData = []; | ||||||||
|
|
||||||||
| for(let i = 0; i < data.list.length; i += 8) { | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a great opportunity to use an array helper method like forEach or map |
||||||||
| const currentWeatherData = data.list[i]; | ||||||||
|
|
||||||||
| const weather = { | ||||||||
| temp: ((currentWeatherData.main.temp - 273.15) * (9/5) + 32), | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for readability, the calculation should have been in a function |
||||||||
| main: currentWeatherData.weather[0].main | ||||||||
| }; | ||||||||
|
|
||||||||
| weatherData.push(weather); | ||||||||
| } | ||||||||
| renderWeather(weatherData, city); | ||||||||
| } | ||||||||
|
|
||||||||
| const renderWeather = weatherData, city => { | ||||||||
| if(document.querySelector('.main-weather').hasChildNodes() && document.querySelector('.sub-weather').hasChildNodes()) { | ||||||||
| document.querySelector('.main-weather').replaceChildren(); | ||||||||
| document.querySelector('.sub-weather').replaceChildren(); | ||||||||
| } | ||||||||
|
Comment on lines
+44
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for readability, move to a function clearCurrentWeatherDisplay() or something like this |
||||||||
|
|
||||||||
|
|
||||||||
| for(let i = 0; i < weatherData.length; i++) { | ||||||||
| const mainWeatherTemplate = ` | ||||||||
| <div class="row"> | ||||||||
| <div class="col-md-2 col-md-offset-2"> | ||||||||
| <ul class="list-unstyled"> | ||||||||
| <li class="text-center temperature">${Math.floor(weatherData[i].temp)}°</li> | ||||||||
| <li class="text-center location">${city}</li> | ||||||||
| <li class="text-center description">${weatherData[i].main}</li> | ||||||||
| </ul> | ||||||||
| </div> | ||||||||
| <div class="col-md-2"> | ||||||||
| <image src="https://openweathermap.org/img/wn/${returnWeatherIcon(weatherData[i].main)}d@2x.png" alt="${weatherData[i].main} Icon" /> | ||||||||
| </div> | ||||||||
| </div>`; | ||||||||
|
|
||||||||
| const subWeatherTemplate = ` | ||||||||
| <div class="col-md-2"> | ||||||||
| <div class="sub-weather-item"> | ||||||||
| <ul class="list-unstyled"> | ||||||||
| <li class="text-center temperature">${Math.floor(weatherData[i].temp)}°</li> | ||||||||
| <li class="text-center description">${weatherData[i].main}</li> | ||||||||
| </ul> | ||||||||
| <image src="https://openweathermap.org/img/wn/${returnWeatherIcon(weatherData[i].main)}d@2x.png" alt="${weatherData[i].main} Icon" /> | ||||||||
| <p>${returnDayOfWeek(i)}</p> | ||||||||
| </div> | ||||||||
| </div>`; | ||||||||
|
|
||||||||
| if (i != 0) { | ||||||||
| document.querySelector('.sub-weather').insertAdjacentHTML('beforeend', subWeatherTemplate); | ||||||||
| } else { | ||||||||
| document.querySelector('.main-weather').insertAdjacentHTML('beforeend', mainWeatherTemplate); | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| const returnWeatherIcon = description => { | ||||||||
| switch(description) { | ||||||||
| case 'Thunderstorm': | ||||||||
| return '11'; | ||||||||
| case 'Drizzle': | ||||||||
| return '09'; | ||||||||
| case 'Rain': | ||||||||
| return '10'; | ||||||||
| case 'Snow': | ||||||||
| return '13'; | ||||||||
| case 'Atmosphere': | ||||||||
| return '50'; | ||||||||
| case 'Clear': | ||||||||
| return '01'; | ||||||||
| case 'Clouds': | ||||||||
| return '04'; | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| const returnDayOfWeek = offset => { | ||||||||
| let currentDay = currentDate.getDay() + offset; | ||||||||
| if (currentDay > 6) { | ||||||||
| currentDay -= 7; | ||||||||
| } | ||||||||
| return daysOfWeek[currentDay]; | ||||||||
| } | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| .form-control { | ||
| margin: 10px 10px; | ||
| } | ||
|
|
||
| .no-padding-left { | ||
| padding-left: 0px; | ||
| } | ||
|
|
||
| .sub-weather-item { | ||
| border-style: solid; | ||
| border-color: black; | ||
| border-width: 2px; | ||
| } | ||
|
|
||
| #location { | ||
| width: 400px; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
arrow function next time =)