-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
172 lines (160 loc) · 5.74 KB
/
app.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
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
// Required Variables
const app = document.querySelector('.wrapper');
const btn = document.querySelector('.city-btn');
const icon = document.querySelector('.fa-solid');
// Instantiate Storage object
const storage = new Storage();
// Get stored location data
const weatherLocation = storage.getCityFromLocalStorage();
// Instantiate Weather object
const weather = new Weather(weatherLocation);
// Instantiate UI object
const ui = new UI();
// Get weather on DOM Load
document.addEventListener("DOMContentLoaded", getWeather);
// Change location by clicking on Giving city
document.querySelector('.cities').addEventListener('click', (e) => {
if(e.target.tagName === 'LI'){
// Get city name
const cityName = e.target.textContent;
// Change city location
weather.changeLocation(cityName);
// Set city in local storage
storage.setCityInLocalStorage(cityName);
// Get and display weather
getWeather();
}
});
// Change location
document.getElementById('get-city').addEventListener('submit', (e) => {
// Get city name
const city = document.getElementById('city-name').value;
// Change city location
weather.changeLocation(city);
// Set city in local storage
storage.setCityInLocalStorage(city);
// Get and display weather
getWeather();
e.preventDefault();
});
// Get weather
function getWeather(){
weather.getWeather()
.then(data => {
ui.paint(data);
// Set default time of day
let timeOfDay = 'day';
// Get unique code for each weathe condition
const code = data.current.condition.code;
// Change to night if its night time in the city
if(!data.current.is_day){
timeOfDay = 'night';
}
// For clear weather
if(code === 1000){
// Set the background to be night or day depending on the weather condition
app.style.backgroundImage = `url(./img/${timeOfDay}/clear.jpg)`;
// Change button background to night or day depending on weather condition
btn.style.background = '#ababed';
if(timeOfDay === 'night'){
btn.style.background = '#4e555f';
icon.style.color = 'white';
}
}
// For cloudy weather conditions
else if(
code === 1003 ||
code === 1006 ||
code === 1009 ||
code === 1030 ||
code === 1069 ||
code === 1087 ||
code === 1135 ){
// Set the background to be night or day depending on the weather condition
app.style.backgroundImage = `url(./img/${timeOfDay}/cloudy.jpg)`;
// Change button background to night or day depending on weather condition
btn.style.background = '#f3d8dc';
if(timeOfDay === 'night'){
btn.style.background = '#a5a5ec';
icon.style.color = 'white';
}
}
// For rainy weather conditions
else if(
code === 1063 ||
code === 1072 ||
code === 1150 ||
code === 1153 ||
code === 1168 ||
code === 1171 ||
code === 1180 ||
code === 1183 ||
code === 1186 ||
code === 1189 ||
code === 1192 ||
code === 1195 ||
code === 1198 ||
code === 1201 ||
code === 1240 ||
code === 1243 ||
code === 1246
){
// Set the background to be night or day depending on the weather condition
app.style.backgroundImage = `url(./img/${timeOfDay}/rainy.jpg)`;
// Change button background to night or day depending on weather condition
btn.style.background = '#6d6c6c';
icon.style.color = 'white';
if(timeOfDay === 'night'){
btn.style.background = '#808080';
}
}
// For snowy weather condition
else if(
code === 1066 ||
code === 1114 ||
code === 1117 ||
code === 1147 ||
code === 1210 ||
code === 1213 ||
code === 1216 ||
code === 1219 ||
code === 1222 ||
code === 1225 ||
code === 1255 ||
code === 1255
){
// Set the background to be night or day depending on the weather condition
app.style.backgroundImage = `url(./img/${timeOfDay}/snowy.jpg)`;
// Change button background to night or day depending on weather condition
btn.style.background = '#c5c0c0';
icon.style.color = 'white';
}
// For thunderstorms
else if(
code === 1087 ||
code === 1273 ||
code === 1276 ||
code === 1279 ||
code === 1282
) {
// Set the background to be night or day depending on the weather condition
app.style.backgroundImage = `url(./img/${timeOfDay}/thunder.jpg)`;
// Change button background to night or day depending on weather condition
btn.style.background = '#4e555f';
icon.style.color = 'white';
}
else{
// Set the background to be night or day depending on the weather condition
app.style.backgroundImage = `url(./img/${timeOfDay}/cloudy.jpg)`;
// Change button background to night or day depending on weather condition
btn.style.background = '#f3d8dc';
if(timeOfDay === 'night'){
btn.style.background = '#a5a5ec';
icon.style.color = 'white';
}
}
})
.catch(() => {
ui.showAlert('City not found')
})
}