-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
71 lines (61 loc) · 2.33 KB
/
script.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
const apiKey = '8d6454a89dff871786a0307b0dbebbee'
async function fetchWeatherData(city) {
try {
const response = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey}`
);
if (!response.ok) {
throw new Error("Unable to fetch weather data");
}
const data = await response.json();
console.log(data);
updateWeatherUI(data);
} catch (error) {
console.error(error);
}
}
const cityElement = document.querySelector(".city");
const temperature = document.querySelector(".temp");
const windSpeed = document.querySelector(".wind-speed");
const humidity = document.querySelector(".humidity");
const visibility = document.querySelector(".visibility-distance");
const descriptionText = document.querySelector(".description-text");
const date = document.querySelector(".date");
const descriptionIcon = document.querySelector(".description i");
function updateWeatherUI(data) {
cityElement.textContent = data.name;
temperature.textContent = `${Math.round(data.main.temp)}`;
windSpeed.textContent = `${data.wind.speed} km/h`;
humidity.textContent = `${data.main.humidity}%`;
visibility.textContent = `${data.visibility / 1000} km`;
descriptionText.textContent = data.weather[0].description;
const currentDate = new Date();
date.textContent = currentDate.toDateString();
const weatherIconName = getWeatherIconName(data.weather[0].main);
descriptionIcon.innerHTML = `<i class="material-icons">${weatherIconName}</i>`;
}
const formElement = document.querySelector(".search-form");
const inputElement = document.querySelector(".city-input");
formElement.addEventListener("submit", function (e) {
e.preventDefault();
const city = inputElement.value;
if (city !== "") {
fetchWeatherData(city);
inputElement.value = "";
}
});
function getWeatherIconName(weatherCondition) {
const iconMap = {
Clear: "wb_sunny",
Clouds: "wb_cloudy",
Rain: "umbrella",
Thunderstorm: "flash_on",
Drizzle: "grain",
Snow: "ac_unit",
Mist: "cloud",
Smoke: "cloud",
Haze: "cloud",
Fog: "cloud",
};
return iconMap[weatherCondition] || "help";
}