-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
70 lines (66 loc) · 2.38 KB
/
script.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
const urlBackend = "https://weatherapp-backend-six-phi.vercel.app";
fetch(`${urlBackend}/weather`)
.then((response) => response.json())
.then((data) => {
if (data.weather) {
for (let i = 0; i < data.weather.length; i++) {
document.querySelector("#cityList").innerHTML += `
<div class="cityContainer">
<p class="name">${data.weather[i].cityName}</p>
<p class="description">${data.weather[i].description}</p>
<img class="weatherIcon" src="images/${data.weather[i].main}.png"/>
<div class="temperature">
<p class="tempMin">${data.weather[i].tempMin}°C</p>
<span>-</span>
<p class="tempMax">${data.weather[i].tempMax}°C</p>
</div>
<button class="deleteCity" id="${data.weather[i].cityName}">Delete</button>
</div>
`;
}
updateDeleteCityEventListener();
}
});
function updateDeleteCityEventListener() {
for (let i = 0; i < document.querySelectorAll(".deleteCity").length; i++) {
document
.querySelectorAll(".deleteCity")
[i].addEventListener("click", function () {
fetch(`${urlBackend}/weather/${this.id}`, { method: "DELETE" })
.then((response) => response.json())
.then((data) => {
if (data.result) {
this.parentNode.remove();
}
});
});
}
}
document.querySelector("#addCity").addEventListener("click", function () {
const cityName = document.querySelector("#cityNameInput").value;
fetch(`${urlBackend}/weather`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ cityName }),
})
.then((response) => response.json())
.then((data) => {
if (data.result) {
document.querySelector("#cityList").innerHTML += `
<div class="cityContainer">
<p class="name">${data.weather.cityName}</p>
<p class="description">${data.weather.description}</p>
<img class="weatherIcon" src="images/${data.weather.main}.png"/>
<div class="temperature">
<p class="tempMin">${data.weather.tempMin}°C</p>
<span>-</span>
<p class="tempMax">${data.weather.tempMax}°C</p>
</div>
<button class="deleteCity" id="${data.weather.cityName}">Delete</button>
</div>
`;
updateDeleteCityEventListener();
document.querySelector("#cityNameInput").value = "";
}
});
});