-
Notifications
You must be signed in to change notification settings - Fork 221
Weather App #224
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
Open
j-emitch
wants to merge
4
commits into
projectshft:master
Choose a base branch
from
j-emitch:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Weather App #224
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <!-- Required meta tags --> | ||
| <link rel="stylesheet" href="style.css" /> | ||
| <meta charset="utf-8" /> | ||
| <meta | ||
| name="viewport" | ||
| content="width=device-width, initial-scale=1, shrink-to-fit=no" | ||
| /> | ||
|
|
||
| <!-- Bootstrap CSS --> | ||
| <link | ||
| rel="stylesheet" | ||
| href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" | ||
| integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" | ||
| crossorigin="anonymous" | ||
| /> | ||
| </head> | ||
| <body> | ||
| <div class="container-fluid"> | ||
| <div class="row"> | ||
| <div class="col-md-6 offset-md-3"> | ||
| <div class="page-header"> | ||
| <h1>The Weather App</h1> | ||
| <hr /> | ||
| </div> | ||
|
|
||
| <form class="search-form"> | ||
| <h3>Search City</h3> | ||
|
|
||
| <div class="form-group"> | ||
| <input | ||
| type="text" | ||
| id="search-query" | ||
| class="form-control" | ||
| placeholder="Enter City Name Here" | ||
| /> | ||
| </div> | ||
|
|
||
| <button type="button" class="btn btn-primary search">Search</button> | ||
| </form> | ||
|
|
||
| <hr /> | ||
| </div> | ||
| </div> | ||
| <div class="current-weather row justify-content-center mt-4"></div> | ||
| <div class="five-day row justify-content-center text-center mt-4"></div> | ||
| </div> | ||
|
|
||
| <script src="main.js" type="text/javascript"></script> | ||
| </body> | ||
| </html> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| const apiKey = ''; | ||
| let curWeather = {}; | ||
| let fiveDayForecast = []; | ||
|
|
||
| document.querySelector('.search').addEventListener('click', function () { | ||
| const city = document.querySelector('#search-query').value; | ||
|
|
||
| fetchCurWeather(city); | ||
| fetchFiveDay(city); | ||
|
|
||
| document.querySelector('#search-query').value = ''; | ||
| }); | ||
|
|
||
| const addCurWeather = (data) => { | ||
| curWeather = { | ||
| city: data.name || null, | ||
| description: data.weather[0].main || null, | ||
| icon: data.weather[0].icon || null, | ||
| temp: Math.round(data.main.temp) || null, | ||
| }; | ||
|
|
||
| renderCurWeather(); | ||
| }; | ||
|
|
||
| const addFiveDay = (data) => { | ||
| fiveDayForecast = []; | ||
|
|
||
| for (i=7; 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. you could have use map instead |
||
| const curDay = { | ||
| description: data.list[i].weather[0].main || null, | ||
| icon: data.list[i].weather[0].icon || null, | ||
| temp: Math.round(data.list[i].main.temp) || null, | ||
| day: new Date(data.list[i].dt_txt).toLocaleString('en',{ | ||
| weekday: 'long'}) | ||
| }; | ||
| fiveDayForecast.push(curDay); | ||
| } | ||
|
|
||
| renderFiveDay(); | ||
| }; | ||
|
|
||
| const fetchCurWeather = (city) => { | ||
| const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=imperial`; | ||
| fetch(url, { | ||
| method: 'GET', | ||
| dataType: 'json' | ||
| }) | ||
| .then(data => data.json()) | ||
| .then(data => addCurWeather(data)); | ||
| } | ||
|
|
||
| const fetchFiveDay = (city) => { | ||
| const url = `https://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=${apiKey}&units=imperial`; | ||
| fetch(url, { | ||
| method: 'GET', | ||
| dataType: 'json' | ||
| }) | ||
| .then(data => data.json()) | ||
| .then(data => addFiveDay(data)); | ||
| } | ||
|
|
||
| const renderCurWeather = () => { | ||
| document.querySelector('.current-weather').replaceChildren(); | ||
|
|
||
| const curWeatherTemplate = | ||
| ` | ||
| <div class = "col-md-4 d-flex justify-content-between align-items-center"> | ||
| <div class = "d-flex flex-column text-center justify-content-center"> | ||
| <h2>${curWeather.temp}\u00B0F</h2> | ||
| <h3>${curWeather.city}</h3> | ||
| <p>${curWeather.description}</p> | ||
| </div> | ||
| <div> | ||
| <img src="https://openweathermap.org/img/wn/${curWeather.icon}@2x.png" alt="Weather Icon"> | ||
| </div> | ||
| </div> | ||
| `; | ||
|
|
||
| document.querySelector('.current-weather').insertAdjacentHTML('beforeend', curWeatherTemplate); | ||
| }; | ||
|
|
||
| const renderFiveDay = () => { | ||
| document.querySelector('.five-day').replaceChildren(); | ||
|
|
||
| const fiveDayTemplate = fiveDayForecast.map(day => { | ||
| return ` | ||
| <div class="col-md-2 col-sm-4"> | ||
| <div class="card p-2 shadow-sm"> | ||
| <div class="card-body"> | ||
| <p class="card-text">${day.description}</p> | ||
| <h3 class="card-title">${day.temp}\u00B0F</h3> | ||
| <img src="https://openweathermap.org/img/wn/${day.icon}@2x.png"" alt="Icon"> | ||
| <p class="card-text fw-bold">${day.day}</p> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| `; | ||
| }).join(''); | ||
|
|
||
| document.querySelector('.five-day').insertAdjacentHTML('beforeend', fiveDayTemplate); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| .page-header { | ||
| margin-top: 40px; | ||
| width: 100%; | ||
| text-align: center; | ||
| } | ||
|
|
||
| .search-form { | ||
| margin-top: 40px; | ||
| margin-bottom: 40px; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
outdated version, there are stable versions