-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
148 lines (119 loc) · 4.79 KB
/
Copy pathscript.js
File metadata and controls
148 lines (119 loc) · 4.79 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const cityInput = document.querySelector('.city-input')
const searchBtn = document.querySelector('.search-btn')
const weatherInfoSection = document.querySelector('.weather-info')
const notFoundSection = document.querySelector('.not-found')
const searchCitySection = document.querySelector('.search-city')
const countryTxt = document.querySelector('.country-txt')
const tempTxt = document.querySelector('.temp-txt')
const conditionTxt = document.querySelector('.condition-txt')
const humidityValueTxt = document.querySelector('.humidity-value-txt')
const windValueTxt = document.querySelector('.wind-value-txt')
const feelsLikeTxt = document.querySelector('.feels-like-txt')
const pressureTxt = document.querySelector('.pressure-value-txt')
const weatherSummaryImg = document.querySelector('.weather-summary-img')
const currentDateTxt = document.querySelector('.current-date-txt')
const forecastItemsContainer = document.querySelector('.forecast-items-container')
const apiKey = 'ee38f4ab15ec45dda3067b6d1180a34d'
searchBtn.addEventListener('click', () => {
if (cityInput.value.trim() != '') {
updateWeatherInfo(cityInput.value)
cityInput.value = ''
cityInput.blur()
}
})
cityInput.addEventListener('keydown', (event) => {
if (event.key == 'Enter' && cityInput.value.trim() != '') {
updateWeatherInfo(cityInput.value)
cityInput.value = ''
cityInput.blur()
}
})
async function getFetchData(endPoint, city) {
const apiUrl = `https://api.openweathermap.org/data/2.5/${endPoint}?q=${city}&appid=${apiKey}&units=metric&lang=es`
const response = await fetch(apiUrl)
return response.json()
}
function getWeatherIcon(id) {
if (id <= 232) return `thunderstorm.svg`
if (id <= 321) return `drizzle.svg`
if (id <= 531) return `rain.svg`
if (id <= 622) return `snow.svg`
if (id <= 781) return `atmosphere.svg`
if (id <= 800) return `clear.svg`
else return `clouds.svg`
}
function getCurrentDate() {
const currentDate = new Date()
// console.log(currentDate)
const options = {
weekday: 'short',
day: '2-digit',
month: 'short',
}
return currentDate.toLocaleDateString('es-ES', options)
// return currentDate.toLocaleDateString('en-GB', options)
}
async function updateWeatherInfo(city) {
const weatherData = await getFetchData('weather', city)
if (weatherData.cod != 200) {
showDisplaySection(notFoundSection)
return
}
const {
name: cityName,
sys: { country: country},
main: { temp, humidity, feels_like, pressure },
weather: [{ id, main }],
wind: { speed }
} = weatherData
// countryTxt.textContent = `${cityName}, ${country}`
countryTxt.textContent = cityName + ', ' + country
tempTxt.textContent = Math.round(temp) + ' °C'
conditionTxt.textContent = main
humidityValueTxt.textContent = humidity + '%'
feelsLikeTxt.textContent = `Sensación: ${Math.floor(feels_like)} °C`
pressureTxt.textContent = pressure + ' hPa'
windValueTxt.textContent = speed + ' m/s'
currentDateTxt.textContent = getCurrentDate()
weatherSummaryImg.src = `assets/weather/${getWeatherIcon(id)}`
await updateForecastsInfo(city)
showDisplaySection(weatherInfoSection)
}
async function updateForecastsInfo(city) {
const forecastsData = await getFetchData('forecast', city)
const timeTaken = '12:00:00'
const todayDate = new Date().toISOString().split('T')[0]
forecastItemsContainer.innerHTML = ''
forecastsData.list.forEach(forecastWeather => {
if (forecastWeather.dt_txt.includes(timeTaken) && !forecastWeather.dt_txt.includes(todayDate)) {
updateForecastsItems(forecastWeather)
}
})
}
function updateForecastsItems(weatherData) {
console.log(weatherData)
const {
dt_txt: date,
weather: [{ id }],
main: { temp }
} = weatherData
const dateTaken = new Date(date)
const dateOption = {
day: '2-digit',
month: 'short'
}
const dateResult = dateTaken.toLocaleDateString('en-us', dateOption)
const forecastItem = `
<div class="forecast-item">
<h5 class="forecast-item-date regular-txt">${dateResult}</h5>
<img src="assets/weather/${getWeatherIcon(id)}" class="forecast-item-img">
<h5 class="forecast-item-temp">${Math.round(temp)} °C</h5>
</div>
`
forecastItemsContainer.insertAdjacentHTML('beforeend', forecastItem)
}
function showDisplaySection(section) {
[weatherInfoSection, searchCitySection, notFoundSection]
.forEach(section => section.style.display = 'none')
section.style.display = 'flex'
}