-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
97 lines (85 loc) · 4.03 KB
/
main.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
/*Calling all needed sections on HTML*/
const cssRoot = document.querySelector(':root');
const mainInfoSection = document.querySelector('.main-info');
const errorInfoSection = document.querySelector('.error-info');
const extraInfoSection = document.querySelector('.extra-info');
/*Calling all needed elements*/
const searchBar = document.querySelector('.search-input');
const searchButton = document.querySelector('.search-btn');
const cityName = document.querySelector('.city');
const tempC = document.querySelector('.temp-c');
const tempF = document.querySelector('.temp-f');
const weatherCondition = document.querySelector('.condition');
const windInfo = document.querySelector('.wind-info');
const humidityInfo = document.querySelector('.humidity-percent');
const weatherImg = document.querySelector('.temp-box img');
searchButton.addEventListener('click', async () => {
let city = searchBar.value;
if (city === '') {
mainInfoSection.classList.add('hidden');
errorInfoSection.classList.add('hidden');
extraInfoSection.classList.add('hidden');
cssRoot.style.setProperty('--color-one', '#526172');
cssRoot.style.setProperty('--color-one-dark', '#2C3E50');
cssRoot.style.setProperty(' --color-two', '#f5f5f5');
return;
}
else {
const options = {
method: 'GET',
headers: {
'X-RapidAPI-Key': '***',
'X-RapidAPI-Host': 'weatherapi-com.p.rapidapi.com'
}
};
fetch(`https://weatherapi-com.p.rapidapi.com/current.json?q=${city}`, options)
.then(response => response.json()).then(json => {
try {
mainInfoSection.classList.remove('hidden');
errorInfoSection.classList.add('hidden');
extraInfoSection.classList.remove('hidden');
cityName.textContent = json.location.name + ", " + json.location.country;
tempC.textContent = Math.round(json.current.temp_c);
tempF.textContent = Math.round(json.current.temp_f);
weatherCondition.textContent = json.current.condition.text;
windInfo.textContent = json.current.wind_kph + "Km/h";
humidityInfo.textContent = json.current.humidity + "%";
weatherImg.src = json.current.condition.icon;
console.log(json);
if (json.current.is_day == '1') {
if (json.current.condition.text == 'Moderate or heavy rain with thunder' ||
json.current.condition.text == 'Light Rain') {
cssRoot.style.setProperty('--color-one', '#81AFCE');
cssRoot.style.setProperty('--color-one-dark', '#2E86C1');
}
else if (json.current.condition.text
== 'Sunny') {
cssRoot.style.setProperty('--color-one', '#7CC2F1');
cssRoot.style.setProperty('--color-one-dark', '#2E86C1');
}
else if (json.current.condition.text
== 'Partly cloudy') {
cssRoot.style.setProperty('--color-one', '#7ABBE6');
cssRoot.style.setProperty('--color-one-dark', '#2E86C1');
}
else {
cssRoot.style.setProperty('--color-one', '#5499C7');
cssRoot.style.setProperty('--color-one-dark', '#2E86C1');
}
}
else if (json.current.is_day == '0'){
cssRoot.style.setProperty('--color-one', '#1F618D');
cssRoot.style.setProperty('--color-one-dark', '#154360');
}
} catch (error) {
console.log("Response status : " + json.error.code);
mainInfoSection.classList.add('hidden');
errorInfoSection.classList.remove('hidden');
extraInfoSection.classList.add('hidden');
cssRoot.style.setProperty('--color-one', '#526172');
cssRoot.style.setProperty('--color-one-dark', '#2C3E50');
cssRoot.style.setProperty(' --color-two', '#f5f5f5');
}
});
}
});