Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ dist-ssr
*.njsproj
*.sln
*.sw?
.env
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
68 changes: 67 additions & 1 deletion src/App.jsx
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";
Copy link
Copy Markdown
Contributor

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.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great use of .env to store your key

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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>
</>
);
Expand Down
33 changes: 33 additions & 0 deletions src/forecast.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from "react";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great implementation of the table, can remove the react import
consider using mui or bootstrap for easy styling and responsive sizes.

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} &deg;C</td>
<td> {list.main.feels_like} &deg;C</td>
<td> {list.weather[0].main} </td>
</tr>
))}
</tbody>
</table>
</div>
);

export default Forecast;
Loading