-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
188 lines (166 loc) · 5.25 KB
/
app.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
const time = document.querySelector('.time');
const timeText = document.querySelector('.time-text');
const timeSpec = document.querySelector('.time-spec');
const timeZone = document.querySelector('.time-zone');
const locationText = document.querySelector('.location-text');
const todayTemp = document.querySelector('.today-temp');
const todayIcon = document.querySelector('.today-icon');
const container = document.querySelector('.container');
const weatherDaysAll = document.querySelector('.weather-days-all');
const donut = document.querySelector('.donut');
const appController = (function () {
const setUpTheme = () => {
const time = new Date().getHours();
const changeColor = (prop) => {
timeText.style.color = prop;
timeSpec.style.color = prop;
locationText.style.color = prop;
timeZone.style.color = prop;
}
if (time < 6 || time >= 20) {
container.style.backgroundImage = `url(./Back-img/evening_01.jpeg)`;
changeColor('white');
} else if (time >= 6 && time <= 13) {
container.style.backgroundImage = `url(./Back-img/morning_01.jpeg)`;
changeColor('black');
} else if (time > 13 && time < 20) {
container.style.backgroundImage = `url(./Back-img/afternoon_02.jpeg)`
changeColor('white');
}
}
const displayTime = () => {
// Get time
let text = '';
const today = new Date();
const time = new Date().toLocaleTimeString('en-US');
const spec = time.slice(time.length - 2, time.length);
if (time.length > 10) {
text = time.slice(0, 5);
} else {
text = time.slice(0, 4)
}
// Display time
timeText.textContent = text;
timeSpec.textContent = spec.toLowerCase();
timeZone.textContent = today.toLocaleDateString(undefined, { timeZoneName: 'long' }).split(",")[1].replace(/[^A-Z]/g, '');
}
// Convert UTC to day of the week
const getDayofTheWeek = (dt) => {
var days = [
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
];
var dayNum = new Date(dt * 1000).getDay();
var result = days[dayNum];
return result;
}
// Geolocation
const getLocation = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(setPosition);
} else {
console.log("Geolocation is not supported by this browser.");
}
}
const setPosition = (position) => {
let latitude = position.coords.latitude;
let longitude = position.coords.longitude;
getWeather(latitude, longitude);
}
// Set Up Object with all data
const setUpData = (data) => {
var weatherObj = {};
var allTemp;
var allIcons;
for (let i in data.list) {
var day = getDayofTheWeek(data.list[i].dt);
allTemp = Math.floor(data.list[i].main.temp);
allIcons = data.list[i].weather[0].icon;
if (weatherObj[day] == undefined) {
weatherObj[day] = {};
weatherObj[day].temps = [];
weatherObj[day].icons = [];
}
weatherObj[day].temps.push(allTemp);
weatherObj[day].tempHigh = Math.max(...weatherObj[day].temps);
weatherObj[day].tempLow = Math.min(...weatherObj[day].temps);
weatherObj[day].icons.push(allIcons);
}
return weatherObj;
}
const displayLocation = (city, country) => {
locationText.textContent = `${city.toUpperCase()}, ${country.toUpperCase()}`;
}
const displayTodaysWeather = (temp, icon) => {
todayTemp.textContent = `${temp}°`;
todayIcon.style.backgroundImage = `url(./icons/${icon}.png)`;
}
const displayAllWeather = (day, { icons, tempHigh, tempLow }) => {
donut.style.display = "none";
const weather = document.createElement('div');
weather.className = "weather-days";
weather.insertAdjacentHTML(
"beforeend",
`
<h3 class="day-text">${day}</h3>
<div class="day-icon">
<img src="./icons/${icons[2]}.png" alt="weather-icon" />
</div>
<p class="day-temp-max">${tempHigh}°</p>
<p class="day-temp-min">${tempLow}°</p>
`
);
weatherDaysAll.insertAdjacentElement("beforeend", weather);
}
const todayIs = () => {
const today = new Date().getDay();
const days = [
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
];
return days[today];
}
const refreshTime = () => {
return setInterval(displayTime, 1000);
}
//Work with API
async function getWeather(latitude, longitude) {
const key = `494a76302024db23035d814ee5934e4e`;
try {
const result = await fetch(
`https://api.openweathermap.org/data/2.5/forecast?lat=${latitude}&lon=${longitude}&units=metric&appid=${key}`
);
const data = await result.json();
const weather = setUpData(data);
const cityName = data.city.name;
const country = data.city.country;
const todayTemp = Math.floor(data.list[0].main.temp);
const todayIcon = data.list[0].weather[0].icon;
for (let [key, value] of Object.entries(weather)) {
if (key != todayIs()) {
displayAllWeather(key, value);
}
}
displayLocation(cityName, country);
displayTodaysWeather(todayTemp, todayIcon)
} catch (error) {
console.log(error);
}
}
function init() {
getLocation();
setUpTheme();
refreshTime()
}
init();
})();