-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
151 lines (136 loc) · 4.63 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
const inputBox = document.querySelector(".form-control");
const searchBtn = document.querySelector(".btn");
const errorMessage = document.querySelector("#error-message");
const screen = document.querySelector("#screen");
const weatherApp = async (name) => {
const apiKey = "26177b56931be80e12c0660121517f13";
const url = `https://api.openweathermap.org/data/2.5/weather?q=${name}&appid=${apiKey}`;
try {
const res = await fetch(url);
if (!res.ok) {
renderError();
throw new Error(`Something went wrong: ${res.status}`);
}
const data = await res.json();
renderWeather(data);
} catch (error) {}
};
let screenCardTotal = [];
searchBtn.addEventListener("click", () => {
if (screenCardTotal.includes(inputBox.value.toUpperCase())) {
counter = 0;
const intervalMessage = setInterval(() => {
errorMessage.innerHTML = `You already now the weather for ${inputBox.value.toUpperCase()}, please search for another`;
counter++;
if (counter > 10) {
clearInterval(intervalMessage);
errorMessage.innerHTML = "";
inputBox.value = "";
}
}, 200);
} else if (screenCardTotal.length < 6) {
if (!inputBox.value) {
counter = 0;
const intervalMessage = setInterval(() => {
errorMessage.innerHTML = `Please enter a city name...`;
counter++;
if (counter > 10) {
clearInterval(intervalMessage);
errorMessage.innerHTML = "";
}
}, 200);
return;
} else {
weatherApp(inputBox.value);
inputBox.value = "";
}
} else {
let question = confirm(
"Maximum city listed. Do you want to remove all items?"
);
if (question) {
inputBox.value = "";
screen.textContent = "";
screenCardTotal = [];
}
}
inputBox.focus();
});
screen.addEventListener("click", (e) => {
if (e.target.classList.contains("fa-x")) {
e.target.closest(".card").remove();
screenCardTotal.splice(
screenCardTotal.indexOf(
e.target.parentElement.parentElement.parentElement.children[0].textContent.toUpperCase()
),
1
);
console.log(screenCardTotal);
}
});
inputBox.addEventListener("keydown", (e) => {
if (e.code == "Enter") {
searchBtn.click();
}
});
window.addEventListener("load", () => {
inputBox.focus();
});
const renderError = () => {
counter = 0;
const intervalMessage = setInterval(() => {
errorMessage.innerHTML = `
<div class="text-center mb-2">Enter the city name exactly...</div>
<img src="./error.jpg" width="300px" alt="">
`;
counter++;
if (counter > 10) {
clearInterval(intervalMessage);
errorMessage.innerHTML = "";
}
}, 200);
};
const renderWeather = (weath) => {
const {
name,
sys: { country },
main: { temp, temp_max, temp_min, feels_like },
wind: { speed },
clouds: { all },
} = weath;
screenCardTotal.push(name.toUpperCase());
const screen = document.querySelector("#screen");
screen.innerHTML =
`
<div class="col-md-4 col-lg-3 card shadow-lg" style="width: 18rem;">
<ul class="list-group list-group-flush text-center">
<li class="list-group-item fs-5 fw-bold d-flex justify-content-between align-items-center text-uppercase"><div>${name}</div> <div><span class="text-danger text-end">${country} </span> <span><i class="fa-solid fa-x ms-1"></i></span></div></li>
<li class="list-group-item fw-bold"><div class=" display-1 fw-bold">${(
temp - 273.15
).toFixed()}℃ </div>
<i class="fa-solid fa-arrow-up text-danger fs-6"></i> H: ${(
temp_max - 273.15
).toFixed()}℃
<i class="fa-solid fa-arrow-down text-primary fs-6"></i> L: ${(
temp_min - 273.15
).toFixed()}℃
<div class="text-capitalize fw-bold opacity-75 fs-4 m-2">${
weath.weather[0].description
}</div>
</li>
<li class="list-group-item"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/162656/${
weath.weather[0].icon
}.svg" /></li>
<li class="list-group-item text-center fw-bold opacity-75 fs-6">
<i class="fa-solid fa-cloud text-black"></i> Clouds: %${all}
<br>
<i class="fa-solid fa-temperature-three-quarters text-secondary fs-6 text-success"></i> Feels Like: ${(
temp_min - 273.15
).toFixed()}℃
<br>
<i class="fa-solid fa-wind text-secondary"></i> Wind Speed: ${speed} Km/h
</li>
</ul>
</div>
` + screen.innerHTML;
};