Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
54 lines (42 loc) · 1.72 KB
/
Copy pathscript.js
File metadata and controls
54 lines (42 loc) · 1.72 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
document.querySelector('.busca').addEventListener('submit', async (event) => {
event.preventDefault();
let input = document.querySelector('#searchInput').value;
if (input !== '') {
clearInfo();
showWarning('Carregando...');
let url = `https://api.openweathermap.org/data/2.5/weather?q=${encodeURI(input)}&appid=0bbd89eccf96fb63ac3454c177587307&units=metric&lang=pt_br`;
let results = await fetch(url);
let json = await results.json();
if (json.cod === 200) {
showInfo({
name: json.name,
country: json.sys.country,
temp: json.main.temp,
temIcon: json.weather[0].icon,
windSpeed: json.wind.speed,
windAngle:json.wind.deg
});
} else {
clearInfo();
showWarning('Cidade não encontrada.');
}
} else {
clearInfo();
}
});
function showInfo(json) {
showWarning('');
document.querySelector('.titulo').innerHTML = `${json.name}, ${json.country}`;
document.querySelector('.tempInfo').innerHTML = `${json.temp} <sup>ºC</sup>`;
document.querySelector('.ventoInfo').innerHTML = `${json.windSpeed} <span>km/h</span>`;
document.querySelector('.temp img').setAttribute('src', `http://openweathermap.org/img/wn/${json.temIcon}@2x.png`)
document.querySelector('.ventoPonto').style.transform = `rotate(${json.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;
}