-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
104 lines (77 loc) · 2.85 KB
/
scripts.js
File metadata and controls
104 lines (77 loc) · 2.85 KB
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
const apiKey = "55ed653ac9766509d35ae5629c01a2d6";
const apiCountryURL = "https://countryflagsapi.com/png/";
const apiUnsplash = "https://source.unsplash.com/1600x900/?";
const cityInput = document.querySelector("#city-input");
const searchBtn = document.querySelector("#search");
const cityElement = document.querySelector("#city");
const tempElement = document.querySelector("#temperature span");
const descElement = document.querySelector("#description");
const weatherIconElement = document.querySelector("#weather-icon");
const countryElement = document.querySelector("#country");
const umidityElement = document.querySelector("#umidity span");
const windElement = document.querySelector("#wind span");
const weatherContainer = document.querySelector("#weather-data");
const errorMessageContainer = document.querySelector("#error-message");
const loader = document.querySelector("#loader");
const suggestionContainer = document.querySelector("#suggestions");
const suggestionButtons = document.querySelectorAll("#suggestions button");
// Loader
const toggleLoader = () => {
loader.classList.toggle("hide");
};
const getWeatherData = async (city) => {
toggleLoader();
const apiWeatherURL = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey}&lang=pt_br`;
const res = await fetch(apiWeatherURL);
const data = await res.json();
toggleLoader();
return data;
};
// Tratamento de erro
const showErrorMessage = () => {
errorMessageContainer.classList.remove("hide");
};
const hideInformation = () => {
errorMessageContainer.classList.add("hide");
weatherContainer.classList.add("hide");
suggestionContainer.classList.add("hide");
};
const showWeatherData = async (city) => {
hideInformation();
const data = await getWeatherData(city);
if (data.cod === "404") {
showErrorMessage();
return;
}
cityElement.innerText = data.name;
tempElement.innerText = parseInt(data.main.temp);
descElement.innerText = data.weather[0].description;
weatherIconElement.setAttribute(
"src",
`http://openweathermap.org/img/wn/${data.weather[0].icon}.png`
);
countryElement.setAttribute("src", apiCountryURL + data.sys.country);
umidityElement.innerText = `${data.main.humidity}%`;
windElement.innerText = `${data.wind.speed}km/h`;
// Change bg image
document.body.style.backgroundImage = `url("${apiUnsplash + city}")`;
weatherContainer.classList.remove("hide");
};
searchBtn.addEventListener("click", async (e) => {
e.preventDefault();
const city = cityInput.value;
showWeatherData(city);
});
cityInput.addEventListener("keyup", (e) => {
if (e.code === "Enter") {
const city = e.target.value;
showWeatherData(city);
}
});
// Sugestões
suggestionButtons.forEach((btn) => {
btn.addEventListener("click", () => {
const city = btn.getAttribute("id");
showWeatherData(city);
});
});