-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
140 lines (113 loc) · 4.3 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
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
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 humadityValueTxt = document.querySelector('.humidity-value-txt')
const windValueTxt = document.querySelector('.wind-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 = '96e2bb7f544f2cfc4633ea5e53933fbe'
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`
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()
const options = {
weekday: 'short',
day: '2-digit',
month: 'short'
}
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: country,
main: { temp, humidity },
weather: [{ id, main }],
wind: { speed }
} = weatherData
countryTxt.textContent = country
tempTxt.textContent = Math.round(temp) + ' °C'
conditionTxt.textContent = main
humadityValueTxt.textContent = humidity + '%'
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)) {
updateForecastItems(forecastWeather)
}
})
}
function updateForecastItems(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'
}