-
Notifications
You must be signed in to change notification settings - Fork 0
/
forcast.js
122 lines (117 loc) · 3.2 KB
/
forcast.js
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
import React, { useState, useEffect, Component } from "react";
import axios from "axios";
import apiKeys from "./apiKeys";
import ReactAnimatedWeather from "react-animated-weather";
function Forcast(props) {
const [query, setQuery] = useState("");
const [error, setError] = useState("");
const [weather, setWeather] = useState({});
const search = (city) => {
axios
.get(
`${apiKeys.base}weather?q=${
city != "[object Object]" ? city : query
}&units=metric&APPID=${apiKeys.key}`
)
.then((response) => {
setWeather(response.data);
setQuery("");
})
.catch(function (error) {
console.log(error);
setWeather("");
setQuery("");
setError({ message: "Not Found", query: query });
});
};
function checkTime(i) {
if (i < 10) {
i = "0" + i;
} // add zero in front of numbers < 10
return i;
}
const defaults = {
color: "white",
size: 112,
animate: true,
};
useEffect(() => {
search("Delhi");
}, []);
return (
<div className="forecast">
<div className="forecast-icon">
<ReactAnimatedWeather
icon={props.icon}
color={defaults.color}
size={defaults.size}
animate={defaults.animate}
/>
</div>
<div className="today-weather">
<h3>{props.weather}</h3>
<div className="search-box">
<input
type="text"
className="search-bar"
placeholder="Search any city"
onChange={(e) => setQuery(e.target.value)}
value={query}
/>
<div className="img-box">
{" "}
<img
src="https://images.avishkaar.cc/workflow/newhp/search-white.png"
onClick={search}
/>
</div>
</div>
<ul>
{typeof weather.main != "undefined" ? (
<div>
{" "}
<li className="cityHead">
<p>
{weather.name}, {weather.sys.country}
</p>
<img
className="temp"
src={`https://openweathermap.org/img/wn/${weather.weather[0].icon}.png`}
/>
</li>
<li>
Temperature{" "}
<span className="temp">
{Math.round(weather.main.temp)}°c ({weather.weather[0].main})
</span>
</li>
<li>
Humidity{" "}
<span className="temp">
{Math.round(weather.main.humidity)}%
</span>
</li>
<li>
Visibility{" "}
<span className="temp">
{Math.round(weather.visibility)} mi
</span>
</li>
<li>
Wind Speed{" "}
<span className="temp">
{Math.round(weather.wind.speed)} Km/h
</span>
</li>
</div>
) : (
<li>
{error.query} {error.message}
</li>
)}
</ul>
</div>
</div>
);
}
export default Forcast;