-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
59 lines (49 loc) · 2.1 KB
/
script.js
File metadata and controls
59 lines (49 loc) · 2.1 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
document.addEventListener('DOMContentLoaded', () => {
const cityInput = document.getElementById("input");
const weatherButton = document.getElementById('button');
const weatherInfo = document.getElementById('weather-info');
const cityNameDisp = document.getElementById('city-name');
const temperatureDisp = document.getElementById('temperature');
const descriptionDisp = document.getElementById('description');
const errorMessage = document.getElementById('error-message');
const API_KEY = 'e3122816d93b171a52b8f77357ec4e20'
weatherButton.addEventListener('click', async () => {
const city = cityInput.value.trim()
if (!city) return;
// it may throw an error
// server/database is alwaya in another continent
try {
const weatherData = await fetchWeatherData(city);
displayWeatherData(weatherData);
} catch (error) {
showError()
}
});
async function fetchWeatherData(city) {
//gets the data
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${API_KEY}`;
try {
const response = await fetch(url);
const data = await response.json();
console.log(typeof response);
console.log("RESPONSE", data);
return data;
} catch (error) {
console.error("Error fetching weather data ", error);
}
}
function displayWeatherData(weatherData) {
console.log(weatherData);
const {name,main,weather} = weatherData;
cityNameDisp.textContent= `City: ${name}`;
temperatureDisp.textContent = `Temperature: ${main.temp}°C`;
descriptionDisp.textContent = `Weather: ${weather[0].description}`;
//unlock the display
weatherInfo.classList.remove('hidden');
errorMessage.classList.add('hidden');
}
function showError() {
weatherInfo.classList.add('hidden');
errorMessage.classList.remove('hidden');
}
});