-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
193 lines (157 loc) · 6.06 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"use strict";
// API key
const API = "15b9c78c017d5a4c079806d61f090cf6";
const dayEl = document.querySelector(".default_day");
const dateEl = document.querySelector(".default_date");
const btnEl = document.querySelector(".btn_search");
const inputEl = document.querySelector(".input_field");
const iconsContainer = document.querySelector(".icons");
const dayInfoEl = document.querySelector(".day_info");
const listContentEl = document.querySelector(".list_content ul");
const imageSection = document.querySelector(".img_section");
const days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
]
// ===== dispaly the day =====
const day = new Date();
const dayName = days[day.getDay()];
dayEl.textContent = dayName;
// dispaly the date
let month = day.toLocaleString("default", { month: "long" });
let date = day.getDate();
let year = day.getFullYear();
dateEl.textContent = date + " " + month + " " + year;
// ===== add event =====
btnEl.addEventListener("click", (e) => {
e.preventDefault();
// check empty value
if(inputEl.value !== ""){
const city = inputEl.value;
inputEl.value = "";
findLocation(city);
console.log(city);
}
else{
alert("Please Give City or Country Name");
}
})
findLocation("United Kingdom");
async function findLocation(city){
iconsContainer.innerHTML="";
dayInfoEl.innerHTML="";
listContentEl.innerHTML = "";
try {
const API_URL = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${API}`;
const data = await fetch(API_URL);
const result = await data.json();
console.log(result);
if(result.cod !== "404"){
// display image content of live temperature
const imgContent = displayImageContent(result);
iconsContainer.insertAdjacentHTML("afterbegin", imgContent);
// display right side content
const dayInfoContent = rightSideContent(result);
dayInfoEl.insertAdjacentHTML("afterbegin", dayInfoContent);
// forcast function with latitude and longitute as parameter
displayForeCast(result.coord.lat, result.coord.lon);
}else{
// diaply when the city not avilable
const message = ` <img src="images/404error.png" alt="404Error">
<h2 class="weather_temp">${result.cod}</h2>
<h3 class="cloudtxt">${result.message}</h3> `;
iconsContainer.insertAdjacentHTML("afterbegin", message);
const daynfoContent = `<div class="content">
<p class="title">Name</p>
<span class="value">City Not found</span>
</div>
<div class="content">
<p class="Temp">Temp</p>
<span class="value">Not found</span>
</div>
<div class="content">
<p class="Temp">HUMIDITY</p>
<span class="value">Not found</span>
</div>
<div class="content">
<p class="Temp">WIND SPEED</p>
<span class="value">Not found</span>
</div>`;
dayInfoEl.innerHTML = daynfoContent;
}
} catch (error) {
}
}
function displayImageContent(data){
return ` <img src="https://openweathermap.org/img/wn/${data.weather[0].icon}@2x.png" alt="weather" />
<h2 class="weather_temp">${Math.round(data.main.temp - 273.15)}°C</h2>
<h3 class="cloudtxt">${data.weather[0].description}</h3> `;
}
// display the right side content
function rightSideContent(result){
const rainyConditions = ["light rain", "moderate rain", "heavy rain", "drizzle", "showers", ];
const thunderConditions = ["thunderstorm", "thunderstorms with rain" ]
if (rainyConditions.includes(result.weather[0].description)) {
imageSection.classList.add ("rain_bg");
} if(thunderConditions.includes(result.weather[0].description)) {
imageSection.classList.add("thunder_bg")
}else{
imageSection.classList.remove ("rain_bg");
imageSection.classList.remove("thunder_bg")
}
return `<div class="content">
<p class="title">Name</p>
<span class="value">${result.name}</span>
</div>
<div class="content">
<p class="title">Temp</p>
<span class="value">${Math.round(result.main.temp - 273.15)}°C</span>
</div>
<div class="content">
<p class="title">HUMIDITY</p>
<span class="value">${result.main.humidity}%</span>
</div>
<div class="content">
<p class="title">WIND SPEED</p>
<span class="value">${(result.wind.speed * 3.6).toFixed(2)}km/h</span>
</div>`;
}
async function displayForeCast(lat,lon){
const ForeCast_API = ` https://api.openweathermap.org/data/2.5/forecast?lat=${lat}&lon=${lon}&appid=${API} `;
const data = await fetch(ForeCast_API);
const result = await data.json();
console.log(result);
// filter the forecast
const uniqeForeCastDays = [];
const daysForecast = result.list.filter( forecast => {
const forecastDate = new Date(forecast.dt_txt).getDate();
if(!uniqeForeCastDays.includes(forecastDate)){
return uniqeForeCastDays.push(forecastDate);
}
})
console.log(daysForecast);
daysForecast.forEach((content, index) => {
if(index <= 4 && index > 0){
listContentEl.insertAdjacentHTML("beforeend", forecast(content))
}
});
}
// forecast html elemnt data
function forecast(frContent){
const day = new Date(frContent.dt_txt);
const dayName = days[day.getDay()];
const splitDay = dayName.split("", 3);
const joinDay = splitDay.join("");
return `
<li>
<span>${joinDay}</span>
<img src="https://openweathermap.org/img/wn/${frContent.weather[0].icon}@2x.png" alt="weather"
<span class="day_temp">${Math.round(frContent.main.temp - 273.15)}°C</span>
</li>
`
}