-
Notifications
You must be signed in to change notification settings - Fork 22
finished project #7
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
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,14 +1,77 @@ | ||
| import logo from "/logo.png"; | ||
| import "./App.css"; | ||
| import axios from "axios"; | ||
| import { useState } from "react"; | ||
|
|
||
| const WEATHER_API_KEY = "2e9b3bbe3e4c91069c9aef609fd54f11"; | ||
|
|
||
| function App() { | ||
| const [cityInputValue, setCityInputValue] = useState(""); | ||
| const [currCity, setCurrCity] = useState(""); | ||
| const [currTemp, setCurrTemp] = useState(""); | ||
| const [weatherType, setWeatherType] = useState(""); | ||
| const [weatherDesc, setWeatherDesc] = useState(""); | ||
| const [weatherIcon, setweatherIcon] = useState(""); | ||
|
|
||
| const handleSubmit = (event) => { | ||
| event.preventDefault(); | ||
| axios | ||
| .get( | ||
| `https://api.openweathermap.org/geo/1.0/direct?q=${cityInputValue}&limit=1&appid=${WEATHER_API_KEY}` | ||
| ) | ||
| .then((response) => { | ||
| const cityGeoData = response.data[0]; | ||
| return cityGeoData; | ||
|
Contributor
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 dont need to make a variable and then return it .then((response) => response.data[0]) |
||
| }) | ||
| .then((cityGeoData) => | ||
| axios.get( | ||
| `https://api.openweathermap.org/data/2.5/weather?lat=${cityGeoData.lat}&lon=${cityGeoData.lon}&appid=${WEATHER_API_KEY}&units=metric` | ||
| ) | ||
| ) | ||
| .then((response) => { | ||
| const { data: weatherData } = response; | ||
| setCityInputValue(""); | ||
| setCurrCity(weatherData.name); | ||
| setCurrTemp(weatherData.main.temp); | ||
| setWeatherType(weatherData.weather[0].main); | ||
| setWeatherDesc(weatherData.weather[0].description); | ||
| setweatherIcon(weatherData.weather[0].icon); | ||
| }); | ||
| }; | ||
|
|
||
| const weatherInfo = currCity ? ( | ||
| <div> | ||
| <img | ||
| src={`https://openweathermap.org/img/wn/${weatherIcon}@2x.png`} | ||
| alt="weather-icon" | ||
| /> | ||
| <p>Current City: {currCity}</p> | ||
| <p>Current Temprature: {currTemp}°C</p> | ||
| <p> | ||
| Current Weather: {weatherType}, {weatherDesc} | ||
| </p> | ||
| </div> | ||
| ) : ( | ||
| <p>Please enter a city name to get its weather data</p> | ||
| ); | ||
|
|
||
| return ( | ||
| <> | ||
| <div> | ||
| <img src={logo} className="logo react" alt="Rocket logo" /> | ||
| </div> | ||
| <h1>Weather App</h1> | ||
| <div className="card"> | ||
| <form onSubmit={handleSubmit}> | ||
| <label>{"City:"}</label> | ||
| <input | ||
| type="text" | ||
| value={cityInputValue} | ||
| onChange={(e) => setCityInputValue(e.target.value)} | ||
| /> | ||
| <button type="submit">Check Weather</button> | ||
| </form> | ||
| {weatherInfo} | ||
| {/* Follow the weather app instructions on the gitbook to implement this exercise */} | ||
| </div> | ||
| </> | ||
|
Contributor
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 only have one commit, you should commit and push more so that you have code to fall back on if something goes wrong with your machine. |
||
|
|
||
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.
Come back and try use a .env to store this credential!