-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
149 lines (122 loc) · 5.3 KB
/
Copy pathscript.js
File metadata and controls
149 lines (122 loc) · 5.3 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const apiKey = "e9d3c40b07cbfeece5cacca634713463";
const apiUrl = "https://api.openweathermap.org/data/2.5/weather";
const searchBox = document.querySelector(".search input");
const searchBtn = document.querySelector(".search button");
const locationBtn = document.querySelector(".location-btn");
const weatherIcon = document.querySelector(".weather-icon");
const loader = document.querySelector(".loader");
const TIMEOUT = 10000;
async function getWeatherByCity(city) {
loader.style.display = "block";
document.querySelector(".weather").style.display = "none";
document.querySelector(".error").style.display = "none";
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), TIMEOUT);
try {
const response = await fetch(`${apiUrl}?q=${city}&appid=${apiKey}&units=metric`, { signal: controller.signal });
clearTimeout(timeoutId);
loader.style.display = "none";
if (response.status === 404) {
document.querySelector(".error").style.display = "block";
document.querySelector(".weather").style.display = "none";
} else {
const data = await response.json();
document.querySelector(".city").innerHTML = data.name;
document.querySelector(".temp").innerHTML = Math.round(data.main.temp) + "°C";
document.querySelector(".humidity").innerHTML = data.main.humidity + "%";
document.querySelector(".wind").innerHTML = data.wind.speed + " km/h";
if (data.weather[0].main === "Clouds") {
weatherIcon.src = "img/clouds.png";
} else if (data.weather[0].main === "Clear") {
weatherIcon.src = "img/clear.png";
} else if (data.weather[0].main === "Rain") {
weatherIcon.src = "img/rain.png";
} else if (data.weather[0].main === "Drizzle") {
weatherIcon.src = "img/drizzle.png";
} else if (data.weather[0].main === "Mist") {
weatherIcon.src = "img/mist.png";
}
document.querySelector(".weather").style.display = "block";
document.querySelector(".error").style.display = "none";
}
}
catch (error) {
clearTimeout(timeoutId);
loader.style.display = "none";
if (error.name === 'AbortError') {
document.querySelector(".error").textContent = "Request timed out. Please try again.";
} else {
document.querySelector(".error").textContent = "Request timed out. PLease Check Your Internet Connection";
}
document.querySelector(".error").style.display = "block";
document.querySelector(".weather").style.display = "none";
}
}
async function getWeatherByLocation(latitude, longitude) {
loader.style.display = "block";
document.querySelector(".weather").style.display = "none";
document.querySelector(".error").style.display = "none";
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), TIMEOUT);
try {
const response = await fetch(`${apiUrl}?lat=${latitude}&lon=${longitude}&appid=${apiKey}&units=metric`, { signal: controller.signal });
clearTimeout(timeoutId);
loader.style.display = "none";
if (response.status === 404) {
document.querySelector(".error").style.display = "block";
document.querySelector(".weather").style.display = "none";
} else {
const data = await response.json();
document.querySelector(".city").innerHTML = data.name;
document.querySelector(".temp").innerHTML = Math.round(data.main.temp) + "°C";
document.querySelector(".humidity").innerHTML = data.main.humidity + "%";
document.querySelector(".wind").innerHTML = data.wind.speed + " km/h";
if (data.weather[0].main === "Clouds") {
weatherIcon.src = "img/clouds.png";
} else if (data.weather[0].main === "Clear") {
weatherIcon.src = "img/clear.png";
} else if (data.weather[0].main === "Rain") {
weatherIcon.src = "img/rain.png";
} else if (data.weather[0].main === "Drizzle") {
weatherIcon.src = "img/drizzle.png";
} else if (data.weather[0].main === "Mist") {
weatherIcon.src = "img/mist.png";
}
document.querySelector(".weather").style.display = "block";
document.querySelector(".error").style.display = "none";
}
} catch (error) {
clearTimeout(timeoutId);
loader.style.display = "none";
if (error.name === 'AbortError') {
document.querySelector(".error").textContent = "Request timed out. Please try again.";
} else {
document.querySelector(".error").textContent = "Error fetching weather data.";
}
document.querySelector(".error").style.display = "block";
document.querySelector(".weather").style.display = "none";
}
}
function getLocationAndWeather() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
const { latitude, longitude } = position.coords;
getWeatherByLocation(latitude, longitude);
}, (error) => {
console.error("Error getting location: ", error);
document.querySelector(".error").style.display = "block";
loader.style.display = "none";
});
} else {
alert("Geolocation is not supported by this browser.");
}
}
searchBtn.addEventListener("click", () => {
const city = searchBox.value.trim();
if (city) {
getWeatherByCity(city);
}
});
locationBtn.addEventListener("click", () => {
getLocationAndWeather();
});