-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
267 lines (235 loc) · 8.8 KB
/
app.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* ---------------------------------- */
/* Initials */
/* ---------------------------------- */
let cities = [];
/* ---------------------------------- */
/* Selectors */
/* ---------------------------------- */
const enterBtn = document.querySelector(".btn");
const inputText = document.querySelector("form input");
const messagePar = document.querySelector(".message");
const appTitle = document.querySelector("center h1");
let lang = "en";
/* ---------------------------------- */
/* API */
/* ---------------------------------- */
const getWeather = async (cityName) => {
try {
console.log("City Name:", cityName); // For checking city name
const API_KEY = "19fadf383f77445c7ead85a8d7ccce88";
const fetchAPI = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&units=metric&lang=${lang}&appid=${API_KEY}`
);
if (!fetchAPI.ok) {
throw new Error(`"${cityName}" Not Found!`);
}
const cityWeather = await fetchAPI.json();
console.log("Response from API: ", cityWeather); // For checking API Response
showOnScreen(cityWeather);
} catch (error) {
if (lang == "de") {
messagePar.textContent = `Stadt nicht gefunden`;
} else {
messagePar.textContent = `City Not Found!`;
}
console.error("Error:", error.message);
setTimeout(() => {
messagePar.classList.replace("d-block", "d-none");
}, 3000);
}
};
/* ---------------------------------- */
/* Event Listeners */
/* ---------------------------------- */
inputText.addEventListener("keypress", (e) => {
if (e.key == "Enter") {
e.preventDefault(); // prevent form submiting
const cityName = capitalizeFirstLetter(inputText.value);
console.log("Key `Enter` Pressed => City name:", cityName); // For checking city name (onkeypress)
getWeather(cityName);
inputText.value = "";
}
});
enterBtn.addEventListener("click", (e) => {
e.preventDefault(); // prevent form submiting
const cityName = capitalizeFirstLetter(inputText.value);
console.log("Clicked on button. City name:", cityName); // For checking city name (onclick)
getWeather(cityName);
inputText.value = "";
});
// change language
const langEN = document.querySelector(".en");
langEN.onclick = () => {
lang = "en";
appTitle.innerHTML = `<img src="assets/weather-logo.png" alt="weather-logo" style="width: 30px;" class="mb-1">
Weather App</h1>`;
inputText.setAttribute("placeholder", "Search for a city");
document.querySelector(
".bottom-container"
).innerHTML = `<small>Coded by <span>❤</span> omrfrkcpr</a>
</small>`;
};
const langDE = document.querySelector(".de");
langDE.onclick = () => {
lang = "de";
appTitle.innerHTML = `<img src="assets/weather-logo.png" alt="weather-logo" style="width: 30px;" class="mb-1">
Wetter App</h1>`;
inputText.setAttribute("placeholder", "Suche nach einer Stadt");
document.querySelector(
".bottom-container"
).innerHTML = `<small>Codiert von <span>❤</span> omrfrkcpr</a>
</small>`;
};
/* ---------------------------------- */
/* Functions */
/* ---------------------------------- */
const showOnScreen = (cityWeather) => {
const iconSrc = cityWeather.weather[0].icon
? `https://s3-us-west-2.amazonaws.com/s.cdpn.io/162656/${cityWeather.weather[0].icon}.svg`
: "assets/404.png";
// const iconSrc = cityWeather.weather[0].icon
// ? `https://openweathermap.org/img/wn/${cityWeather.weather[0].icon}@2x.png`
// : "assets/404.png";
console.log("City Weather on Screen:", cityWeather); // For checking city Weather on Screen
const citiesUl = document.querySelector(".cities");
const { name, main, weather, sys } = cityWeather;
if (cities.indexOf(name) == -1) {
citiesUl.innerHTML =
`
<li class="list-unstyled bg-warning p-1 pt-1 rounded-4 mb-1 d-flex flex-column justify-content-between align-items-between">
<div class="text-end mb-1">
<button class="remove border-0 bg-warning px-1"><i class="fa fa-times" aria-hidden="true"></i>
</button>
</div>
<div class="d-flex text-center city-info justify-content-center">
<h2 class="text-secondary">${name}</h2>
<p class="bg-warning-subtle h-50 rounded-3 px-1 text-black">${
sys.country
}</p>
</div>
<div class="city-temperature display-5 fw-bold text-center d-flex justify-content-center">
<span class="temperature">${main.temp.toFixed(0)}</span>
<span class="unit">°C</span>
</div>
<div class="weather-info text-center text-secondary justify-content-center">
<div class="icon">
<img src=${iconSrc} alt="" style="width: 100px;">
</div>
<div class="description mb-2">${
weather[0].description.toUpperCase()
? weather[0].description.toUpperCase()
: "No Description!"
}</div>
</div>
</li>
` + citiesUl.innerHTML;
cities.push(name);
console.log(cities);
} else {
if (lang == "de") {
messagePar.textContent = "Sie kennen dieses Wetter bereits";
} else {
messagePar.textContent = "You already know this city weather";
}
setTimeout(() => {
messagePar.classList.replace("d-block", "d-none");
}, 5000);
}
// ------------------------------------------------------------------
// const newLi = document.createElement("li");
// newLi.classList.add(
// "list-unstyled",
// "bg-warning",
// "p-3",
// "pt-2",
// "rounded-4",
// "mb-2"
// );
// // İçerisine alt öğeleri ekle
// const div1 = document.createElement("div");
// div1.classList.add("text-end", "mb-1");
// const removeBtn = document.createElement("button");
// removeBtn.classList.add("remove", "border-0", "bg-warning", "px-1");
// removeBtn.innerHTML = '<i class="fa fa-times" aria-hidden="true"></i>';
// div1.appendChild(removeBtn);
// newLi.appendChild(div1);
// const div2 = document.createElement("div");
// div2.classList.add(
// "d-flex",
// "text-center",
// "justify-content-center",
// "city-info"
// );
// const h2 = document.createElement("h2");
// h2.classList.add("text-secondary");
// h2.textContent = name;
// div2.appendChild(h2);
// const p = document.createElement("p");
// p.classList.add(
// "bg-warning-subtle",
// "h-50",
// "rounded-3",
// "px-1",
// "text-black"
// );
// p.textContent = sys.country;
// div2.appendChild(p);
// newLi.appendChild(div2);
// const div3 = document.createElement("div");
// div3.classList.add(
// "city-temperature",
// "display-5",
// "fw-bold",
// "text-center",
// "justify-content-center",
// "d-flex"
// );
// const tempSpan = document.createElement("span");
// tempSpan.classList.add("temperature", "text-center");
// tempSpan.textContent = main.temp.toFixed(0);
// div3.appendChild(tempSpan);
// const unitSpan = document.createElement("span");
// unitSpan.classList.add("unit", "text-center");
// unitSpan.textContent = "°C";
// div3.appendChild(unitSpan);
// newLi.appendChild(div3);
// const div4 = document.createElement("div");
// div4.classList.add("weather-info", "text-center", "text-secondary");
// const iconDiv = document.createElement("div");
// iconDiv.classList.add("icon");
// const iconImg = document.createElement("img");
// iconImg.src = iconSrc;
// iconImg.alt = "";
// iconImg.style.width = "100px";
// iconDiv.appendChild(iconImg);
// div4.appendChild(iconDiv);
// const descDiv = document.createElement("div");
// descDiv.classList.add("description");
// descDiv.textContent = weather[0].description.toUpperCase()
// ? weather[0].description.toUpperCase()
// : "No Description!";
// div4.appendChild(descDiv);
// newLi.appendChild(div4);
// citiesUl içerisine yeni li öğesini ekle
// const citiesUl = document.querySelector(".cities");
// const firstLi = citiesUl.firstChild; // listenin ilk öğesini al
// citiesUl.insertBefore(newLi, firstLi); // yeni öğeyi ilk öğenin öncesine ekle
// -------------------------------------------------------------------
// remove closest li after clicking on remove button
document.querySelectorAll(".remove").forEach((removeBtn) => {
removeBtn.addEventListener("click", (e) => {
const removedLi = e.target.closest("li");
//remove from cities array
cities.splice(
cities.indexOf(removedLi.querySelector("h2").textContent),
1
);
console.log(cities);
// remove from screen (ul)
removedLi.remove();
});
});
};
function capitalizeFirstLetter(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}