Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"preview": "vite preview"
},
"dependencies": {
"axios": "^1.6.2",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
69 changes: 68 additions & 1 deletion src/App.jsx
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) => {
Copy link
Contributor

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

setCity(event.target.value);
}

Copy link
Contributor

Choose a reason for hiding this comment

The 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([
Copy link
Contributor

Choose a reason for hiding this comment

The 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 }

Copy link
Contributor

Choose a reason for hiding this comment

The 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}
</>
);
}
Expand Down
Loading