-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
79 lines (70 loc) · 2.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class UI {
constructor() {
this.temperature = document.getElementById("temperature");
this.humidity = document.getElementById("humidity");
this.description = document.getElementById("description");
this.windspeed = document.getElementById("windspeed");
this.cloudiness = document.getElementById("cloudiness");
this.city = document.getElementById("city");
}
showInformation(weather) {
this.temperature.innerHTML = Math.round(weather.main.temp - 272.15) + "°C";
this.humidity.innerHTML = weather.main.humidity + "%";
this.description.innerHTML = weather.weather[0].description;
this.windspeed.innerHTML = Math.round(weather.wind.speed) + "km/h";
this.cloudiness.innerHTML = weather.clouds.all + "%";
this.city.innerHTML = searchCity;
}
clearOutput() {
document.getElementById("search_input").value = "";
document.getElementById("city").innerHTML = "";
document.getElementById("temperature").innerHTML = "";
document.getElementById("humidity").innerHTML = "";
document.getElementById("description").innerHTML = "";
document.getElementById("windspeed").innerHTML = "";
document.getElementById("cloudiness").innerHTML = "";
}
showNotFound() {
document.getElementById("notfound").innerHTML = "City Not Found";
setTimeout(() => {
document.getElementById("notfound").innerHTML = "";
}, 3000);
}
}
class Weather {
constructor() {
this.api_key = "753817e77bafd9796a32d0ba7e32ba54";
}
async getWeather(searchCity) {
const weatherResponse = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${searchCity}&appid=${this.api_key}`);
const weather = await weatherResponse.json();
return {
weather
}
}
}
// Init Weather
const weather = new Weather();
// Init UI
const ui = new UI();
// User searched city
let searchCity;
document.getElementById("second-btn").addEventListener('click', (e) => {
//Search input
searchCity = document.getElementById("search_input").value;
if (searchCity === "") {
ui.showNotFound();
} else {
//Make HTTP call
weather.getWeather(searchCity)
.then((data) => {
ui.showInformation(data.weather);
});
}
e.preventDefault();
});
document.getElementById("first-btn").addEventListener("click", (e) => {
// Clear the output
ui.clearOutput();
e.preventDefault();
});