-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
290 lines (233 loc) · 9.96 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { API_KEY, API, GEO_API, ICON_URL } from './config.js'
const citySearch = document.getElementById("citySearch");
const searchButton = document.getElementById("searchButton")
const currentLocation = document.getElementById("currentLocation")
const errorMessage = document.getElementById("errorMessage");
const citySuggestions = new Set(); // To store unique city names
const dropdownList = document.querySelector("#dropdownList");
const MAX_SUGGESTIONS = 5; // Limit the dropdown to 5 cities
// Default fallback coordinates (Mumbai, for example)
const DEFAULT_LAT = 19.07; // Mumbai Latitude
const DEFAULT_LON = 72.87; // Mumbai Longitude
// Temperature Conversion
function convertToCelsius(kelvin) {
return (kelvin - 273.15).toFixed(2);
}
// Wind Speed Conversion: function to convert wind speed to km/h:
function convertWindSpeedToKmH(mps) {
return (mps * 3.6).toFixed(2);
}
function airQualityIndex(aqi) {
let aqiMsg = `aqi`;
switch (aqi) {
case 1:
aqiMsg = `Good`;
break;
case 2:
aqiMsg = `Fair`;
break;
case 3:
aqiMsg = `Moderate`;
break;
case 4:
aqiMsg = `Poor`;
break;
case 5:
aqiMsg = `Very Poor`;
break;
default:
break;
}
return aqiMsg;
}
// Convert Unix timestamp to Date object
function convertDataAndTime(dt) {
const date = new Date(dt * 1000);
const dateString = date.toLocaleDateString(); // Format the date
const timeString = date.toLocaleTimeString(); // Format the time
const todayDate = `${dateString} ${timeString}`
return todayDate;
}
async function fetchCoordinates(cityName) {
try {
const resp = await fetch(`${GEO_API}?q=${cityName}&appid=${API_KEY}`);
const data = await resp.json();
if (data.length > 0) {
const { lat, lon } = data[0];
return { lat, lon };
}
} catch (error) {
return null;
}
}
function getUserLocation() {
return new Promise((resolve, reject) => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(position) => {
const { latitude, longitude } = position.coords;
resolve({ lat: latitude, lon: longitude });
},
(error) => {
reject(error);
}
)
}
})
}
async function fetchWeatherForCoordinates(lat, lon) {
try {
const weatherResponse = await fetch(`${API}/weather?lat=${lat}&lon=${lon}&appid=${API_KEY}`)
const weatherData = await weatherResponse.json();
const pollutionResponse = await fetch(`${API}/air_pollution?lat=${lat}&lon=${lon}&appid=${API_KEY}`)
const pollutionData = await pollutionResponse.json();
displayCurrentWeather(weatherData, pollutionData);
const forecastResponse = await fetch(`${API}/forecast?lat=${lat}&lon=${lon}&appid=${API_KEY}`)
const forecastData = await forecastResponse.json();
displayForecastWeather(forecastData);
} catch (err) {
showErrorMessage("Error fetching weather or pollution data.");
return err;
}
}
function displayCurrentWeather(resp, pollutants) {
const currentWeather = document.getElementById("currentWeather");
currentWeather.querySelector(".currentDateAndTime").textContent = convertDataAndTime(resp.dt);
currentWeather.querySelector("img").src = `${ICON_URL}${resp.weather[0].icon}@2x.png`;
const aqiMsg = airQualityIndex(pollutants.list[0].main.aqi);
currentWeather.querySelector("#aqiDisplay").textContent = `${aqiMsg}`
currentWeather.querySelector("#weatherDesc").textContent = resp.weather[0].description
currentWeather.querySelector("#cityName").textContent = resp.name;
currentWeather.querySelector("#currentTemperature").textContent = `Temperature: ${convertToCelsius(resp.main.temp)}°C`;
currentWeather.querySelector("#currentHumidity").textContent = `Humidity: ${resp.main.humidity}%`;
currentWeather.querySelector("#currentWind").textContent = `Wind: ${convertWindSpeedToKmH(resp.wind.speed)} km/h`;
}
function displayForecastWeather(resp) {
const extendedForecast = document.getElementById("extendedForecast");
const forecastTemplate = document.querySelector("#forecastTemplate");
// Clear previous forecast cards
extendedForecast.innerHTML = ''; // Clears all child elements of the container
// Create a Map to group forecasts by date (12 PM for uniformity)
const forecastMap = new Map();
// Iterate over the forecast data
resp.list.forEach(item => {
const forecastDate = new Date(item.dt * 1000); // Convert UNIX timestamp to Date
const forecastDateMidday = new Date(forecastDate.setHours(12, 0, 0, 0)); // Normalize to 12 PM for each day
// If the forecast is not in the map then add it
if (!forecastMap.has(forecastDateMidday.toDateString())) {
forecastMap.set(forecastDateMidday.toDateString(), item);
}
});
// Convert the map to an array and get the first 5 days
const forecastDays = Array.from(forecastMap.values()).slice(0, 5);
// Display the forecast for each of the 5 days
forecastDays.forEach(day => {
const card = forecastTemplate.cloneNode(true);
// Set the forecast date
card.querySelector('h3').textContent = new Date(day.dt * 1000).toLocaleDateString();
// Set weather icon and description
card.querySelector('img').src = `${ICON_URL}${day.weather[0].icon}@2x.png`;
card.querySelector('.desc').textContent = day.weather[0].description;
// Set temperature, humidity, and wind speed
card.querySelector(".temperature").textContent = `Temperature: ${convertToCelsius(day.main.temp)}°C`;
card.querySelector(".humidity").textContent = `Humidity: ${day.main.humidity}%`;
card.querySelector(".wind").textContent = `Wind Speed: ${convertWindSpeedToKmH(day.wind.speed)} km/h`;
extendedForecast.append(card); // Add the new card to the container
});
}
// Add city to session storage if it's a valid city name (not a partial input)
function addCityToSuggestions(cityName) {
if (cityName && !citySuggestions.has(cityName)) {
citySuggestions.add(cityName);
const cityArray = Array.from(citySuggestions);
sessionStorage.setItem("cityName", JSON.stringify(cityArray)); // Store in sessionStorage
}
}
// Function to update the dropdown with city suggestions based on user input
function dropDownList(cityName) {
// Only add valid city names (after typing complete city name, not partials)
if (cityName.length > 1 && !citySuggestions.has(cityName)) {
addCityToSuggestions(cityName);
}
// Clear any existing items in the dropdown list
dropdownList.innerHTML = '';
let cities = sessionStorage.getItem("cityName");
if (cities) {
cities = JSON.parse(cities);
const filteredCities = cities.slice(0, MAX_SUGGESTIONS);
// If there are cities to display, show the dropdown
if (filteredCities.length > 0) {
filteredCities.forEach((city) => {
// Create the list item for each city
const list = document.createElement("li");
list.textContent = city;
list.style.cursor = "pointer"; // Make the cursor a pointer for click action
// Event listener to update input field on click
list.addEventListener("click", () => {
citySearch.value = city; // Set the value of the input field
dropdownList.classList.add("hidden"); // Hide the dropdown after selection
});
dropdownList.appendChild(list); // Append the list item to the dropdown
});
// Show the dropdown if there are cities to display
dropdownList.classList.remove("hidden");
} else {
dropdownList.classList.add("hidden"); // Hide the dropdown if no matches
}
} else {
dropdownList.classList.add("hidden"); // Hide the dropdown if no cities
}
}
// Event listener for input field to trigger dropdown list
citySearch.addEventListener("focus", () => {
const cityName = citySearch.value.trim();
if (cityName.length > 0) {
dropDownList(cityName); // Call to show cities in dropdown
} else {
dropdownList.classList.add("hidden"); // Hide dropdown if input is empty
}
});
// Close dropdown if clicked outside of input or dropdown
document.addEventListener("click", (event) => {
if (!citySearch.contains(event.target) && !dropdownList.contains(event.target)) {
dropdownList.classList.add("hidden"); // Hide dropdown if click is outside
}
});
function showErrorMessage(message, duration = 2000) {
errorMessage.classList.remove("hidden");
errorMessage.querySelector("#errorText").textContent = message;
setTimeout(() => {
errorMessage.classList.add("hidden");
}, duration);
}
searchButton.addEventListener("click", async () => {
const cityName = citySearch.value.trim();
if (!cityName) {
showErrorMessage("Please enter city name.");
}
if (cityName) {
const coordinates = await fetchCoordinates(cityName);
if (coordinates) {
const { lat, lon } = coordinates;
fetchWeatherForCoordinates(lat, lon);
} else {
showErrorMessage("City not found.")
}
}
})
currentLocation.addEventListener("click", async () => {
try {
const { lat, lon } = await getUserLocation();
fetchWeatherForCoordinates(lat, lon);
} catch (error) {
showErrorMessage("Please enable your location .")
}
})
window.addEventListener("load", async () => {
try {
const { lat, lon } = await getUserLocation();
fetchWeatherForCoordinates(lat, lon);
} catch (error) {
fetchWeatherForCoordinates(DEFAULT_LAT, DEFAULT_LON);
}
})