-
Notifications
You must be signed in to change notification settings - Fork 22
Weather app- Iffah #6
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,3 +22,4 @@ dist-ssr | |
| *.njsproj | ||
| *.sln | ||
| *.sw? | ||
| .env | ||
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,15 +1,81 @@ | ||
| import logo from "/logo.png"; | ||
| import "./App.css"; | ||
| import { useState, useEffect } from "react"; | ||
| import axios from "axios"; | ||
| import WeatherCard from "./weather"; | ||
| import Forecast from "./forecast"; | ||
|
|
||
| function App() { | ||
| const [city, setCity] = useState(""); | ||
| const [response, setResponse] = useState([]); | ||
| const [weather, setWeather] = useState([]); | ||
| const [forecast, setForecast] = useState([]); | ||
|
|
||
| let lat = ""; | ||
| let lon = ""; | ||
| const key = import.meta.env.VITE_SOME_WEATHER_API_KEY; | ||
|
|
||
|
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. Great use of .env to store your key
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. Superb .then chain and use of returns |
||
| const handleSubmit = (e) => { | ||
| e.preventDefault(); | ||
| axios | ||
| .get( | ||
| `http://api.openweathermap.org/geo/1.0/direct?q=${city}&limit=1&appid=${key}` | ||
| ) | ||
| .then((response) => response.data[0]) | ||
| .then((cityGeoData) => { | ||
| lat = cityGeoData.lat; | ||
| console.log(lat); | ||
| lon = cityGeoData.lon; | ||
| console.log(`lon ${lon}`); | ||
| return axios.get( | ||
| `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${key}&units=metric` | ||
| ); | ||
| }) | ||
| .then((response) => { | ||
| const { data: weatherData } = response; | ||
| console.log(`WEATHER ${weatherData}`); | ||
| setWeather(weatherData); | ||
| //set forecast | ||
| return axios.get( | ||
| `https://api.openweathermap.org/data/2.5/forecast?lat=${lat}&lon=${lon}&appid=${key}&units=metric` | ||
| ); | ||
| }) | ||
| .then((response) => { | ||
| const { data: forecastData } = response; | ||
| console.log(`forecast ${forecastData}`); | ||
| setForecast(forecastData); | ||
| }); | ||
| }; | ||
|
|
||
| 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={(event) => handleSubmit(event)}> | ||
| <input | ||
| name="search" | ||
| type="text" | ||
| value={city} | ||
| onChange={(e) => { | ||
| setCity(e.target.value); | ||
| }} | ||
| /> | ||
| <button type="submit"> Search </button> | ||
| </form> | ||
| <h1>City: {city}</h1> | ||
| {typeof weather.main != "undefined" ? ( | ||
| <WeatherCard weatherData={weather} /> | ||
| ) : ( | ||
| <div> </div> | ||
| )} | ||
| {typeof forecast.list != "undefined" ? ( | ||
| <Forecast forecastData={forecast} /> | ||
| ) : ( | ||
| <div> </div> | ||
| )} | ||
| </div> | ||
| </> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import React from "react"; | ||
|
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. Great implementation of the table, can remove the react import |
||
| import "./App.css"; | ||
|
|
||
| const Forecast = ({ forecastData }) => ( | ||
| <div> | ||
| <table> | ||
| <thead> | ||
| <tr> | ||
| <th> Date</th> | ||
| <th> Temperature</th> | ||
| <th> Feels like</th> | ||
| <th> Description</th> | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| {forecastData.list.map((list, i) => ( | ||
| <tr key={i}> | ||
| <td> | ||
| {new Date(list.dt_txt).toLocaleString("en-GB", { | ||
| timeZone: "Asia/Singapore", | ||
| })} | ||
| </td> | ||
| <td> {list.main.temp} °C</td> | ||
| <td> {list.main.feels_like} °C</td> | ||
| <td> {list.weather[0].main} </td> | ||
| </tr> | ||
| ))} | ||
| </tbody> | ||
| </table> | ||
| </div> | ||
| ); | ||
|
|
||
| export default Forecast; | ||
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.
Nice work splitting the JSX into multiple components.