-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather-page.js
98 lines (78 loc) · 2.87 KB
/
weather-page.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
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
async function getData (city) {
let apiUrl = '';
if (!city) {
apiUrl = `https://api.openweathermap.org/data/2.5/weather?&units=metric&q=kolkata`;
}
else {
apiUrl = `https://api.openweathermap.org/data/2.5/weather?&units=metric&q=${city}`;
}
const apiKeys = "082fadc58f06877f50c19190d7d39a5e";
const response = await fetch(apiUrl + `&appid=${apiKeys}`);
const data = await response.json();
console.log(data)
displayData(data);
};
function displayData(data) {
if (data.cod == "404") {
document.querySelector(".hidden-message")
.classList.add("not-hidden-message");
setTimeout(() => {
document.querySelector(".hidden-message")
.classList.remove("not-hidden-message");
}, 2000)
}
else {
document.querySelector(".js-temperature")
.innerText = `${Math.round(data.main.temp)}`;
document.querySelector(".js-location")
.innerText = data.name;
document.querySelector(".js-humidity-amount")
.innerText = data.main.humidity + '%';
document.querySelector(".js-wind-speed")
.innerText = Math.round(data.wind.speed) + ' km/h'
weatherImage(data);
}
};
document.querySelector(".js-search")
.addEventListener("click", () => {
processData();
})
document.querySelector(".js-search-input")
.addEventListener("keydown", () => {
if(event.key == "Enter") {
processData();
}
})
function processData() {
let city = document.querySelector(".js-search-input");
console.log(city);
getData(city.value);
city.value = '';
}
getData();
function weatherImage (data) {
if (data.weather[0].main == "Clear") {
document.querySelector(".js-weather-image")
.innerHTML = `<img src="./images/clear.png" alt="" class="weather-image">`;
}
else if (data.weather[0].main == "Clouds") {
document.querySelector(".js-weather-image")
.innerHTML = '<img src="./images/clouds.png" alt="" class="weather-image">';
}
else if (data.weather[0].main == "Drizzle") {
document.querySelector(".js-weather-image")
.innerHTML = `<img src="./images/drizzle.png" alt="" class="weather-image">`;
}
else if (data.weather[0].main == "Mist") {
document.querySelector(".js-weather-image")
.innerHTML = `<img src="./images/mist.png" alt="" class="weather-image">`;
}
else if (data.weather[0].main == "Rain") {
document.querySelector(".js-weather-image")
.innerHTML = `<img src="./images/rain.png" alt="" class="weather-image">`;
}
else if (data.weather[0].main === "Snow") {
document.querySelector(".js-weather-image")
.innerHTML = `<img src="./images/snow.png" alt="" class="weather-image">`;
}
}