-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathts.js
More file actions
43 lines (35 loc) · 1.69 KB
/
ts.js
File metadata and controls
43 lines (35 loc) · 1.69 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
const BACKEND_URL = "http://localhost:8080/api/weather";
async function getWeather() {
const cityInput = document.getElementById('cityInput');
const weatherCard = document.getElementById('weatherCard');
const errorMessage = document.getElementById('errorMessage');
if (cityInput.value.trim() === "") {
errorMessage.textContent = "Please enter a city name!";
errorMessage.style.display = "block";
return;
}
try {
const response = await fetch(`${BACKEND_URL}?city=${cityInput.value}`);
const weatherData = await response.json();
if (weatherData.error) {
throw new Error(weatherData.error);
}
document.getElementById('cityName').textContent = cityInput.value;
document.getElementById('temperature').textContent = `${weatherData.current_weather.temperature}°C`;
document.getElementById('feelsLike').textContent = `${weatherData.current_weather.temperature}°C`;
document.getElementById('humidity').textContent = `N/A`;
document.getElementById('windSpeed').textContent = `${weatherData.current_weather.windspeed} m/s`;
document.getElementById('description').textContent = `Weather Code: ${weatherData.current_weather.weathercode}`;
weatherCard.classList.add('visible');
errorMessage.style.display = 'none';
} catch (error) {
weatherCard.classList.remove('visible');
errorMessage.textContent = 'Could not fetch weather data. Please try again.';
errorMessage.style.display = 'block';
}
}
document.getElementById('cityInput').addEventListener('keypress', function (event) {
if (event.key === "Enter") {
getWeather();
}
});