-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
100 lines (88 loc) · 3.34 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
const form = document.querySelector("form"); // Faster way
const input = document.querySelector("form input");
const msgSpan = form.querySelector(".msg");
const listCities = document.querySelector(".container .cities");
// localStorage.setItem(
// "apiKey",
// ? Encrypted Our Api to save it local storage.
// EncryptStringAES("c329c3b96528c0c0f92577a6850e5a5e")
// );
form.addEventListener("submit", (e) => {
// alert("Form was Submitted!");
//* to prevent default function behind submit method.
e.preventDefault();
getWeatherDataFromAPI();
form.reset();
// input.value = "";
// e.currentTarget.reset();
// e.target.reset();
});
// ! We are going to be using axios in React !
const getWeatherDataFromAPI = async () => {
const apiKey = DecryptStringAES(localStorage.getItem("apiKey"));
const cityName = input.value;
const units = "metric";
const lang = "en";
try {
// ! Http request url ( endpoint )
const url = `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${apiKey}&units=${units}&lang=${lang}`;
const response = await fetch(url).then((response) => response.json());
//* obj destructuring
const { main, name, sys, weather } = response;
const iconUrlAWS = `https://s3-us-west-2.amazonaws.com/s.cdpn.io/162656/${weather[0].icon}.svg`;
// ! Checking if the city has already been added to the list of cities.
const cityNameSpans = listCities.querySelectorAll("span");
//filter, map, reduce, forEach ==> array
//forEach => nodeList
if (cityNameSpans.length > 0) {
const filteredArray = [...cityNameSpans].filter(
(span) => span.innerText == name
);
if (filteredArray.length > 0) {
msgSpan.innerText = `You already know the weather for ${name}, Please search for another city 😉`;
setTimeout(() => {
msgSpan.innerText = "";
}, 5000);
return;
}
}
const createdLi = document.createElement("li");
createdLi.classList.add("city");
createdLi.innerHTML = ` <h2 class="city-name" data-name="${name},${
sys.country
}">
<span>${name}</span>
<sup>${sys.country}</sup>
</h2>
<div class="city-temp">${Math.round(main.temp)}<sup>°C</sup></div>
<figure>
<img class="city-icon" src="${iconUrlAWS}">
<figcaption>${weather[0].description}</figcaption>
</figure>`;
listCities.append(createdLi);
//Capturing => parent to child
listCities.addEventListener("click", (e) => {
alert("List clicked!");
});
//Bubbling => child to parent
createdLi.addEventListener("click", (e) => {
// alert("li element clicked");
window.location.href = `https://openweathermap.org/find?q=${name}`;
});
} catch (error) {
msgSpan.innerText = `City Not Found !`;
setTimeout(() => {
msgSpan.innerText = "";
}, 5000);
}
};
// ! -------------- INTERVIEW QUESTION -------------- //
// ? .class .class vs. .class.class
// ? inline(-HTML-), addEventListener, onClick, setAttribute("submit", submitFunction) - to set click method.
// ? target vs. currentTarget
// ? Status Codes Meanings
// ? What is Http and http reques and endpoint
// ? //append vs. prepend
// ? filter, map, reduce, forEach ==> array
// ? forEach => nodeList
// ! -------------- ------------------ -------------- //