forked from Technigo/project-weather-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
131 lines (112 loc) · 4.6 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
// DOM selectors
const currentWeather = document.getElementById("weather-container");
const cityDropdown = document.getElementById("city-dropdown");
const displayForecast = document.getElementById("weather-forecast");
// API
const API_BASE_URL = "https://api.openweathermap.org/data/2.5";
const API_KEY = "a1f68c4f65b632802ca0dd3405694457";
// UNIX TIMESTAMP CONVERTER
const convertDateTime = (unixTimestamp, timezoneOffset) => {
const adjustedTimestamp = unixTimestamp + timezoneOffset;
const date = new Date(adjustedTimestamp * 1000);
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
return `${hours}:${minutes}`;
};
// WEEKDAY DISPLAY FUNCTION
const getWeekdays = date => {
const weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
return weekdays[date.getDay()];
};
// TODAY'S WEATHER
const printWeather = async weatherURL => {
try {
const weatherNow = await fetch(weatherURL);
const data = await weatherNow.json();
const currentTemp = data.main.temp.toFixed(1);
const currentLocation = data.name;
const currentCondition = data.weather[0].description;
const icon = data.weather[0].icon;
const todaySunrise = convertDateTime(data.sys.sunrise, data.timezone);
const todaySunset = convertDateTime(data.sys.sunset, data.timezone);
const ICON_URL = `https://openweathermap.org/img/wn/${icon}@2x.png`;
const currentTimezoneOffset = data.timezone;
const currentTime = new Date(
new Date().getTime() + currentTimezoneOffset * 1000
);
const hours = currentTime.getHours();
let backgroundClass;
if (hours >= 18 || hours < 6) {
backgroundClass = "night-background";
} else {
backgroundClass = "day-background";
}
const currentWeather = document.getElementById("current-weather");
const currentHeader = document.querySelector(".header");
const currentDropdown = document.getElementById("city-dropdown");
currentWeather.className = backgroundClass;
currentHeader.className = `header ${backgroundClass}`;
currentDropdown.className = backgroundClass;
currentWeather.innerHTML = `
<div id="today-weather-backgroundr">
<p class="temp">${currentTemp}<span>°C</span></p>
<h1 class="location">${currentLocation}</h1>
<p class="condition">${currentCondition} <img class="icon" src="${ICON_URL}"></p>
<div class="sun-time">
<p class="sunrise">Sunrise ${todaySunrise}</p>
<p class="sunset">Sunset ${todaySunset}</p>
</div>
</div>`;
} catch (error) {
console.error(error); //in case of an error
}
};
// 4-DAY WEATHER FORECAST
const printForecast = async forecastURL => {
try {
const fourDayWeather = await fetch(forecastURL);
const json = await fourDayWeather.json();
const forecastData = json.list
.filter((item, index) => (index - 1) % 8 === 0)
.slice(0, 4);
forecastData.forEach((forecast, index) => {
const startIdx = index * 8;
const endIdx = startIdx + 8;
const temperatures = json.list
.slice(startIdx, endIdx)
.map(item => item.main.temp);
const lowestTemp = Math.min(...temperatures).toFixed(0);
const highestTemp = Math.max(...temperatures).toFixed(0);
const forecastDate = new Date(forecast.dt * 1000);
const tomorrowDate = new Date(forecastDate.getTime() + (1000 * 60 * 60 * 24));
const weekday = getWeekdays(tomorrowDate);
const tomorrowWeekday = (weekday === 6) ? 0 : weekday;
const icon = forecast.weather[0].icon;
const forecastElement = document.createElement("div");
forecastElement.innerHTML = `
<div class="forecast-display">
<p>${tomorrowWeekday}</p>
<img class="icon" src="https://openweathermap.org/img/wn/${icon}@2x.png">
<p>${lowestTemp}° / ${highestTemp}°C</p>
</div>`;
displayForecast.appendChild(forecastElement);
});
} catch (error) {
console.error(error);
}
};
// EVENTLISTENER
cityDropdown.addEventListener("change", () => {
const newCity = cityDropdown.value;
const newWeatherURL = `${API_BASE_URL}/weather?q=${newCity}&units=metric&appid=${API_KEY}`;
const newForecastURL = `${API_BASE_URL}/forecast?q=${newCity}&units=metric&appid=${API_KEY}`;
currentWeather.innerHTML = "";
displayForecast.innerHTML = "";
printWeather(newWeatherURL);
printForecast(newForecastURL);
});
// Initial call
const initialWeatherURL = `${API_BASE_URL}/weather?q=bern&units=metric&appid=${API_KEY}`;
const initialForecastURL = `${API_BASE_URL}/forecast?q=bern&units=metric&appid=${API_KEY}`;
printWeather(initialWeatherURL);
printForecast(initialForecastURL);