-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
106 lines (91 loc) · 3.27 KB
/
weather.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
import {updateForecast} from './forecast.js';
const searchButton = document.querySelector('.js-search-button');
const searchInput = document.querySelector('.js-search');
const cityName = document.querySelector('.js-city-name');
const temperature = document.querySelector('.js-temperature');
const humidity = document.querySelector('.js-humidity-percent');
const wind = document.querySelector('.js-wind-speed');
const feel = document.querySelector('.js-real-feel-temp');
const pressure = document.querySelector('.js-pressure-hPa');
const icon = document.querySelector('.js-icon');
const lastSearchedCity = localStorage.getItem('lastSearchedCity') || "London";
if(lastSearchedCity){
searchInput.value = lastSearchedCity;
updateWeather();
updateForecast();
}
document.body.addEventListener('keydown', (event) =>{
if(event.key === 'Enter'){
updateWeather();
updateForecast();
}
});
searchButton.addEventListener('click', () =>{
updateWeather();
updateForecast();
});
async function updateWeather() {
const city = searchInput.value;
if(city){
try{
localStorage.setItem('lastSearchedCity', city);
const data = await getWeather(city);
// console.log(data);
cityName.innerHTML = data.name;
temperature.innerHTML = `${Math.round(data.main.temp)}°C`;
humidity.innerHTML = `${data.main.humidity}%`;
wind.innerHTML = `${(data.wind.speed).toFixed(1)} km/h`;
feel.innerHTML = `${Math.round(data.main.feels_like)}°C`;
pressure.innerHTML = `${data.main.pressure} hPa`;
icon.src = iconImage(data);
searchInput.value = '';
} catch(error){
console.error("Error fetching weather data:", error);
cityName.innerHTML = 'City not found';
temperature.innerHTML = ``;
humidity.innerHTML = '';
wind.innerHTML = ``;
icon.src = ``;
searchInput.value = '';
}
}
}
async function getWeather(city){
const apiKey = "807b0a1f0c5726475a4225e8eb35a177";
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
const response = await fetch(apiUrl);
if(!response.ok){
throw new Error(`HTTP error! Status: ${response.status}`)
}
const data = await response.json();
return data;
}
export function iconImage(data){
if(data.weather[0].main === 'Clouds'){
return 'images/clouds.png';
}
else if(data.weather[0].main === 'Clear'){
return 'images/clear.png';
}
else if(data.weather[0].main === 'Drizzle'){
return 'images/drizzle.png';
}
else if(data.weather[0].main === 'Mist'){
return 'images/mist.png';
}
else if(data.weather[0].main === 'Rain'){
return 'images/rain.png';
}
else if(data.weather[0].main === 'Snow'){
return 'images/snow.png';
}
else if(data.weather[0].main === 'Thunderstorm'){
return 'images/thunderstorm.png';
}
else if(data.weather[0].main === 'Haze'){
return 'images/haze.png';
}
else if(data.weather[0].main === 'Fog' || 'Smoke' || 'Dust' || 'Sand' || 'Ash' || 'Squall' || 'Tornado'){
return 'images/fog.png';
}
}