-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
73 lines (67 loc) · 2.75 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
const form = document.querySelector("section.top-banner form");
const input = document.querySelector(".top-banner input");
const msg = document.querySelector("span.msg");
const list = document.querySelector(".ajax-section .cities");
// localStorage.setItem("apiKey", EncryptStringAES("4d8fb5b93d4af21d66a2948710284366"));
form.addEventListener("submit", (e) =>{
e.preventDefault();
getWeatherDataFromApi();
});
// function getWeatherDataFromApi(){}
const getWeatherDataFromApi = async() =>{
// alert("http request gone");
// input.value = "";
let tokenKey = DecryptStringAES(localStorage.getItem("apiKey"));
// console.log(apikey);
let inputVal = input.value;
let unitType = "metric";
let lang = "tr";
let url = `https://api.openweathermap.org/data/2.5/weather?q=${inputVal}&appid=${tokenKey}&units=${unitType}`;
try {
// const response = await fetch.get(url).then(response => response.json());
//axios.get(url) == axios(url)
const response = await axios(url);
const { name, main, sys, weather } = response.data;
// console.log(response.data);
let iconUrl = `http://openweathermap.org/img/wn/${weather[0].icon}@2x.png`;
//forEach => array + nodeList
//map, filter, reduce => array
const cityListItems = list.querySelectorAll(".city");
const cityListItemsArray = Array.from(cityListItems);
if(cityListItemsArray.length > 0){
const filteredArray = cityListItemsArray.filter(cityCard => cityCard.querySelector("span").innerText == name);
// console.log(cityListItemsArray.length);
if(filteredArray.length > 0){
msg.innerText = `You already know the weather for ${name}, Please search for another city 😉`;
setTimeout(()=>{
msg.innerText = "";
}, 5000);
form.reset();
return;
}
}
// else{}
const createdLi = document.createElement("li");
createdLi.classList.add("city");
const createdLiInnerHTML =
`<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="${iconUrl}">
<figcaption>${weather[0].description}</figcaption>
</figure>`;
createdLi.innerHTML = createdLiInnerHTML;
//append vs. prepend
list.prepend(createdLi);
}
catch (error) {
msg.innerText = error;
setTimeout(()=>{
msg.innerText = "";
}, 5000);
}
form.reset();
}