-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
55 lines (43 loc) · 1.71 KB
/
script.js
File metadata and controls
55 lines (43 loc) · 1.71 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
document.querySelector('.busca').addEventListener('submit', async (event) => {
event.preventDefault();
let input = document.querySelector('#searchInput').value;
if(input !== '') {
clearInfo();
showWarning('Carregando...');
let results = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=
${encodeURI(input)}&units=metric&lang=pt_br&appid=5023f6bf05e2c126ab67602f2e18961d`);
let json = await results.json();
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
});
} else {
clearInfo();
showWarning('Não encontramos esta localização.');
}
} else {
clearInfo();
}
});
function showInfo(obj) {
showWarning('');
document.querySelector('.titulo').innerHTML = `${obj.name}, ${obj.country}`;
document.querySelector('.tempInfo').innerHTML = `${obj.temp} <sup>ºC</sup>`;
document.querySelector('.ventoInfo').innerHTML = `${obj.windSpeed} <span>km/h</span>`;
document.querySelector('.temp img').setAttribute('src',
`http://openweathermap.org/img/wn/${obj.tempIcon}@2x.png`);
document.querySelector('.ventoPonto').style.transform = `rotate(${obj.windAngle-90}deg)`;
document.querySelector('.resultado').style.display = 'block';
}
function clearInfo() {
showWarning('');
document.querySelector('.resultado').style.display = 'none';
}
function showWarning(msg) {
document.querySelector('.aviso').innerHTML = msg;
}