-
Notifications
You must be signed in to change notification settings - Fork 221
weather app project successful #230
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
pwarrengrovesjr
wants to merge
8
commits into
projectshft:master
Choose a base branch
from
pwarrengrovesjr: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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
546965a
init commit
pwarrengrovesjr 5fcb2b9
header and search bar done, working on fetching city data
pwarrengrovesjr 76adb58
configuring city search bar
pwarrengrovesjr e06344b
figured out the city-searching mechanism
pwarrengrovesjr 6c4b8a6
replaced collapsible city-options element with datalist of city options
pwarrengrovesjr b57c473
city search functionality complete; retrieval of current weather data…
pwarrengrovesjr 879d6be
working on template for current-weather div
pwarrengrovesjr 0125cb6
project finished
pwarrengrovesjr 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,44 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| <title>Weather Project</title> | ||
| <link | ||
| href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" | ||
| rel="stylesheet" | ||
| integrity="sha384-LN+7fdVzj6u52u30Kp6M/trliBMCMKTyK833zpbD+pXdCLuTusPj697FH4R/5mcr" | ||
| crossorigin="anonymous" | ||
| /> | ||
| </head> | ||
| <body> | ||
| <div class="container"> | ||
| <div class="h1 text-center p-4 border-bottom">Weather Project</div> | ||
| <div class="row pt-5" id="search-bar"> | ||
| <div class="col-9 city-search"> | ||
| <input | ||
| type="search" | ||
| class="form-control search-query" | ||
| placeholder="Enter City Name" | ||
| id="search-query" | ||
| /> | ||
| </div> | ||
| <div class="d-grid col-2"> | ||
| <button type="button" class="btn btn-primary search-btn"> | ||
| Search | ||
| </button> | ||
| </div> | ||
| </div> | ||
| <div class="cities m-2 list-group" id="cities" style="width: 50%"></div> | ||
| <div class="current-weather row mt-5"></div> | ||
| <div class="forecast row my-3"></div> | ||
| </div> | ||
| <script | ||
| src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/js/bootstrap.bundle.min.js" | ||
| integrity="sha384-ndDqU0Gzau9qJ1lfW4pNLlhNTkCfHzAVBReH9diLvGRem5+R9g2FzA8ZGN954O5Q" | ||
| crossorigin="anonymous" | ||
| ></script> | ||
| <script src="main.js"></script> | ||
| <script src="https://unpkg.com/@popperjs/core@2"></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,175 @@ | ||
| var cities = []; | ||
| var currentWeather = []; | ||
| var forecast = []; | ||
| var dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; | ||
|
|
||
| const fetchCities = function(city) { | ||
| const url = 'http://api.openweathermap.org/geo/1.0/direct?q=' + city + '&limit=5&appid=ad7a6c47620e290c667cee3d6af14631' | ||
|
|
||
| fetch(url, { | ||
| method: 'GET', | ||
| datatype: 'json', | ||
| }) | ||
| .then(data => data.json()) | ||
| .then(data => addCities(data)) | ||
| }; | ||
|
|
||
| const addCities = (data) => { | ||
| cities = []; | ||
|
|
||
| for (let i = 0; i < data.length; i++) { | ||
| const cityData = data[i]; | ||
|
|
||
| const city = { | ||
| name: cityData.name || null, | ||
| state: cityData.state || null, | ||
| country: cityData.country || null, | ||
| lon: cityData.lon || null, | ||
| lat: cityData.lat || null | ||
| }; | ||
|
|
||
| cities.push(city); | ||
| } | ||
|
|
||
| renderCities(); | ||
| }; | ||
|
|
||
| const renderCities = () => { | ||
| document.querySelector('.cities').replaceChildren(); | ||
|
|
||
| for (let i = 0; i < cities.length; i++) { | ||
| const city = cities[i]; | ||
|
|
||
| let template = `<button type="button" class="city${i} list-group-item list-group-item-action">${city.name}, ${city.state}, ${city.country}</button>`; | ||
|
|
||
| if (city.state === null) { | ||
| template = `<button type="button" class="city${i} list-group-item list-group-item-action">${city.name}, ${city.country}</button>`; | ||
| } | ||
|
|
||
| document.querySelector('.cities').insertAdjacentHTML('beforeend', template); | ||
|
|
||
| document.querySelector(`.city${i}`).addEventListener('click', function(){ | ||
| document.querySelector('.cities').replaceChildren() | ||
|
|
||
| fetchCurrentWeather(city) | ||
| fetchForecast(city) | ||
| }) | ||
| } | ||
| }; | ||
|
|
||
| document.querySelector('.search-btn').addEventListener('click', function () { | ||
| const search = document.querySelector('.search-query').value; | ||
|
|
||
| fetchCities(search); | ||
|
|
||
| document.querySelector('.search-query').value = ''; | ||
| }); | ||
|
|
||
| const fetchCurrentWeather = function(city) { | ||
| const url = 'https://api.openweathermap.org/data/2.5/weather?lat=' + city.lat + '&lon=' + city.lon + '&appid=ad7a6c47620e290c667cee3d6af14631'; | ||
|
|
||
| fetch(url, { | ||
| method: 'GET', | ||
| datatype: 'json', | ||
| }) | ||
| .then(data => data.json()) | ||
| .then(data => addCurrentWeather(data)) | ||
| } | ||
|
|
||
| const addCurrentWeather = function(data) { | ||
| currentWeather = []; | ||
|
|
||
| const weather = { | ||
| place: data.name || null, | ||
| temp: Math.ceil(1.8 * (data.main.temp - 273.15) + 32) || null, | ||
| conditions: data.weather[0].main || null, | ||
| icon: data.weather[0].icon || null | ||
| } | ||
|
|
||
| currentWeather.push(weather) | ||
|
|
||
| renderCurrentWeather(); | ||
| } | ||
|
|
||
| const renderCurrentWeather = function() { | ||
| document.querySelector('.current-weather').replaceChildren(); | ||
|
|
||
| let template = ` | ||
| <div class="col text-center"> | ||
| <div class="temp display-4 pb-1"><strong>${currentWeather[0].temp}°</strong></div> | ||
| <div class="temp display-6 pb-1"><strong>${currentWeather[0].place}</strong></div> | ||
| <div class="temp display-6 pb-1"><strong>${currentWeather[0].conditions}</strong></div> | ||
| </div> | ||
| <div class="col text-center"><img src="https://openweathermap.org/img/wn/${currentWeather[0].icon}@4x.png"></div>`; | ||
|
|
||
| document.querySelector('.current-weather').insertAdjacentHTML('beforeend', template); | ||
| } | ||
|
|
||
| const fetchForecast = function(city) { | ||
| const url = 'https://api.openweathermap.org/data/2.5/forecast?lat=' + city.lat + '&lon=' + city.lon + '&appid=ad7a6c47620e290c667cee3d6af14631'; | ||
|
|
||
| fetch(url, { | ||
| method: 'GET', | ||
| datatype: 'json', | ||
| }) | ||
| .then(data => data.json()) | ||
| .then(data => addForecast(data)) | ||
| } | ||
|
|
||
| const addForecast = function(data) { | ||
| forecast = []; | ||
|
|
||
| for (let i = 0; i < data.list.length; i++) { | ||
| const element = data.list[i]; | ||
|
|
||
| var weather = { | ||
| condition: element.weather[0].main || null, | ||
| temp: Math.ceil(1.8 * (element.main.temp - 273.15) + 32) || null, | ||
| icon: element.weather[0].icon || null, | ||
| day: dayNames[new Date(element.dt_txt).getDay()] || null | ||
| }; | ||
|
|
||
| forecast.push(weather); | ||
|
|
||
| } | ||
|
|
||
| renderForecast(); | ||
| } | ||
|
|
||
| const renderForecast = function() { | ||
| document.querySelector('.forecast').replaceChildren(); | ||
|
|
||
| let template = ` | ||
|
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 could have been a loop |
||
| <div class="col border border-black text-center"> | ||
| <div class="mt-2">${forecast[5].condition}</div> | ||
| <div>${forecast[5].temp}°</div> | ||
| <div class="my-2"><img src="https://openweathermap.org/img/wn/${forecast[5].icon}@2x.png"></div> | ||
| <div class="mb-2">${forecast[5].day}</div> | ||
| </div> | ||
| <div class="col border border-black text-center"> | ||
| <div class="mt-2">${forecast[5 + 8].condition}</div> | ||
| <div>${forecast[5 + 8].temp}°</div> | ||
| <div class="my-2"><img src="https://openweathermap.org/img/wn/${forecast[5 + 8].icon}@2x.png"></div> | ||
| <div class="mb-2">${forecast[5 + 8].day}</div> | ||
| </div> | ||
| <div class="col border border-black text-center"> | ||
| <div class="mt-2">${forecast[5 + 16].condition}</div> | ||
| <div>${forecast[5 + 16].temp}°</div> | ||
| <div class="my-2"><img src="https://openweathermap.org/img/wn/${forecast[5 + 16].icon}@2x.png"></div> | ||
| <div class="mb-2">${forecast[5 + 16].day}</div> | ||
| </div> | ||
| <div class="col border border-black text-center"> | ||
| <div class="mt-2">${forecast[5 + 24].condition}</div> | ||
| <div>${forecast[5 + 24].temp}°</div> | ||
| <div class="my-2"><img src="https://openweathermap.org/img/wn/${forecast[5 + 24].icon}@2x.png"></div> | ||
| <div class="mb-2">${forecast[5 + 24].day}</div> | ||
| </div> | ||
| <div class="col border border-black text-center"> | ||
| <div class="mt-2">${forecast[5 + 32].condition}</div> | ||
| <div>${forecast[5 + 32].temp}°</div> | ||
| <div class="my-2"><img src="https://openweathermap.org/img/wn/${forecast[5 + 32].icon}@2x.png"></div> | ||
| <div class="mb-2">${forecast[5 + 32].day}</div> | ||
| </div>`; | ||
|
|
||
| document.querySelector('.forecast').insertAdjacentHTML('beforeend', template); | ||
| } | ||
Empty file.
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.
export this to a function and label the fixed numbers with consts so its more readable.