-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
73 lines (56 loc) · 2.16 KB
/
app.js
File metadata and controls
73 lines (56 loc) · 2.16 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
const cityForm = document.querySelector('form');
const card = document.querySelector('.card');
const details = document.querySelector('.details')
const time = document.querySelector('.time');
const icon = document.querySelector('.icon img')
const updateUI = data => {
//getting particular value Note:Its a local variable, dont be confused
// const cityDetails = data.cityDetails;
// const weatherDetails = data.weatherDetails;
//Just to show some koolness, using Destructure proporties
const {cityDetails,weatherDetails} = data;
//update templates
details.innerHTML = `<h5 class="my-3">${cityDetails.EnglishName}</h5>
<div class="my-3">${weatherDetails.WeatherText}</div>
<div class="display-4 my-4">
<span>${weatherDetails.Temperature.Metric.Value}</span>
<span>°C</span>
</div>`;
//Update Icon
const iconsrc = `img/icons/${weatherDetails.WeatherIcon}.svg`;
icon.setAttribute('src',iconsrc);
//Update Image
let imagesrc = weatherDetails.IsDayTime ? 'img/day.svg' : 'img/night.svg'
time.setAttribute('src', imagesrc)
//remove d-none class
if(card.classList.contains('d-none')){
card.classList.remove('d-none');
}
}
const updateCity = async city => {
//cityDetails
const cityDetails = await getCity(city);
//weatherDetails
const weatherDetails = await getWeather(cityDetails.Key)
//return Promises
return{cityDetails,weatherDetails}
}
cityForm.addEventListener('submit',e => {
//prevent default actions on form
e.preventDefault();
//Get City
const city = e.target.city.value;
e.target.reset();
//Update date with new submission
updateCity(city).then(data=> {
//Update UI after getting data
updateUI(data)
//If you start judging my code, i will start judging your pp size.:)
}).catch(err => console.log(err))
//Setting Local Storage
localStorage.setItem('city',city)
})
//If there is data in local storage, fetch it and display
if(localStorage.getItem('city')){
updateCity(localStorage.getItem('city')).then(data => updateUI(data)).catch(err=>console.log(err))
}