-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.js
More file actions
152 lines (130 loc) · 4.5 KB
/
Script.js
File metadata and controls
152 lines (130 loc) · 4.5 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
141
142
143
144
145
146
147
148
149
150
151
152
"use strict";
const API = "6470f0b01f3079f88465bfdf5062d707";
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 days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
// display the day
const day = new Date();
const dayName = days[day.getDay()];
dayEl.textContent = dayName;
// display date
let month = day.toLocaleString("default", { month: "long" });
let date = day.getDate();
let year = day.getFullYear();
console.log();
dateEl.textContent = date + " " + month + " " + year;
// add event
btnEl.addEventListener("click", (e) => {
e.preventDefault();
// check empty value
if (inputEl.value !== "") {
const Search = inputEl.value;
inputEl.value = "";
findLocation(Search);
} else {
console.log("Please Enter City or Country Name");
}
});
async function findLocation(name) {
iconsContainer.innerHTML = "";
dayInfoEl.innerHTML = "";
listContentEl.innerHTML = "";
try {
const API_URL = `https://api.openweathermap.org/data/2.5/weather?q=${name}&appid=${API}`;
const data = await fetch(API_URL);
const result = await data.json();
console.log(result);
if (result.cod !== "404") {
// display image content
const ImageContent = displayImageContent(result);
// display right side content
const rightSide = rightSideContent(result);
// forecast function
displayForeCast(result.coord.lat, result.coord.lon);
setTimeout(() => {
iconsContainer.insertAdjacentHTML("afterbegin", ImageContent);
iconsContainer.classList.add("fadeIn");
dayInfoEl.insertAdjacentHTML("afterbegin", rightSide);
}, 1500);
} else {
const message = `<h2 class="weather_temp">${result.cod}</h2>
<h3 class="cloudtxt">${result.message}</h3>`;
iconsContainer.insertAdjacentHTML("afterbegin", message);
}
} catch (error) {}
}
// display image content and temp
function displayImageContent(data) {
return `<img src="https://openweathermap.org/img/wn/${
data.weather[0].icon
}@4x.png" alt="" />
<h2 class="weather_temp">${Math.round(data.main.temp - 275.15)}°C</h2>
<h3 class="cloudtxt">${data.weather[0].description}</h3>`;
}
// display the right side content
function rightSideContent(result) {
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 - 275.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} Km/h</span>
</div>`;
}
async function displayForeCast(lat, long) {
const ForeCast_API = `https://api.openweathermap.org/data/2.5/forecast?lat=${lat}&lon=${long}&appid=${API}`;
const data = await fetch(ForeCast_API);
const result = await data.json();
// 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, indx) => {
if (indx <= 3) {
listContentEl.insertAdjacentHTML("afterbegin", forecast(content));
}
});
}
// forecast html element 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("");
// console.log(dayName);
return `<li>
<img src="https://openweathermap.org/img/wn/${
frContent.weather[0].icon
}@2x.png" />
<span>${joinDay}</span>
<span class="day_temp">${Math.round(frContent.main.temp - 275.15)}°C</span>
</li>`;
}
// password for api is "samirNFSU@2024"