-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (44 loc) · 1.6 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
const form = document.querySelector('form');
const details = document.querySelector('.details');
const card = document.querySelector('.card')
const time = document.querySelector('.time');
const icon = document.querySelector('.icon img');
const weather = new Weather();
const updateUI = async (data) => {
const {cityDetails, weatherDetails} = data;
const weatherTemplate = `
<h5 class="my-3">${cityDetails.EnglishName}</h5>
<div>${weatherDetails.WeatherText}</div>
<div class="display-4 my-3">
<span>${weatherDetails.Temperature.Metric.Value}</span>
<span>°C</span>
</div>
`;
details.innerHTML = weatherTemplate;
const timeImage = (weatherDetails.IsDayTime)? "asserts/day.svg" : "asserts/night.svg";
time.setAttribute("src", timeImage);
const iconImage = `asserts/icons/${weatherDetails.WeatherIcon}.svg`;
icon.setAttribute("src", iconImage);
if (card.classList.contains('d-none')) {
card.classList.remove('d-none');
}
};
form.addEventListener('submit', (event) => {
event.preventDefault();
const city = form.city.value.trim();
form.reset();
weather.getWeatherAndCityDeatils(city).then((data) => {
updateUI(data);
}).catch((error) => {
console.log("Error is: ", error);
});
localStorage.setItem('city', city);
});
const localStorageCity = localStorage.getItem('city');
if (localStorageCity !== null) {
weather.getWeatherAndCityDeatils(localStorageCity).then((data) => {
updateUI(data);
}).catch((error) => {
console.log("Error is: ", error);
});
}