-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.js
More file actions
189 lines (139 loc) · 5.9 KB
/
main.js
File metadata and controls
189 lines (139 loc) · 5.9 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// || TOGGLE HAMBURGER BAR
const hamburger = document.querySelector('.hamburger');
const slidebar = document.querySelector('.slidebar');
hamburger.addEventListener('click', () => {
hamburger.classList.toggle('active');
slidebar.classList.toggle('active');
})
// || copyrights
const copyright = document.getElementById('copyright');
copyright.innerHTML = new Date().getFullYear();
// || MAIN WEATHER FUNCTIONALITY
// Navigation
var city = document.getElementById('city');
var country = document.getElementById('country');
var searchCity = document.getElementById('search');
// box-1
var cityTemp = document.getElementById('temp');
var weatherIcon = document.getElementById('weather-icon');
var weatherDescription = document.getElementById('description');
var weatherPressure = document.getElementById('pressure');
var weatherVisibilty = document.getElementById('visibility');
var weatherHumidity = document.getElementById('humidity');
// box-2
var sunriseTime = document.getElementById('sunrise-time');
var sunsetTime = document.getElementById('sunset-time');
var uviRays = document.getElementById('uvi-rays');
var uviConcernLevel = document.querySelector('.uvi-level');
var uviConcernLevel2 = document.querySelector('.uvi-level2');
// Hours report
var hoursIcon = document.querySelectorAll('.hourly-icon');
var hoursTemp = document.querySelectorAll('.hours-temp');
// Days temperature
var daysIcon = document.querySelectorAll('.days-icon');
var nextDay = document.querySelectorAll('.prediction-day');
var predictionDesc = document.querySelectorAll('.prediction-desc');
var daysTemp = document.querySelectorAll('.days-temp');
// Time and dates
var currentTime = document.querySelector('.time');
var currentDate = document.querySelector('.date')
var aqi = document.querySelector('.aqi');
// || GLOBAL VARIABLES
var weatherApi;
var responseData;
var monthName = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
var weekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
// || FUNCTION FOR GET WEATHER REPORT
async function weatherReport(searchCity) {
weatherApi = await fetch(`https://api.weatherapi.com/v1/forecast.json?key=da2103b2c4ce4f95af051626232503&q=${searchCity}&days=7&aqi=yes&alerts=no`);
responseData = await weatherApi.json();
todayWeatherReport();
console.log(responseData);
// Hours
hoursWeatherReport();
// Days
forecastdayReport()
}
// || By default city
weatherReport('New Delhi');
function todayWeatherReport() {
city.innerHTML = responseData.location.name;
country.innerHTML = ' <i class="fa-sharp fa-solid fa-location-dot"></i>' + responseData.location.country;
// Box-1
cityTemp.innerHTML = responseData.current.temp_c;
weatherDescription.innerHTML = responseData.current.condition.text;
weatherIcon.setAttribute('src', responseData.current.condition.icon);
weatherPressure.innerHTML = responseData.current.pressure_mb + 'mb'
weatherVisibilty.innerHTML = responseData.current.vis_km + ' km'
weatherHumidity.innerHTML = responseData.current.humidity + '%'
// Box-2
sunriseTime.innerHTML = responseData.forecast.forecastday[0].astro.sunrise;
sunsetTime.innerHTML = responseData.forecast.forecastday[0].astro.sunset;
uviRays.innerHTML = responseData.current.uv + ' UVI';
aqi.innerHTML = Math.round(responseData.current.air_quality.pm2_5)
checkUVraysIndex();
time()
}
// || Functions for do some task
function checkUVraysIndex() {
let uviLevel = Number.parseInt(uviRays.textContent);
if (uviLevel <= 2) {
checkUviValue('Good', '#6ae17c');
}
else if (uviLevel <= 5) {
checkUviValue('Moderate', '#CCE16A');
}
else if (uviLevel <= 7) {
checkUviValue('High', '#d4b814');
}
else if (uviLevel <= 10) {
checkUviValue('Very high', '#d43114');
}
else {
checkUviValue('Etreme high', '#dc15cf');
}
}
function checkUviValue(level, color) {
uviConcernLevel.innerHTML = level;
uviConcernLevel.style.backgroundColor = color;
uviConcernLevel2.innerHTML = level;
}
// || Hours
function hoursWeatherReport() {
hoursTemp.forEach((t, i) => {
t.innerHTML = responseData.forecast.forecastday[0].hour[i].temp_c;
})
hoursIcon.forEach((t, i) => {
t.src = responseData.forecast.forecastday[0].hour[i].condition.icon;
})
}
// Days
function forecastdayReport() {
daysIcon.forEach((icon, index) => {
icon.src = responseData.forecast.forecastday[index].day.condition.icon
})
daysTemp.forEach((temp, index) => {
temp.innerHTML = Math.round(responseData.forecast.forecastday[index].day.maxtemp_c) + '°c' + `<span> / </span>` + Math.round(responseData.forecast.forecastday[index].day.mintemp_c) + '°c';
})
predictionDesc.forEach((d, index) => {
d.innerHTML = responseData.forecast.forecastday[index].day.condition.text;
})
nextDay.forEach((day, index) => {
let weekdate = new Date(responseData.forecast.forecastday[index].date).getDate();
let weekday = weekDays[new Date(responseData.forecast.forecastday[index].date).getDay()];
day.innerHTML = `${weekday} ${weekdate}`
})
}
function time() {
var timezone = responseData.location?.tz_id;;
var now = new Date().toLocaleTimeString('en-US', { timeZone: timezone });
currentTime.innerHTML = now;
var today = new Date(responseData.forecast.forecastday[0].date);
currentDate.innerHTML = `${today.getDate()} ${monthName[today.getMonth()]} ${today.getFullYear()}, ${weekDays[today.getDay()]}`
}
setInterval(() => {
time();
}, 1000)
searchCity.addEventListener('keydown', () => {
weatherReport(searchCity.value)
})