-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
72 lines (60 loc) · 2.72 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
71
72
let now = new Date();
let day = ["Sundat", 'Monday', "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
let mes = ["January", "February", "March","April", "May", "June", "July", "August", "September", "October", "November", "December"];
let dayAt = day[now.getDay()];
let mesAt = mes[now.getMonth()];
let dayDomes = now.getDate();
document.querySelector(".busca").addEventListener('submit', async (event) => {
event.preventDefault();
let imput = document.querySelector('#searchInput').value;
if (imput !== "") {
clearInfo();
showWarning('Loading');
let url = `https://api.openweathermap.org/data/2.5/weather?q=${encodeURI(imput)}&appid=e39e7e6a3977b4fe61d0d8d978afbfe0&units=metric`;
let resuts = await fetch(url);
let json = await resuts.json();
console.log(url);
if (json.cod === 200) {
showInfo({
name: json.name,
country: json.sys.country,
temp: json.main.temp,
tempIcon: json.weather[0].icon,
windSpeed: json.wind.speed,
windAngle: json.wind.deg,
party: json.weather[0].description,
sens: json.main.feels_like,
min: json.main.temp_min,
max: json.main.temp_max,
hum: json.main.humidity
});
} else {
clearInfo();
showWarning('Não foi possível localizar a cidade informada.');
}
} else {
clearInfo();
}
});
function clearInfo() {
showWarning('');
document.querySelector(".result").style.display = "none";
}
function showInfo(json) {
showWarning('');
document.querySelector('.titulo').innerHTML = `${json.name}, ${json.country}`;
document.querySelector('.tempInfo').innerHTML = `${Math.round(json.temp)} <sup>ºC<sup>`;
document.querySelector('.ventoInfo').innerHTML = `${json.windSpeed} <span>Km/h<span>`;
document.querySelector('.dvl img').setAttribute('src', `http://openweathermap.org/img/wn/${json.tempIcon}@2x.png`);
document.querySelector(".sensasao-termica").innerHTML = `Feels like: ${Math.round(json.sens)}º`;
document.querySelector("#min").innerHTML = `Min ${Math.round(json.min)}º`;
document.querySelector("#max").innerHTML = `Max ${Math.round(json.max)}º`;
document.querySelector(".hu").innerHTML = `${Math.round(json.hum)}%`;
document.querySelector(".month").innerHTML =`${dayDomes}th, ${mesAt}`;
document.querySelector('.pc').innerHTML = `${json.party}`;
document.querySelector('.d').innerHTML = dayAt;
document.querySelector(".result").style.display = "block";
};
function showWarning(aviso) {
document.querySelector('.aviso').innerHTML = aviso;
}