-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
59 lines (41 loc) · 2.09 KB
/
script.js
File metadata and controls
59 lines (41 loc) · 2.09 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
import {
searchButton, searchInput, APIKey, rainStats, windDirection, sunrise, sunset,
timeNow, circlePosition, voiceSearchButton, closeVoiceSearch
}
from './export.js'
import { getEnterClick, getUserLocation, temperature, airQuality, weekWeather, sunPosition, errorAlert, voiceSearch } from './public/index.js'
// Calling the function that gets Enter key press to search a city input
getEnterClick(searchInput, searchButton)
// Calling the function to get user location
getUserLocation(APIKey, searchButton)
// Getting click or voice to search
voiceSearchButton.addEventListener('click', () => { voiceSearch(searchButton, voiceSearchButton, closeVoiceSearch, searchInput) })
searchButton.addEventListener('click', () => {
const city = document.querySelector('#search-input').value
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${APIKey}&lang=pt_br`)
.then(res => res.json())
.then(json => {
// Getting DT, timezone, sunrise, sunset, latitude and longitude
const getDT = json.dt
const getTimezone = json.timezone
const getSunrise = json.sys.sunrise
const getSunset = json.sys.sunset
const latitude = json.coord.lat
const longitude = json.coord.lon
windDirection.style.rotate = `${json.wind.deg}deg`
// Calling the function to get temperature, humidity, wind speed and rain probability
temperature(json)
// Calling the function to set the sun position
sunPosition(sunrise, sunset, timeNow, circlePosition, getDT, getTimezone, getSunrise, getSunset)
// Calling the function to get the air quality
airQuality(latitude, longitude, APIKey)
// Adding data in the week weather section
weekWeather(city, rainStats, APIKey)
})
.catch(
() => {
errorAlert(city)
console.log(`Não foi possível localizar: ${city}`)
}
)
})