-
Notifications
You must be signed in to change notification settings - Fork 15
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
Weather App (Ben) #5
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,83 @@ | ||
import logo from "/logo.png"; | ||
import "./App.css"; | ||
import { useState } from "react"; | ||
import axios from "axios"; | ||
import WeatherTable from "./WeatherTable"; | ||
|
||
function App() { | ||
const [city, setCity] = useState(""); | ||
const [weatherData, setWeatherData] = useState(null); | ||
const [hourlyData, setHourlyData] = useState(null); | ||
const [error, setError] = useState(); | ||
|
||
const handleChange = (event) => { | ||
setCity(event.target.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. Can try to come back and add in a .env for practice |
||
const API_Key = "119b0643de27a258eb734cc83e735875" | ||
|
||
const handleSubmit = (event) => { | ||
event.preventDefault(); | ||
axios | ||
.get( | ||
`https://api.openweathermap.org/geo/1.0/direct?q=${city}&limit=1&appid=${API_Key}` | ||
) | ||
.then((response) => response.data[0]) | ||
.then((cityGeoData) => { | ||
const cityData = cityGeoData; | ||
return Promise.all([ | ||
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. Great use of promise all to return a set of data and an API call |
||
cityData, | ||
axios.get( | ||
`https://api.openweathermap.org/data/2.5/weather?lat=${cityGeoData.lat}&lon=${cityGeoData.lon}&appid=${API_Key}&units=metric` | ||
), | ||
]); | ||
} | ||
) | ||
.then(([cityData, weatherResponse]) => { | ||
const cityGeoData = cityData | ||
const { data: weatherData } = weatherResponse; | ||
setWeatherData(weatherData); | ||
setError(null) | ||
return axios.get( | ||
`https://api.openweathermap.org/data/2.5/forecast?lat=${cityGeoData.lat}&lon=${cityGeoData.lon}&appid=${API_Key}` | ||
); | ||
}) | ||
.then ((response) => { | ||
const { data: hourlyData } = response; | ||
setHourlyData(hourlyData.list) | ||
}) | ||
.catch((err) => { | ||
setWeatherData(null) | ||
setError(err.code) | ||
} | ||
); | ||
}; | ||
|
||
return ( | ||
<> | ||
<div> | ||
<img src={logo} className="logo react" alt="Rocket logo" /> | ||
</div> | ||
<h1>Weather App</h1> | ||
<div className="card"> | ||
{/* Follow the weather app instructions on the gitbook to implement this exercise */} | ||
<form onSubmit={handleSubmit}> | ||
<label>Please Enter a City: </label> | ||
<input type="text" onChange={handleChange} /> | ||
<div style={{margin:"1rem"}}><input type="submit"/></div> | ||
</form> | ||
</div> | ||
{weatherData? | ||
<div> | ||
<div><span>City: </span>{weatherData.name}</div> | ||
<div>Temperature: {weatherData.main.feels_like}</div> | ||
<div>Weather: {weatherData.weather[0].main}, {weatherData.weather[0].description}</div> | ||
</div> : null } | ||
|
||
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. Nice work getting the hourly data |
||
<div style={{margin:"1rem"}}> | ||
{hourlyData? <WeatherTable data={hourlyData} /> : null} | ||
</div> | ||
|
||
{error? <div>Error: {error}</div> : null} | ||
</> | ||
); | ||
} | ||
|
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.
Dont need to wrap setCity in handleChange