-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeather.jsx
156 lines (148 loc) · 5.49 KB
/
Weather.jsx
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import React from "react";
import { useState } from "react";
import "./weatherapp.css";
import Clouds from "./cloud.png";
import searchicon from "./search.png";
import snow from "./snow.png";
import Rain from "./storm.png";
import Clear from "./sun.png";
import wind from "./wind.png";
import humidity from "./humidity.png";
import Haze from "./Haze.png";
import Mist from "./Haze.png";
function Weather() {
const [city, setcity] = useState({
name: "Chennai",
country: "IN",
lat: 13.0827,
long: 80.2707,
temp: "",
});
const [weather, setweather] = useState(Clear);
const [additional, setadditional] = useState({
humid: 100,
air: 0,
temp: 34,
});
const [loading, setloading] = useState("yes");
const search = async () => {
let url = `https://api.openweathermap.org/data/2.5/weather?q=${city.temp}&appid=687cc5966fe261a7ec614af6ab4f9333`;
setloading("yes");
try {
let res = await fetch(url);
let data = await res.json();
setcity({
...city,
name: data.name,
lat: data.coord.lat,
long: data.coord.lon,
country: data.sys.country,
});
setadditional({
...additional,
humid: data.main.humidity,
temp: data.main.temp - 273.15,
air: data.wind.speed,
});
setweather(eval(data.weather[0].main));
} catch (error) {
console.error("Error in fetching", error.message);
setcity({
...city,
name: "City Not Available",
lat: "---",
long: "---",
country: "Not Available",
});
setadditional({ ...additional, temp: 0, air: 0, humid: 0 });
} finally {
setloading("no");
}
};
return (
<>
<div id="container">
<div id="input-container">
<input
type="text"
name="name"
id="city"
placeholder="Search city"
autoComplete="off"
onChange={(e) => {
setcity({ ...city, temp: e.target.value });
}}
onKeyUp={(e) => {
if (e.key === 'Enter') {
search();
}
}}
/>
<img
src={searchicon}
alt="search-icon"
id="search-icon"
onClick={search}
/>
</div>
{loading === "yes" && (
<div id="city-container">
<h1 id="loading">
PLease wait...
<br></br> we are loading
</h1>
<iframe src="https://lottie.host/embed/df1a0015-60fd-4975-ab04-4d18a9f10392/OP8lc8nBhM.json"></iframe>
</div>
)}
{loading === "no" && (
<div id="city-container">
<img src={weather} alt="current-weather" id="current" />
<h1 id="cityname">
{city.name}--{additional.temp.toFixed(1)}° C
</h1>
<h1>--{city.country}--</h1>
{city.lat == "---" ? (
<h1>---</h1>
) : (
<h1>Lat:{city.lat.toFixed(6)}° N</h1>
)}
{city.lat == "---" ? (
<h1>---</h1>
) : (
<h1>Long:{city.long.toFixed(6)}° E</h1>
)}
</div>
)}
{loading == "no" && (
<div id="additional">
<span id="humidity">
<img src={humidity} alt="Humidity" />
<p className="hu">Humidity</p>
<h3>{additional.humid}%</h3>
</span>
<span id="air">
<img src={wind} alt="Wind" />
<p className="humid">wind Speed</p>
<h3>{additional.air}km/hr</h3>
</span>
</div>
)}
{loading == "yes" && (
<div id="additional">
<span id="humidity">
<img src={humidity} alt="Humidity" />
<p className="hu">Humidity</p>
<h3>----</h3>
</span>
<span id="air">
<img src={wind} alt="Wind" />
<p className="humid">wind Speed</p>
<h3>----</h3>
</span>
</div>
)}
</div>
</>
);
}
export default Weather;