-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
61 lines (53 loc) · 1.77 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
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=96475ff8423fcf9c9366c6446a1d165a&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,
tempIcon: json.weather[0].icon,
description: json.weather[0].description,
windSpeed: json.wind.speed,
windAngle: json.wind.deg
})
} else {
clearInfo()
showWarning('Não encontramos a localização...')
}
}
})
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.tempIcon}@2x.png`
)
document.querySelector('.tempDescrition').innerHTML = `${json.description}`
document.querySelector('.ventoPonto').style.transform = `rotate(${
json.windAngle - 90
}.deg)`
document.querySelector('.resultado').style.display = 'block'
}
function showWarning(msg) {
document.querySelector('.aviso').innerHTML = msg
}
function clearInfo() {
showWarning('')
document.querySelector('.resultado').style.display = 'none'
}