-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherForecast.js
More file actions
51 lines (44 loc) · 1.36 KB
/
WeatherForecast.js
File metadata and controls
51 lines (44 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import React, { useState, useEffect } from "react";
import "./WeatherForecast.css";
import axios from "axios";
import WeatherForecastDay from "./WeatherForecastDay";
export default function WeatherForecast(props) {
let [loaded, setLoaded] = useState(false);
let [forecast, setForecast] = useState(null);
useEffect(() => {
setLoaded(false);
}, [props.coordinates]);
function handleResponse(response) {
setForecast(response.data.daily);
setLoaded(true);
}
function load() {
let apiKey = "5f472b7acba333cd8a035ea85a0d4d4c";
let longitude = props.coordinates.lon;
let latitude = props.coordinates.lat;
let apiUrl = `https://api.openweathermap.org/data/2.5/onecall?lat=${latitude}&lon=${longitude}&appid=${apiKey}&units=metric`;
axios.get(apiUrl).then(handleResponse);
}
if (loaded) {
return (
<div className="WeatherForecast">
<div className="row">
{forecast.map(function (dailyForecast, index) {
if (index < 5) {
return (
<div className="col" key={index}>
<WeatherForecastDay data={dailyForecast} />
</div>
);
} else {
return null;
}
})}
</div>
</div>
);
} else {
load();
return null;
}
}