App.js
3.91 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Weather broadcasting page
import { useState } from "react";
import { Navigate } from "react-router-dom";
import weatherService from "./service/weather";
// components
import Bottombar from "./components/Bottombar";
import Topbar from "./components/Topbar";
function App() {
const token = sessionStorage.getItem("user-token");
const [visited, setVisited] = useState(0);
const [weather, setWeather] = useState();
const [forecast, setForecast] = useState();
const [air, setAir] = useState();
const handleData = async (e) => {
setVisited(1);
setWeather(null);
setForecast(null);
setAir(null);
// TODO: settimeout for data fetch from openweather api?
if (e.target.id === "weather") {
const weatherData = await weatherService.getWeather(token);
setWeather(weatherData);
} else if (e.target.id === "forecast") {
const forecasetData = await weatherService.getForecaset(token);
setForecast(forecasetData);
} else if (e.target.id === "air") {
const airData = await weatherService.getAirPollution(token);
setAir(airData);
}
};
if (!token) {
return <Navigate to="/login" />;
}
return (
<div>
<Topbar />
<div className="mainBox">
<div className="weather-buttons">
<button id="weather" onClick={(e) => handleData(e)}>
Weather
</button>
<button id="forecast" onClick={(e) => handleData(e)}>
Forecast
</button>
<button id="air" onClick={(e) => handleData(e)}>
Air Pollution
</button>
</div>
<hr></hr>
{!visited && !weather && !forecast && !air ? (
<h2>Click a button for weather service!</h2>
) : null}
{visited && !weather && !forecast && !air ? <h2>Loading...</h2> : null}
{!weather ? (
""
) : (
<div>
<h2>
{weather.meta.city} ({weather.meta.country}){" "}
<small>UTC {weather.meta.timezone}</small>
</h2>
<h3>* {weather.description} *</h3>
<p>
🌡 : {weather.temp.realCelcius} ℃ / feels like{" "}
{weather.temp.feelCelcius} C
</p>
<p>🌬 : {weather.types.wind} m/s</p>
<p>☁️ : {weather.types.clouds} %</p>
<p>{weather.rain ? "☔️ : Yes" : "☔️ : No"}</p>
<p>{weather.snow ? "❄️ : Yes" : "❄️ : No"}</p>
</div>
)}
{!forecast ? (
""
) : (
<div>
<h2>
{forecast.meta.city} ({forecast.meta.country}){" "}
<small>UTC {forecast.meta.timezone}</small>
</h2>
{forecast.forecast.map((item, index) => (
<div key={index} className="forecastItemBox">
<h3>
{item.description} <small>{item.dateTime}</small>
</h3>
<p>
Temperature : {item.temp.realCelcius} ℃ / feels like{" "}
{item.temp.feelCelcius} ℃
</p>
<p>Wind : {item.types.wind} m/s</p>
<p>Cloud : {item.types.clouds} %</p>
</div>
))}
</div>
)}
{!air ? (
""
) : (
<div>
<h2>
{air.meta.state ? air.meta.state : ""} {air.meta.country}
</h2>
<p>CO : {air.airData.co} μg/m3</p>
<p>NH3 : {air.airData.nh3} μg/m3</p>
<p>NO : {air.airData.no} μg/m3</p>
<p>NO2 : {air.airData.no2} μg/m3</p>
<p>O3 : {air.airData.o3} μg/m3</p>
<p>SO2 : {air.airData.so2} μg/m3</p>
<p>
{"pm2.5"} : {air.airData.pm2_5} μg/m3
</p>
<p>pm10 : {air.airData.pm10} μg/m3</p>
</div>
)}
</div>
<Bottombar />
</div>
);
}
export default App;