-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
102 lines (89 loc) · 3.37 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
const searchField = document.querySelector('#search-text');
const searchBtn = document.querySelector('.search-btn');
const cityName = document.querySelector('.bold');
const temp = document.querySelector('.temp');
const description = document.querySelector('.desc');
const windStat = document.querySelector('.wind-stat');
const humidityStat = document.querySelector('.humidity-stat');
const cloudStat = document.querySelector('.cloud-stat');
const loader = document.querySelector('.loader-container');
const weatherContainer = document.querySelector('.weather-data-container');
const errorPage = document.querySelector('.error-container');
const currWeather = document.querySelector('.weather-condition');
const homePage = document.querySelector('.home-page');
const APIkey = '4f712e2d8f10efcb77274bc8ece460b6';
homePage.classList.add('home-active');
searchBtn.addEventListener('click', ()=>{
let location = searchField.value;
if(location == ""){
homePage.classList.add('home-active');
}
else{
weatherContainer.classList.remove('active');
errorPage.classList.remove('error-active');
homePage.classList.remove('home-active');
loader.classList.add('active-loader');
searchLocationWeather(location);
}
})
searchField.addEventListener('keydown', (event)=>{
homePage.classList.remove('home-active');
if(event.key === 'Enter'){
console.log('pressed enter');
let location = searchField.value;
if(location == ""){
homePage.classList.add('home-active');
}
else{
weatherContainer.classList.remove('active');
errorPage.classList.remove('error-active');
loader.classList.add('active-loader');
searchLocationWeather(location);
}
}
})
async function searchLocationWeather(location){
try {
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${location}&appid=${APIkey}&units=metric`);
const result = await response.json();
if(result.message == 'city not found'){
loader.classList.remove('active-loader');
errorPage.classList.add('error-active');
}
else{
loader.classList.remove('active-loader');
errorPage.classList.remove('error-active');
renderWeatherData(result);
weatherContainer.classList.add('active');
}
}
catch (error) {
errorPage.classList.add('error-active');
loader.classList.remove('active-loader');
}
}
function renderWeatherData(data){
cityName.innerText = data?.name + '.';
temp.innerHTML = data?.main?.temp + `°C`;
description.innerText = data?.weather?.[0]?.description;
let value = data?.weather?.[0]?.main;
if(value == 'Clouds'){
currWeather.src = 'assets/cloudy.png';
}
else if(value == 'Clear'){
currWeather.src = 'assets/clear.png';
}
else if(value == 'Atmosphere'){
currWeather.src = 'assets/atmosphere.png';
}
else if(value == 'Snow'){
currWeather.src = 'assets/snow.png';
}
else if(value == 'Rain'){
currWeather.src = 'assets/rain.png';
}
let wind = data?.wind?.speed * 3.6;
windStat.innerText = wind.toFixed(2) + ' km/h';
humidityStat.innerText = data?.main?.humidity + '%';
cloudStat.innerText = data?.clouds?.all + '%';
}