-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
154 lines (147 loc) · 4.06 KB
/
weather.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
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
const API_Key = "616b8c33560dedc066c91582334a30c2";
// ====== Get HTML Element ======
const getElem = (idClass) => document.getElementById(idClass);
// ======== Fetch City ======
const fetchCity = (search) => {
getElem("default").style.display = "none";
getElem("loader").style.display = "inline-block";
getElem("loader2").style.display = "inline-block";
const url = `https://api.openweathermap.org/geo/1.0/direct?q=${search}&appid=${API_Key}&units=metric`;
fetch(url)
.then((res) => res.json())
.then((data) => fetchData(data))
.catch((err) => alert("Something Went Wrong. Please Search A Valid Input"));
};
// ===== Fetch DATA ======
const fetchData = (data) => {
const lat = data[0].lat;
const lon = data[0].lon;
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_Key}`;
fetch(url)
.then((res) => res.json())
.then((data) => searchUiAction(data));
};
// ====== Search Button Action =======
const searchBtnAction = () => {
const inputVal = getElem("search-input").value;
fetchCity(inputVal);
};
// ====== Search UI Action ======
const searchUiAction = (data) => {
const { weather, main, visibility, wind, clouds, sys, name, timezone } = data;
const mainCardContainer = getElem("main-card");
mainCardContainer?.childNodes[3]?.remove();
const mainCard = document.createElement("div");
mainCard.classList.add("mx-auto");
mainCard.classList.add("card");
mainCard.setAttribute("style", "width:18rem");
mainCard.innerHTML = `<div class="bg-light">
<img
src="http://openweathermap.org/img/w/${weather[0].icon}.png"
class="card-img-top w-25 mx-auto"
alt="..."
/>
</div>
<div class="card-body">
<h4 class="fw-bold">${name}</h4>
<p class="text-capitalize card-text">
${weather[0].description}
</p>
</div>`;
mainCardContainer.appendChild(mainCard);
// ----- Details Section ----
const moreDetailsContainer = getElem("moreDetails");
moreDetailsContainer?.childNodes[3]?.remove();
const moreDetails = document.createElement("tbody");
moreDetails.innerHTML = `
<tr>
<th scope="row">01</th>
<td class="table-active text-start" colspan="2">
Temperature:
</td>
<td>${main.temp}℃</td>
</tr>
<tr>
<th scope="row">02</th>
<td class="table-active text-start" colspan="2">
Feels:
</td>
<td>${main.feels_like}℃</td>
</tr>
<tr>
<th scope="row">03</th>
<td class="table-active text-start" colspan="2">
Minimum Temperature:
</td>
<td>${main.temp_min}℃</td>
</tr>
<tr>
<th scope="row">04</th>
<td class="table-active text-start" colspan="2">
Maximum Temperature:
</td>
<td>${main.temp_max}℃</td>
</tr>
<tr>
<th scope="row">05</th>
<td class="table-active text-start" colspan="2">
Pressure:
</td>
<td>${main.pressure}mbar</td>
</tr>
<tr>
<th scope="row">06</th>
<td class="table-active text-start" colspan="2">
Humidity:
</td>
<td>${main.humidity}%</td>
</tr>
<tr>
<th scope="row">07</th>
<td class="table-active text-start" colspan="2">
Visibility:
</td>
<td>${visibility}m</td>
</tr>
<tr>
<th scope="row">08</th>
<td class="table-active text-start" colspan="2">
Sunrise:
</td>
<td>${new Date(sys.sunrise * 1000)}</td>
</tr>
<tr>
<th scope="row">09</th>
<td class="table-active text-start" colspan="2">
Sunset:
</td>
<td>${new Date(sys.sunset * 1000)}</td>
</tr>
<tr>
<th scope="row">10</th>
<td class="table-active text-start" colspan="2">
Wind Speed:
</td>
<td>${wind.speed}km/h</td>
</tr>
<tr>
<th scope="row">11</th>
<td class="table-active text-start" colspan="2">
Wind Direction:
</td>
<td>${wind.deg}degree</td>
</tr>
`;
moreDetailsContainer.appendChild(moreDetails);
getElem("loader2").style.display = "none";
getElem("loader").style.display = "none";
};
// ======= Search Handler ======
getElem("search-btn").addEventListener("click", searchBtnAction);
// ======= Search Handler By ENTER======
getElem("search-input").addEventListener("keyup", (event) => {
if (event.key == "Enter") {
getElem("search-input").blur();
searchBtnAction();
}
});