-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
155 lines (141 loc) · 5.01 KB
/
app.js
File metadata and controls
155 lines (141 loc) · 5.01 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
150
151
152
153
154
155
const API_KEY = '44473efb7cb26143190f7d2899b22f68';
document.getElementById('search-btn').addEventListener('click', async () => {
const city = document.getElementById('city-input').value.trim();
if (city) {
try {
const data = await fetchWeather(city);
displayWeather(data);
const forecastData = await fetchExtendedForecast(city);
displayExtendedForecast(forecastData);
saveRecentCity(city);
} catch (error) {
displayError(error.message);
}
} else {
displayError('Please enter a city name.');
}
});
document.getElementById('current-location-btn').addEventListener('click', () => {
navigator.geolocation.getCurrentPosition(async (position) => {
const { latitude, longitude } = position.coords;
try {
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${API_KEY}&units=metric`);
const data = await response.json();
if (response.ok) {
displayWeather(data);
const forecastData = await fetchExtendedForecastByCoordinates(latitude, longitude);
displayExtendedForecast(forecastData);
} else {
throw new Error(data.message);
}
} catch (error) {
displayError(error.message);
}
}, () => {
displayError('Unable to retrieve your location.');
});
});
async function fetchExtendedForecastByCoordinates(latitude, longitude) {
try {
const response = await fetch(`https://api.openweathermap.org/data/2.5/forecast?lat=${latitude}&lon=${longitude}&appid=${API_KEY}&units=metric`);
const data = await response.json();
if (response.ok) {
return data;
} else {
throw new Error(data.message);
}
} catch (error) {
console.error('Error fetching extended forecast:', error);
throw error;
}
}
async function fetchWeather(city) {
try {
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${API_KEY}&units=metric`);
const data = await response.json();
if (response.ok) {
return data;
} else {
throw new Error(data.message);
}
} catch (error) {
console.error('Error fetching weather data:', error);
throw error;
}
}
async function fetchExtendedForecast(city) {
try {
const response = await fetch(`https://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=${API_KEY}&units=metric`);
const data = await response.json();
if (response.ok) {
return data;
} else {
throw new Error(data.message);
}
} catch (error) {
console.error('Error fetching extended forecast:', error);
throw error;
}
}
function displayWeather(data) {
const weatherDiv = document.getElementById('weather-data');
weatherDiv.innerHTML = `
<div id="weather-data-container">
<h2 class="text-2xl font-bold">${data.name}</h2>
<p>Temperature: ${data.main.temp} °C</p>
<p>Humidity: ${data.main.humidity} %</p>
<p>Wind Speed: ${data.wind.speed} m/s</p>
</div>
<div id="weather-icon" class="flex flex-col items-center justify-center">
<img src="http://openweathermap.org/img/wn/${data.weather[0].icon}.png" alt="Weather icon" class="w-16 h-16">
<p>${data.weather[0].description}</p>
</div>
`;
weatherDiv.classList.remove('hidden');
}
function displayExtendedForecast(data) {
const forecastDiv = document.getElementById('forecast-data');
forecastDiv.innerHTML = data.list.slice(0, 5).map(forecast => `
<div class="forecast-item mb-4 bg-gray-300 p-4 rounded-lg">
<p>${new Date(forecast.dt_txt).toLocaleDateString()}</p>
<img src="http://openweathermap.org/img/wn/${forecast.weather[0].icon}.png" alt="Weather icon">
<p>Temperature: ${forecast.main.temp} °C</p>
<p>Wind: ${forecast.wind.speed} m/s</p>
<p>Humidity: ${forecast.main.humidity} %</p>
</div>
`).join('');
forecastDiv.classList.remove('hidden');
}
function displayError(message) {
const weatherDiv = document.getElementById('weather-data');
weatherDiv.innerHTML = `<p class="text-red-500">${message}</p>`;
weatherDiv.classList.remove('hidden');
}
function saveRecentCity(city) {
let recentCities = JSON.parse(localStorage.getItem('recentCities')) || [];
if (!recentCities.includes(city)) {
recentCities.push(city);
localStorage.setItem('recentCities', JSON.stringify(recentCities));
updateRecentCitiesDropdown();
}
}
function updateRecentCitiesDropdown() {
const dropdown = document.getElementById('recent-cities-dropdown');
dropdown.innerHTML = '';
const recentCities = JSON.parse(localStorage.getItem('recentCities')) || [];
recentCities.forEach(city => {
const option = document.createElement('option');
option.value = city;
option.textContent = city;
dropdown.appendChild(option);
});
}
document.getElementById('recent-cities-dropdown').addEventListener('change', async (event) => {
const city = event.target.value;
try {
const data = await fetchWeather(city);
displayWeather(data);
} catch (error) {
displayError(error.message);
}
});