-
Notifications
You must be signed in to change notification settings - Fork 221
Chris d'Arcy Eval 3 #222
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
cmdarcy
wants to merge
9
commits into
projectshft:master
Choose a base branch
from
cmdarcy: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
Chris d'Arcy Eval 3 #222
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3c750e6
initial commit
cmdarcy db66c5f
add form structure and submit logic
cmdarcy 1009f4a
add renderWeather and getWeatherData functionality, first pass getFor…
cmdarcy b2ae2ce
format forecastData
cmdarcy 4f495f9
add renderForecast func, format Date
cmdarcy c33dba9
style weather and forecast containers
cmdarcy 6ca9ca1
add error handling and form validation
cmdarcy 1e7ce48
add default city button
cmdarcy d4d2e44
add comments
cmdarcy 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,41 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>My Weather Project</title> | ||
|
|
||
| <link | ||
| href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" | ||
| rel="stylesheet" | ||
| /> | ||
|
|
||
| <link rel="stylesheet" href="style.css" /> | ||
| </head> | ||
| <body> | ||
| <div class="container text-center"> | ||
| <h1 class="mt-4">Weather Project</h1> | ||
| <section> | ||
| <form action=""> | ||
| <div class="form-group mb-2"> | ||
| <input | ||
| type="text" | ||
| class="form-control input-control" | ||
| placeholder="City Name" | ||
| /> | ||
| <div class="invalid-feedback">Please enter a valid city name!</div> | ||
| </div> | ||
| <button type="submit" class="btn btn-primary">Search</button> | ||
| </form> | ||
| </section> | ||
| <section> | ||
| <div class="weather-container container"></div> | ||
| <div class="container forecast-container"></div> | ||
| </section> | ||
| </div> | ||
|
|
||
| <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script> | ||
|
|
||
| <script src="main.js"></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,222 @@ | ||
| // Global Variables | ||
| // =================================== | ||
| const form = document.getElementsByTagName("form")[0]; | ||
| const inputField = document.querySelector(".input-control"); | ||
| const apiKey = `c8d0a8706ef69da2623e093b018f765f`; | ||
| let weatherData; | ||
| let forecastData; | ||
|
|
||
| /** | ||
| * Fetches weather data for a given city | ||
| * @param {string} query - The city name to search for | ||
| * @returns {Promise<Object>} Weather data object or error object | ||
| */ | ||
| const getWeatherData = async (query) => { | ||
| try { | ||
| const response = await fetch( | ||
| `https://api.openweathermap.org/data/2.5/weather?q=${query}&appid=${apiKey}&units=imperial` | ||
| ); | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to find city: ${query} (Status: ${response.status})` | ||
| ); | ||
| } else { | ||
| const data = await response.json(); | ||
| return { | ||
| name: data.name, | ||
| temp: data.main.temp, | ||
| weather: data.weather[0], | ||
| }; | ||
| } | ||
| } catch (error) { | ||
| console.error(error); | ||
| return { error: error.message }; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Fetches forecast data for a given city | ||
| * @param {string} query - The city name to search for | ||
| * @returns {Promise<Object>} Weather data object or error object | ||
| */ | ||
| const getForecastData = async (query) => { | ||
| try { | ||
| const response = await fetch( | ||
| `https://api.openweathermap.org/data/2.5/forecast?q=${query}&appid=${apiKey}&units=imperial` | ||
| ); | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to find city: ${query} (Status: ${response.status})` | ||
| ); | ||
| } else { | ||
| const data = await response.json(); | ||
| //reduces data array to 5-day forecast by taking every 8th forecast | ||
| const compiledForecast = data.list.reduce((acc, curr, index) => { | ||
| if (index % 8 === 0) { | ||
| acc.push({ | ||
| day: new Date(curr.dt_txt).toLocaleDateString("en-US", { | ||
| weekday: "long", | ||
| }), | ||
| temp: curr.main.temp, | ||
| weather: curr.weather[0], | ||
| }); | ||
| return acc; | ||
| } else { | ||
| return acc; | ||
| } | ||
| }, []); | ||
| return compiledForecast; | ||
| } | ||
| } catch (error) { | ||
| console.error(error); | ||
| return { error: error.message }; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Renders the current weather data to the DOM | ||
| * Creates a card displaying city name, weather icon, temperature, and description | ||
| * Also creates a "Set City as Default" button if it doesn't exist | ||
| * @returns {void} | ||
| */ | ||
| const renderWeather = () => { | ||
| const weatherDiv = document.querySelector(".weather-container"); | ||
| weatherDiv.replaceChildren(); | ||
| weatherDiv.classList.add("d-flex", "justify-content-center", "mt-4"); | ||
|
|
||
| const card = document.createElement("div"); | ||
| card.classList.add("card", "text-center", "col-12", "col-md-4", "p-0"); | ||
|
|
||
| const cardBody = document.createElement("div"); | ||
| cardBody.classList.add("card-body"); | ||
|
|
||
| const cityName = document.createElement("h2"); | ||
| cityName.classList.add("card-title", "mb-3"); | ||
| cityName.textContent = weatherData.name; | ||
|
|
||
| const weatherImg = document.createElement("img"); | ||
| weatherImg.setAttribute( | ||
| "src", | ||
| `https://openweathermap.org/img/wn/${weatherData.weather.icon}@2x.png` | ||
| ); | ||
| weatherImg.classList.add("card-img"); | ||
|
|
||
| const tempP = document.createElement("p"); | ||
| tempP.classList.add("card-text", "fs-2", "fw-bold", "mb-2"); | ||
| tempP.textContent = `${weatherData.temp}°F`; | ||
|
|
||
| const weatherP = document.createElement("p"); | ||
| weatherP.classList.add("card-text", "text-muted"); | ||
| weatherP.textContent = weatherData.weather.description; | ||
|
|
||
| cardBody.appendChild(cityName); | ||
| cardBody.appendChild(weatherImg); | ||
| cardBody.appendChild(tempP); | ||
| cardBody.appendChild(weatherP); | ||
|
|
||
| card.appendChild(cardBody); | ||
| weatherDiv.appendChild(card); | ||
|
|
||
| if (!document.querySelector(".btn-secondary")) { | ||
| const defaultBtn = document.createElement("button"); | ||
| defaultBtn.classList.add("btn", "btn-secondary"); | ||
| defaultBtn.setAttribute("type", "button"); | ||
| defaultBtn.innerText = "Set City as Default"; | ||
|
|
||
| defaultBtn.addEventListener("click", () => { | ||
| localStorage.setItem("weatherData", JSON.stringify(weatherData)); | ||
| localStorage.setItem("forecastData", JSON.stringify(forecastData)); | ||
| }); | ||
| form.appendChild(defaultBtn); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Renders the 5-day forecast data to the DOM | ||
| * Creates a list of cards displaying day of week, weather icon, temperature, and description | ||
| * Each card represents one day's forecast | ||
| * @returns {void} | ||
| */ | ||
| function renderForecast() { | ||
| const forecastsDiv = document.querySelector(".forecast-container"); | ||
| forecastsDiv.replaceChildren(); | ||
| forecastsDiv.classList.add("row", "gap-3", "justify-content-center", "mt-4"); | ||
|
|
||
| forecastData.forEach((forecast) => { | ||
| const forecastDiv = document.createElement("div"); | ||
| forecastDiv.classList.add( | ||
| "card", | ||
| "col-12", | ||
| "col-md-2", | ||
| "text-center", | ||
| "p-0" | ||
| ); | ||
|
|
||
| const cardBody = document.createElement("div"); | ||
| cardBody.classList.add("card-body"); | ||
|
|
||
| const dateP = document.createElement("p"); | ||
| dateP.classList.add("card-title", "fw-bold"); | ||
|
|
||
| const weatherImg = document.createElement("img"); | ||
| weatherImg.classList.add("card-img"); | ||
|
|
||
| const tempP = document.createElement("p"); | ||
| tempP.classList.add("card-text", "fs-4"); | ||
|
|
||
| const weatherP = document.createElement("p"); | ||
| weatherP.classList.add("card-text", "text-muted"); | ||
|
|
||
| weatherImg.setAttribute( | ||
| "src", | ||
| `https://openweathermap.org/img/wn/${forecast.weather.icon}@2x.png` | ||
| ); | ||
|
|
||
| dateP.textContent = `${forecast.day}`; | ||
| tempP.textContent = `${forecast.temp}°F`; | ||
| weatherP.textContent = `${forecast.weather.description}`; | ||
|
|
||
| cardBody.appendChild(dateP); | ||
| cardBody.appendChild(weatherImg); | ||
| cardBody.appendChild(tempP); | ||
| cardBody.appendChild(weatherP); | ||
|
|
||
| forecastDiv.appendChild(cardBody); | ||
| forecastsDiv.appendChild(forecastDiv); | ||
| }); | ||
| } | ||
|
|
||
| form.addEventListener("submit", async (e) => { | ||
| e.preventDefault(); | ||
| weatherData = await getWeatherData(inputField.value); | ||
| forecastData = await getForecastData(inputField.value); | ||
|
|
||
| //checks if data fetching returned an error, if so alert user and highlight input | ||
| if (weatherData.error || forecastData.error) { | ||
| alert(`Could not fetch weather data on this city, please try another one!`); | ||
| inputField.classList.add("is-invalid"); | ||
| inputField.focus(); | ||
| return; | ||
| } | ||
| renderWeather(); | ||
| renderForecast(); | ||
| inputField.value = ""; | ||
| }); | ||
|
|
||
| //removes invalid class from input upon change | ||
| inputField.addEventListener("input", () => { | ||
| inputField.classList.remove("is-invalid"); | ||
| }); | ||
|
|
||
| //on DOM load checks if local storage contains default city weather info, if so renders it | ||
| document.addEventListener("DOMContentLoaded", () => { | ||
| if ( | ||
| localStorage.getItem("weatherData") && | ||
| localStorage.getItem("forecastData") | ||
| ) { | ||
| weatherData = JSON.parse(localStorage.getItem("weatherData")); | ||
| forecastData = JSON.parse(localStorage.getItem("forecastData")); | ||
| renderWeather(); | ||
| renderForecast(); | ||
| } | ||
| }); | ||
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,7 @@ | ||
| body { | ||
| background-color: #f8f9fa; | ||
| } | ||
|
|
||
| .container { | ||
| padding: 20px; | ||
| } |
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.
not a good ux to alert these days