forked from Ajil5467/weatherapp.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapptest.js
More file actions
99 lines (68 loc) · 2.14 KB
/
Copy pathapptest.js
File metadata and controls
99 lines (68 loc) · 2.14 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
const key = ' Ug1G2aI458tEK0GfNZEBl4aCUUVsKiKC';
// get weather information
const getWeather = async (id) => {
const base ='http://dataservice.accuweather.com/currentconditions/v1/';
const query =`${id}?apikey=${key}`;
const response = await fetch(base + query);
const data = await response.json();
return data[0];
};
// get city information
const getCity = async (city) => {
const base = 'http://dataservice.accuweather.com/locations/v1/cities/search';
const query = `?apikey=${key}&q=${city}`;
const response = await fetch(base + query);
const data = await response.json();
return data[0];
};
// getCity('mumbai').then(data => {
// return getWeather(data.Key)
// }).then(data =>{
// console.log(data);
// }).catch(err => console.log(err));
const cityForm = document.querySelector('form');
const card = document.querySelector('.card');
const details = document.querySelector('.details');
const time = document.querySelector('img.time');
const icon = document.querySelector('.icon img');
const updateUI = (data) => {
const cityDets = data.cityDets;
const weather = data.weather;
//update deatails
details.innerHTML = `
<h5 class="my-3">${cityDets.EnglishName}</h5>
<div class="my-3">${weather.WeatherText}</div>
<div class="display-4 my-4">
<span>${weather.Temperature.Metric.Value}</span>
<span>°C</span>
</div>
`;
//update night/day icon images
let timeSrc = null;
if(weather.IsDayTime){
timeSrc = 'https://i.imgur.com/MbcC6II.jpg';
}else {
timeSrc = 'https://i.imgur.com/PKf8NXh.jpg';
}
time.setAttribute('src',timeSrc);
//remove the d-none class if neccessary
};
const updateCity = async (city) => {
const cityDets = await getCity(city);
const weather = await getWeather(cityDets.Key);
return {
cityDets,
weather
};
};
cityForm.addEventListener('submit', e => {
// prevent deafault action
e.preventDefault();
//get city value
const city = cityForm.city.value.trim();
cityForm.reset();
//update the ui with new city
updateCity(city)
.then(data => updateUI(data))
.catch(err => console.log(err));
});