-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
165 lines (136 loc) · 4.75 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
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
156
157
158
159
160
161
162
163
164
165
const API_CONFIG = {
key: '04b78ff2db37b1392ae7d0ca79fdc470',
baseUrl: 'https://api.openweathermap.org/data/2.5',
endpoints: {
weather: '/weather',
forecast: '/forecast'
}
};
const buildApiUrl = (endpoint, params) => {
const url = new URL(`${API_CONFIG.baseUrl}${endpoint}`);
url.search = new URLSearchParams({
...params,
appid: API_CONFIG.key,
lang: 'fr'
}).toString();
return url;
};
const searchInput = document.getElementById('search-input');
const searchBtn = document.getElementById('search-btn');
const locationName = document.querySelector('.location h1');
const dateElement = document.querySelector('.date');
const temperatureElement = document.querySelector('.temperature');
const descriptionElement = document.querySelector('.description');
const weatherIcon = document.querySelector('.weather-icon i');
const details = document.querySelectorAll('.detail-info p');
const forecastContainer = document.querySelector('.forecast-container');
const kelvinToCelsius = (kelvin) => {
return Math.round(kelvin - 273.15);
};
const formatDate = (date) => {
const options = { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' };
return new Date(date).toLocaleDateString('fr-FR', options);
};
const updateWeatherIcon = (weatherCode) => {
const iconMap = {
'01': 'fa-sun',
'02': 'fa-cloud-sun',
'03': 'fa-cloud',
'04': 'fa-cloud',
'09': 'fa-cloud-rain',
'10': 'fa-cloud-showers-heavy',
'11': 'fa-bolt',
'13': 'fa-snowflake',
'50': 'fa-smog'
};
const iconPrefix = weatherCode.slice(0, 2);
const iconClass = iconMap[iconPrefix] || 'fa-sun';
weatherIcon.className = `fas ${iconClass}`;
};
async function getCurrentWeather(city) {
try {
const weatherUrl = buildApiUrl(API_CONFIG.endpoints.weather, {
q: city,
units: 'metric'
});
const response = await fetch(weatherUrl);
if (!response.ok) {
throw new Error(`Erreur: ${response.status} - ${response.statusText}`);
}
const data = await response.json();
updateCurrentWeather(data);
getForecast(city);
} catch (error) {
handleError(error);
}
}
async function getForecast(city) {
try {
const forecastUrl = buildApiUrl(API_CONFIG.endpoints.forecast, {
q: city,
units: 'metric'
});
const response = await fetch(forecastUrl);
if (!response.ok) {
throw new Error(`Erreur: ${response.status} - ${response.statusText}`);
}
const data = await response.json();
updateForecast(data);
} catch (error) {
handleError(error);
}
}
function updateCurrentWeather(data) {
locationName.textContent = `${data.name}, ${data.sys.country}`;
dateElement.textContent = formatDate(new Date());
temperatureElement.textContent = `${Math.round(data.main.temp)}°C`;
descriptionElement.textContent = data.weather[0].description;
updateWeatherIcon(data.weather[0].icon);
details[0].textContent = `${Math.round(data.main.feels_like)}°C`;
details[1].textContent = `${Math.round(data.wind.speed * 3.6)} km/h`;
details[2].textContent = `${data.main.humidity}%`;
}
function updateForecast(data) {
const dailyForecasts = data.list
.filter(forecast => forecast.dt_txt.includes('12:00:00'))
.slice(0, 5);
forecastContainer.innerHTML = dailyForecasts.map(forecast => `
<div class="forecast-day">
<p>${new Date(forecast.dt_txt).toLocaleDateString('fr-FR', {weekday: 'short'})}</p>
<i class="fas ${getWeatherIconClass(forecast.weather[0].icon)}"></i>
<span>${Math.round(forecast.main.temp)}°C</span>
</div>
`).join('');
}
function handleError(error) {
console.error('Erreur:', error);
alert(`Une erreur est survenue: ${error.message}`);
}
function getWeatherIconClass(iconCode) {
const iconMap = {
'01': 'fa-sun',
'02': 'fa-cloud-sun',
'03': 'fa-cloud',
'04': 'fa-cloud',
'09': 'fa-cloud-rain',
'10': 'fa-cloud-showers-heavy',
'11': 'fa-bolt',
'13': 'fa-snowflake',
'50': 'fa-smog'
};
const iconPrefix = iconCode.slice(0, 2);
return iconMap[iconPrefix] || 'fa-sun';
}
searchBtn.addEventListener('click', () => {
const city = searchInput.value.trim();
if (city) getCurrentWeather(city);
});
searchInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
const city = searchInput.value.trim();
if (city) getCurrentWeather(city);
}
});
document.addEventListener('DOMContentLoaded', () => {
getCurrentWeather('Dakar');
});