forked from DeepNinja07x/Weather-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
103 lines (96 loc) · 3.92 KB
/
script.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
99
100
101
102
103
let weather = {
cityField: document.querySelector(".city"),
iconField: document.querySelector(".icon"),
descriptionField: document.querySelector(".description"),
tempField: document.querySelector(".temp"),
humidityField: document.querySelector(".humidity"),
windField: document.querySelector(".wind"),
apiKey: "5e1ddb8233f281231d95bf6718bddbb8",
changeVisibilty: function (fields, value) {
for (let field of fields) {
field.style.visibility = value;
}
},
fetchWeather: function (city) {
main
fetch(
"https://api.openweathermap.org/data/2.5/weather?q=" +
city +
"&units=metric&appid=" +
this.apiKey
)
.then((response) => {
if (!response.ok) {
alert("Location not found.");
throw new Error("Location not found.");
}
return response.json();
})
.then((data) => this.displayWeather(data));
},
displayWeather: function (data) {
const { name } = data;
const { icon, description } = data.weather[0];
const { temp, humidity } = data.main;
const { speed } = data.wind;
document.querySelector(".city").innerText = "Weather in " + name;
document.querySelector(".icon").src =
"https://openweathermap.org/img/wn/" + icon + ".png";
document.querySelector(".description").innerText = description;
document.querySelector(".temp").innerText = Math.round(temp * 10) / 10 + "°C" + " / " + Math.round((temp*(9/5) + 32) * 10) / 10 + "°F";
document.querySelector(".humidity").innerText =
"Humidity: " + humidity + "%";
document.querySelector(".wind").innerText =
"Wind speed: " + speed + " km/h";
document.querySelector(".weather").classList.remove("loading");
document.body.style.backgroundImage =
"url('https://source.unsplash.com/1600x900/?" + name + "')";
fetch(
"https://api.openweathermap.org/data/2.5/weather?q=" +
city +
"&units=metric&appid=" +
this.apiKey
)
.then((response) => {
if (!response.ok) {
this.cityField.innerText = `Sorry, the weather in ${city} was not found, try again.`;
this.changeVisibilty([this.iconField, this.descriptionField,
this.tempField, this.humidityField, this.windField], "hidden");
throw new Error("No weather found.");
}
return response.json();
})
.then((data) => this.displayWeather(data));
},
displayWeather: function (data) {
const { name } = data;
const { icon, description } = data.weather[0];
const { temp, humidity } = data.main;
const { speed } = data.wind;
this.changeVisibilty([this.iconField, this.descriptionField,
this.tempField, this.humidityField, this.windField], "visible");
this.cityField.innerText = `Weather in ${name}`;
this.iconField.src = `https://openweathermap.org/img/wn/${icon}.png`;
this.descriptionField.innerText = description;
this.tempField.innerText = ` ${temp} °C `;
this.humidityField.innerText = `Humidity: ${humidity} %`;
this.windField.innerText = `Wind speed: ${speed} km/h`;
document.querySelector(".weather").classList.remove("loading");
document.body.style.backgroundImage = "url('https://source.unsplash.com/1600x900/?" + name + "')";
main
},
search: function () {
this.fetchWeather(document.querySelector(".search-bar").value);
},
};
document.querySelector(".search button").addEventListener("click", function () {
weather.search();
});
document
.querySelector(".search-bar")
.addEventListener("keyup", function (event) {
if (event.key == "Enter") {
weather.search();
}
});
weather.fetchWeather("Kolkata");