-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
96 lines (73 loc) · 2.87 KB
/
Copy pathscripts.js
File metadata and controls
96 lines (73 loc) · 2.87 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
const api_key = '071e2fa4010c990746e335e6441c27e5'
const api_country_url = "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 humidityElement = document.querySelector("#humidity 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");
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=${api_key}&lang=pt_br`
const res = await fetch(apiWeatherUrl)
const data = await res.json()
toggleLoader();
return data
}
const showErrorMessage = () => {
errorMessageContainer.classList.remove("hide");
};
const hideInformation = () => {
errorMessageContainer.classList.add("hide");
weatherContainer.classList.add("hide");
suggestionContainer.classList.add("hide");
};
const changeBg = async (city) =>{
document.body.style.backgroundImage = `url("${apiUnsplash + city}")`;
}
const showWeatherData = async (city)=>{
hideInformation();
const data = await getWeatherData(city);
await changeBg(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", api_country_url + data.sys.country)
humidityElement.innerText = `${data.main.humidity}%`
windElement.innerText = `${data.wind.speed}km/h`
weatherContainer.classList.remove("hide")
}
searchBtn.addEventListener("click", (e)=>{
e.preventDefault()
const city = cityInput.value
showWeatherData(city)
})
cityInput.addEventListener("keyup",(e)=>{
if(e.code == "Enter"){
const city = e.target.value
showWeatherData(city)
}
})
suggestionButtons.forEach((btn) => {
btn.addEventListener("click", () => {
const city = btn.getAttribute("id");
showWeatherData(city);
});
});