Skip to content

Commit d093309

Browse files
committed
Fetching and mapping data from weather API
1 parent 4adf68c commit d093309

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed

src/App.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,44 @@
1+
import { useState } from 'react';
2+
13
import './App.css';
24
import CurrentWeather from './components/current-weather/CurrentWeather';
35
import Search from './components/search/Search';
6+
import { WEATHER_API_URL, WEATHER_API_KEY } from './api';
47

58
function App() {
9+
const [currentWeather, setCurrrentWeather] = useState(null);
10+
const [forecast, setForecast] = useState(null);
11+
612
const handleOnSearchChange = (searchData) => {
7-
console.log(searchData);
13+
const [lat, lon] = searchData.value.split(' ');
14+
15+
const currentWeatherFetch = fetch(
16+
`${WEATHER_API_URL}/weather?lat=${lat}&lon=${lon}&appid=${WEATHER_API_KEY}&units=metric`
17+
);
18+
const forecastFetch = fetch(
19+
`${WEATHER_API_URL}/forecast?lat=${lat}&lon=${lon}&appid=${WEATHER_API_KEY}&units=metric`
20+
);
21+
22+
Promise.all([currentWeatherFetch, forecastFetch])
23+
.then(async (response) => {
24+
const weatherResponse = await response[0].json();
25+
const forecastResponse = await response[1].json();
26+
27+
setCurrrentWeather({ city: searchData.label, ...weatherResponse });
28+
setForecast({ city: searchData.label, ...forecastResponse });
29+
})
30+
.catch((err) => {
31+
console.log(err);
32+
});
833
};
934

35+
console.log(currentWeather);
36+
console.log(forecast);
37+
1038
return (
1139
<div className="container">
1240
<Search onSearchChange={handleOnSearchChange} />
13-
<CurrentWeather />
41+
{currentWeather && <CurrentWeather data={currentWeather} />}
1442
</div>
1543
);
1644
}

src/api.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ export const geoApiOptions = {
99
};
1010

1111
export const GEO_API_URL = 'https://wft-geo-db.p.rapidapi.com/v1/geo';
12+
13+
export const WEATHER_API_URL = 'https://api.openweathermap.org/data/2.5';
14+
export const WEATHER_API_KEY = process.env.REACT_APP_WEATHER_KEY;

src/components/current-weather/CurrentWeather.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,42 @@
11
import './current-weather.css';
22

3-
const CurrentWeather = () => {
3+
const CurrentWeather = ({ data }) => {
44
return (
55
<div className="weather">
66
<div className="top">
77
<div>
8-
<p className="city">CITY</p>
9-
<p className="weather-description">DESCRIPTION</p>
8+
<p className="city">{data.city}</p>
9+
<p className="weather-description">{data.weather[0].description}</p>
1010
</div>
11-
<img className="weather-icon" src="icons/01d.png" alt="weather" />
11+
<img
12+
className="weather-icon"
13+
src={`icons/${data.weather[0].icon}.png`}
14+
alt="weather"
15+
/>
1216
</div>
1317
<div className="bottom">
14-
<p className="temperature">18°C</p>
18+
<p className="temperature">{Math.round(data.main.temp)}°C</p>
1519
<div className="details">
1620
<div className="parameter-row">
1721
<span className="parameter-label">Details:</span>
1822
</div>
1923
<div className="parameter-row">
2024
<span className="parameter-label">Feels like</span>
21-
<span className="parameter-value">22°C</span>
25+
<span className="parameter-value">
26+
{Math.round(data.main.temp)}°C
27+
</span>
2228
</div>
2329
<div className="parameter-row">
2430
<span className="parameter-label">Wind</span>
25-
<span className="parameter-value">2 m/s</span>
31+
<span className="parameter-value">{data.wind.speed} m/s</span>
2632
</div>
2733
<div className="parameter-row">
2834
<span className="parameter-label">Humidity</span>
29-
<span className="parameter-value">15%</span>
35+
<span className="parameter-value">{data.main.humidity}%</span>
3036
</div>
3137
<div className="parameter-row">
3238
<span className="parameter-label">Pressure</span>
33-
<span className="parameter-value">15 hPa</span>
39+
<span className="parameter-value">{data.main.pressure} hPa</span>
3440
</div>
3541
</div>
3642
</div>

src/components/current-weather/current-weather.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
}
6161

6262
.parameter-value {
63+
margin-right: 5px;
6364
text-align: right;
6465
font-weight: 600;
6566
font-size: 12px;

0 commit comments

Comments
 (0)