-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
140 lines (117 loc) · 4.42 KB
/
main.js
File metadata and controls
140 lines (117 loc) · 4.42 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
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
let container = document.querySelector('.container'),
searchCity = document.querySelector('.searchcity'),
btnSearch = document.querySelector('.btn-search'),
cityLocation = document.querySelector('.city'),
dateTxt = document.querySelector('.date-txt'),
cloud = document.querySelector('.cloud'),
temperature = document.querySelector('.temp'),
weather = document.querySelector('.weather'),
humdityDegree = document.querySelector('.hum-degree'),
windDegree = document.querySelector('.wind-degree'),
forecastSection = document.querySelector('.forecast-section');
let sectionFound = document.querySelector('.section-found'),
sectionSearch = document.querySelector('.section-search'),
sectionNotFound = document.querySelector('.not-found-search');
btnSearch.onclick = function() {
if (searchCity.value.trim() !== "") {
updateWeather(searchCity.value);
searchCity.value = "";
}
}
searchCity.onkeydown = function(e) {
if (e.key === 'Enter' && searchCity.value.trim() !== "") {
updateWeather(searchCity.value);
searchCity.value = "";
}
}
const apiKey = '4db3dac04d6dce8c7b6cab87350baf9d';
async function getFetchData(endPoint,city) {
const apiUrl = `https://api.openweathermap.org/data/2.5/${endPoint}?q=${city}&appid=${apiKey}&units=metric`;
let response = await fetch(apiUrl);
return response.json();
}
function getCurrentDate() {
let currentDate = new Date();
let newDate = {
weekday: 'short',
day: '2-digit',
month: 'short'
}
return currentDate.toLocaleDateString('en-GB', newDate);
}
function getImage(id) {
if (id >= 200 && id <= 232) return "thunderstorm.svg";
else if (id >= 300 && id <= 321) return "drizzle.svg";
else if (id >= 500 && id <= 521) return "rain.svg";
else if (id >= 600 && id <= 622) return "snow.svg";
else if (id === 800) return "clear.svg";
else if (id >= 801 && id <= 804) return "clouds.svg";
else return "atmosphere.svg";
}
function updateTheme(mainWeather) {
// إزالة أي كلاس قديم
document.body.classList.remove('weather-clear', 'weather-rain', 'weather-clouds', 'weather-drizzle', 'weather-thunderstorm', 'weather-atmosphere');
// إضافة الكلاس الجديد بناءً على الحالة (تحويل للنص الصغير للتأكد من المطابقة)
const weatherClass = `weather-${mainWeather.toLowerCase()}`;
document.body.classList.add(weatherClass);
}
async function updateWeather(city) {
let weatherData = await getFetchData('weather', city);
if (weatherData.cod !== 200) {
showSection(sectionNotFound);
return;
}
const {
name: country,
main: {temp,humidity},
weather: [{id,main}],
wind: {speed}
} = weatherData
cityLocation.textContent = country;
temperature.textContent = Math.round(temp) + "" + '°C';
weather.textContent = main;
humdityDegree.textContent = humidity + "%";
windDegree.textContent = speed + "" + 'M/s';
dateTxt.textContent = getCurrentDate();
cloud.src = `assets/assets/weather/${getImage(id)}`;
updateTheme(main)
showSection(sectionFound);
await updateForecast(city);
}
async function updateForecast(city) {
let forecastData = await getFetchData('forecast', city);
let timeTaken = '12:00:00';
let todayDate = new Date().toISOString().split('T')[0];
forecastSection.innerHTML = "";
forecastData.list.forEach(item =>{
if (item.dt_txt.includes(timeTaken) && !item.dt_txt.includes(todayDate)) {
forecastItem(item);
};
})
}
function forecastItem(itemData) {
const {
dt_txt: date,
main: {temp},
weather: [{id}]
} = itemData
let foreDate = new Date(date);
let newForeDate = {
day: '2-digit',
month: 'short'
}
let resultDate = foreDate.toLocaleDateString('en-GB', newForeDate);
let forecastHtml = `
<div class="forecast">
<div class="date-info">${resultDate}</div>
<img class="fore-cloud" src="assets/assets/weather/${getImage(id)}" alt="">
<h5 class="fore-degree">${Math.round(temp)} °C</h5>
</div>`;
forecastSection.insertAdjacentHTML('beforeend', forecastHtml);
}
function showSection(section) {
[sectionFound,sectionSearch,sectionNotFound].forEach(sec =>{
sec.style.display = 'none';
})
section.style.display = 'block';
}