forked from Technigo/project-weather-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
294 lines (268 loc) · 8.83 KB
/
script.js
File metadata and controls
294 lines (268 loc) · 8.83 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// DOM
const body = document.querySelector("body");
const weatherToday = document.getElementById("weather-today");
const weatherForecast = document.getElementById("weather-forecast");
const weatherBackground = document.querySelector(".weather-background");
const menuBtn = document.getElementById("menu-btn");
const menuClose = document.getElementById("close");
const navWrapper = document.querySelector(".nav");
const navItems = document.querySelectorAll(".nav-item");
const navGeo = document.querySelector(".geo");
const searchInput = document.getElementById("location-search");
const searchBtn = document.querySelector(".search-btn");
const scrollArrow = document.getElementById("scroll-arrow");
// global var
const APP_ID = "22a9947f80352a8e0b470d4aaefb4388";
const API_URL = "https://api.openweathermap.org";
// Print error
const printError = error => {
const errorMessage = document.createElement("div");
errorMessage.innerHTML = `<p class="error">Unfortunately, something went wrong and we could not find your location.</p>`;
weatherBackground.insertBefore(errorMessage, weatherToday);
setTimeout(() => weatherBackground.removeChild(errorMessage), 5000);
};
// -- Styling
// Toggle class hidden
const toggleHide = el => el.classList.toggle("hidden");
// Toggle class fullscreen
const toggleFullscreen = el => el.classList.toggle("fullscreen");
// Change background and image if it's night
const setStyling = weatherData => {
const currentTime = convertTime(Date.now() / 1000, weatherData.timezone);
const sunset = convertTime(weatherData.sunset, weatherData.timezone);
const sunrise = convertTime(weatherData.sunrise, weatherData.timezone);
if (currentTime > sunset || currentTime < sunrise) {
weatherBackground.classList.add("night");
return "./design/design1/assets/moon.svg";
} else {
weatherBackground.classList.remove("night");
return "./design/design1/assets/sun.svg";
}
};
// convert to weekday
const setWeekday = date => {
dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
return dayNames[new Date(date).getDay()];
};
// Filter forecast, one entry per day
const getNoons = forecastData =>
forecastData.list.filter(obj => obj.dt_txt.includes("12:00"));
// Convert milliseconds to readable time HH:MM, converted to local time
const convertTime = (seconds, timezone) => {
const local = seconds + timezone;
return new Date(local * 1000);
};
// Construct minutes and hours with two digits UTC
const constructHours = time =>
time.getUTCHours() < 10 ? "0" + time.getUTCHours() : time.getUTCHours();
const constructMinutes = time =>
time.getUTCMinutes() < 10 ? "0" + time.getUTCMinutes() : time.getUTCMinutes();
// Get max temp for entire day from api data
const getMaxTemp = (day, data) => {
const date = convertTime(day.dt, data.timezone).getDate();
const max = data.list
.filter(entry => convertTime(entry.dt, data.timezone).getDate() === date)
.sort((a, b) => b.main.temp_max - a.main.temp_max)[0];
return Math.round(max.main.temp_max);
};
// Get min temp for entire day from api data
const getMinTemp = (day, data) => {
const date = convertTime(day.dt, data.timezone).getDate();
const min = data.list
.filter(entry => convertTime(entry.dt, data.timezone).getDate() === date)
.sort((a, b) => a.main.temp_min - b.main.temp_min)[0];
return Math.floor(min.main.temp_min);
};
// Print current weather to DOM
const printWeather = weatherData => {
const sunriseTime = convertTime(weatherData.sunrise, weatherData.timezone);
const sunsetTime = convertTime(weatherData.sunset, weatherData.timezone);
const localTime = convertTime(Date.now() / 1000, weatherData.timezone);
const roundedTemp = Math.round(weatherData.temp * 10) / 10;
weatherToday.innerHTML = `
<p class="temp-current">${roundedTemp}<span>°C</span></p>
<img
src="${setStyling(weatherData)}"
class="weather-img" />
<p class="city">${weatherData.locationName}</p>
<p class="weather-desc">
<span>${weatherData.description} </span>
<img src="https://openweathermap.org/img/wn/${
weatherData.icon
}.png" class="weather-icon"/>
</p>
<div class="local-time">
<time datetime="${localTime}" class="time">
${constructHours(localTime)}:${constructMinutes(localTime)}
</time>
</div>
<div class="sun">
<div id="sunrise">
<p class="label">sunrise</p>
<time datetime="${sunriseTime}" class="time">
${constructHours(sunriseTime)}:${constructMinutes(sunriseTime)}
</time>
</div>
<div id="sunset">
<p class="label">sunset</p>
<time datetime="${sunsetTime}" class="time">
${constructHours(sunsetTime)}:${constructMinutes(sunsetTime)}
</time>
</div>
</div>
`;
};
// Print Forecast to DOM
const printForecast = forecastData => {
const noons = getNoons(forecastData);
weatherForecast.innerHTML = "";
noons.forEach(obj => {
const maxTemp = getMaxTemp(obj, forecastData);
const minTemp = getMinTemp(obj, forecastData);
const day = setWeekday(obj.dt_txt);
weatherForecast.innerHTML += `
<div class="forecast-day">
<p class="forecast-day-label">${day}</p>
<i class="weather-icon"><img src="https://openweathermap.org/img/wn/${obj.weather[0].icon}.png"/></i>
<p class="forecast-temp">${maxTemp} / ${minTemp} °C</p>
</div>
`;
});
};
// --API'S and initiating functions
// fetch API for current weather
const fetchWeather = async (lat, lon) => {
try {
// Fetch current weather
response = await fetch(
`${API_URL}/data/2.5/weather?lat=${lat}&lon=${lon}&units=metric&appid=${APP_ID}`
);
if (!response.ok) {
throw new Error("Network response was not ok");
}
// construct json
const weatherData = await response.json();
// deconstruct to filter out only relevant data
const {
name: locationName,
main: { temp },
sys: { sunrise },
sys: { sunset },
timezone,
weather: [{ description }],
weather: [{ icon }],
} = weatherData;
printWeather({
locationName,
temp,
sunrise,
sunset,
timezone,
description,
icon,
});
} catch (error) {
printError(error);
throw new Error("Fetch error: ", error);
}
};
// fetch API for forecast
const fetchForecast = async (lat, lon) => {
try {
response = await fetch(
`${API_URL}/data/2.5/forecast?lat=${lat}&lon=${lon}&units=metric&appid=${APP_ID}`
);
if (!response.ok) {
throw new Error("Network response was not ok");
}
const forecastData = await response.json();
const {
city: { timezone },
list,
} = forecastData;
printForecast({ timezone, list });
} catch (error) {
printError(error);
throw new Error("Fetch error: ", error);
}
};
// fetch coordinates from Geocoding API
const fetchGeocode = async location => {
try {
response = await fetch(
`${API_URL}/geo/1.0/direct?q=${location}&limit=1&appid=${APP_ID}`
);
if (!response.ok) {
throw new Error("Network response was not ok");
}
const json = await response.json();
const { lat, lon } = json[0];
fetchWeather(lat, lon);
fetchForecast(lat, lon);
} catch (error) {
printError(error);
throw new Error("Fetch error: ", error);
}
};
// Check location with geolocation -"Your location"
const getLocation = () => {
return new Promise((resolve, reject) => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(resolve, reject, {
timeout: 5000,
});
} else {
throw new Error("No geolocation available");
}
});
};
// Handle Geolocation request
const handleLocal = async () => {
try {
const location = await getLocation();
lat = location.coords.latitude;
lon = location.coords.longitude;
fetchWeather(lat, lon);
fetchForecast(lat, lon);
} catch (error) {
printError(error.message);
}
};
// -- Event listeners
menuBtn.addEventListener("click", () => toggleHide(navWrapper));
menuClose.addEventListener("click", () => toggleHide(navWrapper));
scrollArrow.addEventListener("click", () => {
toggleFullscreen(body);
toggleHide(weatherForecast);
window.scrollTo({
top: window.innerHeight,
behavior: "smooth",
});
});
navGeo.addEventListener("click", () => {
setTimeout(() => toggleHide(navWrapper), 500);
handleLocal();
});
navItems.forEach(location =>
location.addEventListener("click", event => {
setTimeout(() => toggleHide(navWrapper), 500);
fetchGeocode(event.target.firstChild.nodeValue);
})
);
searchInput.addEventListener("change", event => {
setTimeout(() => {
toggleHide(navWrapper);
searchInput.value = "";
}, 500);
fetchGeocode(event.target.value);
searchInput.value = "";
});
searchBtn.addEventListener("click", event => {
setTimeout(() => {
toggleHide(navWrapper);
searchInput.value = "";
}, 500);
fetchGeocode(event.target.value);
});
// load site
fetchGeocode("Ulricehamn");