-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (62 loc) · 2.14 KB
/
index.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
const submit = document.getElementById('submit');
const description = document.getElementById('description');
const temperature = document.getElementById('temperature');
const weatherIcon = document.getElementById('weatherIcon');
const inputCity = document.getElementById('inputCity');
const placeImg = document.getElementById('placeImg');
submit.addEventListener('click', main);
function main() {
let description;
let lat;
let lng;
let temperature;
let icon;
let image;
let country;
let cityName;
fetchGeonames().then((data) => {
lat = data.geonames[0].lat;
lng = data.geonames[0].lng;
country = data.geonames[0].countryName;
cityName = data.geonames[0].name;
fetchWeather(lat, lng).then((data) => {
description = data.data[0].weather.description;
temperature = data.data[0].temp;
icon = data.data[0].weather.icon;
fetchPixabay(country).then((data) => {
image = data.hits[0].largeImageURL;
render(description, temperature, icon, image, cityName);
});
});
});
}
function fetchGeonames() {
return fetch(`/fetchGeonames?city=${encodeURIComponent(inputCity.value)}`)
.then((response) => response.json())
.then((data) => {
return data;
})
.catch((error) => console.log(error));
}
function fetchWeather(latitude, longitude) {
return fetch(`/fetchWeather?lat=${latitude}&lon=${longitude}`)
.then((response) => response.json())
.then((data) => {
return data;
})
.catch((error) => console.log(error));
}
function fetchPixabay(country) {
return fetch(`/fetchPixabay?country=${encodeURIComponent(country)}`)
.then((response) => response.json())
.then((data) => {
return data;
})
.catch((error) => console.log(error));
}
function render(des, temp, icon, image, cityName) {
description.innerHTML = des + " at " + cityName;
temperature.innerHTML = `${temp}°C`;
weatherIcon.src = `/iconosWeather/${icon}.svg`;
placeImg.style.backgroundImage = `url(${image})`;
}