-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
419 lines (215 loc) · 9.42 KB
/
Copy pathapp.js
File metadata and controls
419 lines (215 loc) · 9.42 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
const auth = "5d8d0f350c51c38d0174906526cc3a21";
const auth2 = "lnGbZdhXxlr7kaA3xc1DA6G7G8ep3szr";
// API Key
// Global variables
const container = document.querySelector('.widget-city');
const form = document.querySelector('.search-form');
const inputValue = document.querySelector('.search-input');
const submitBtn = document.querySelector('.submit-btn');
// Parameter values
const detailsContainer = document.querySelector('.weather-details');
const temperature = document.querySelector('.temp-standard');
const clouds = document.querySelector('.clouds');
const temperatureMin = document.querySelector('.temp-min');
const temperatureMax = document.querySelector('.temp-max');
const feelsLike = document.querySelector('.feels-like');
const countryName = document.querySelector('.country-name');
const wind = document.querySelector('.wind');
const humidity = document.querySelector('.humidity');
// Open slider
const openBtn = document.querySelector('.open-slide');
const widgetContainer = document.querySelector('.widget-container');
// Main widget parameters
const currentTemp = document.querySelector('.current-temperature');
const currentLoc = document.querySelector('.location');
const currentTime = document.querySelector('.current-time');
const weatherCond = document.querySelector('.weather-type');
const infoSection = document.querySelector('.info-section');
const savedLocations = document.querySelector('.saved-locations');
// Weather widget prediction
const futureData = document.querySelector('.days-forecast');
let savedValue;
// Rotation image
const imageRotation = document.querySelectorAll('.container-images');
// Event Listeners
inputValue.addEventListener('input', updateInput);
form.addEventListener('submit', (e) => {
detailsContainer.classList.add('active');
e.preventDefault();
displayData(savedValue);
// Next 5 days forecast data (3, 6, 9, 12, 15, 18, 21, 0)
nextDaysData(savedValue);
futureData.innerHTML = '';
});
// Open slider
openBtn.addEventListener('click', () => {
widgetContainer.classList.toggle('active');
openBtn.classList.toggle('active');
});
// Getting info from the server once we click on the city saved
// Function that takes the data from the api and convert data into a JSON Object
async function fetchApi(url){
const dataKey = await fetch(url, {
method: "GET",
headers: {}
})
const data = await dataKey.json();
return data;
}
// Displaying all the data in a widget
async function displayData(city){
// Fetch all data from the server for a specific city
// Current day
const fetchLink = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${auth}&units=metric`;
const dataFetch = await fetchApi(fetchLink);
// For every city object create a widget that takes the next data:
// Widget details: temp_max, temp_min, temp, clouds, hummidity, feels_like
generateWidgets(dataFetch);
widgetData(dataFetch);
saveToLocal(dataFetch.name, dataFetch);
}
async function nextDaysData(city){
const fetchLink = `https://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=${auth}&units=metric`;
const dataFetch = await fetchApi(fetchLink);
console.log(dataFetch);
futureForecast(dataFetch);
}
function generateWidgets(data){
// Current day forecast
// Name of the country
console.log(data);
countryName.innerHTML = data.name;
// Insert data automatically into the right parameter
// Temp
temperature.children[0].innerHTML = `${data.main.temp} °`;
// Temp_min
temperatureMin.children[0].innerHTML = `${data.main.temp_min} °`;
// Temp_max
temperatureMax.children[0].innerHTML = `${data.main.temp_max} °`;
// Feels_like
feelsLike.children[0].innerHTML = `${data.main.feels_like} °`;
// Clouds
clouds.children[0].innerHTML = `${data.clouds.all} %`;
// Hummidity
humidity.children[0].innerHTML = `${data.main.humidity} %`;
// Wind speed
wind.children[0].innerHTML = `${data.wind.speed} km/h`;
inputValue.value = '';
}
function widgetData(data){
// Current temp
currentTemp.children[0].innerHTML = `${data.main.temp} °`;
// Current location
currentLoc.children[0].innerHTML = data.name;
// Current date-time
// Obtain current city offset in seconds and convert it to miliseconds
// Obtain current timezone and convert it to miliseconds
// Convert the result to a readable format
const dataTime = new Date(data.dt * 1000);
const dataFormat = dataTime.toLocaleString();
currentTime.children[0].innerHTML = dataFormat;
// Current weather condition
// Fetch icons based on the weather condition
for(let i of data.weather){
weatherCond.innerHTML = `<img src='./icons/${i.icon}.gif'>
<p>${i.description}</p>`;
const description = i.description;
changeImages(description);
}
}
// Weather next days widget
function futureForecast(data){
// Display next day forecast
console.log(data);
data.list.forEach((day, index) => {
if(index > 0){
// Get the future dates from the api forecast
let dayname = new Date(day.dt * 1000);
// Get hours from the generated future data
const hours = dayname.getHours();
// Get the day
const dayNr = dayname.getDate();
// Get name of the day
const dayName = dayname.toLocaleDateString('en-US', {weekday: "long"});
// If hours are equal to the 3PM, display all the days that has the 3PM in the composition
// Unix needs to be converted into date and get only the hour
// If the hour is equal to our chosen hour
// Display only the days that equal that hour
if(hours === 14){
// Create one section of time display and one section of displaying the weather
// Container
console.log(dayname)
const date = document.createElement('div');
date.classList.add('date');
futureData.appendChild(date);
// Data display
const displayDay = document.createElement('h2');
displayDay.classList.add('day');
date.appendChild(displayDay);
displayDay.innerHTML = `${dayNr} ${dayName}`;
// Weather display
const weatherDisplay = document.createElement('div');
weatherDisplay.classList.add('weather-condtion');
date.appendChild(weatherDisplay);
// Weather temp
const temp = document.createElement('h2');
weatherDisplay.appendChild(temp);
temp.innerHTML = `${day.main.temp}°`
// Weather condition
const weatherCond = document.createElement('p');
// Loop over all images and select the one according to the weather conditions
for(let i of day.weather){
weatherCond.innerHTML = `<img class="weather-image" src='./icons/${i.icon}.gif'>`;
}
weatherDisplay.appendChild(weatherCond);
}
}
})
}
// Save local input value
// Function take the savedValue from the input
// When we submit the form, savedValue is saved in local storage
function saveToLocal(city, data){
// Check if there is a value in the storage
let save;
let index = data.id;
if(localStorage.getItem('locations') === null){
save = [];
}else {
save = JSON.parse(localStorage.getItem('locations'));
}
// Display only the new added items
// The rest need to stay on the location history
// Push a new value inside the array only if it doesn't have already similar values in it
if(save.indexOf(city) == -1){
//add the value to the array
save.push(city);
// Set items in the in the local storage array and converts the values into JSON strings
localStorage.setItem("locations",JSON.stringify(save));
}
}
// Take the value of the input
function updateInput(e){
savedValue = e.target.value;
}
// Function that checks the time and rotate images after a specific hour
function changeImages(name){
// Look over each case inside the description from the api and display specific images according to the weather conditions
switch(name){
case 'scattered clouds':
document.body.style.backgroundImage = "url('./images/scattered-clouds.jpg')";
break;
case 'clear sky':
document.body.style.backgroundImage = "url('./images/clear-sky.jpg')";
break;
case 'broken clouds':
document.body.style.backgroundImage = "url('./images/broken-clouds.jpg')";
break;
case 'overcast clouds':
document.body.style.backgroundImage = "url('./images/overcast-clouds.jpg')";
break;
case 'light rain':
document.body.style.backgroundImage = "url('./images/clear-sky.jpg')";
break;
}
}