forked from faypi/project-weather-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
234 lines (216 loc) · 7.96 KB
/
Copy pathscript.js
File metadata and controls
234 lines (216 loc) · 7.96 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
const weatherContainer = document.getElementById("weatherContainer");
const today = document.getElementById("today");
const temperature = document.getElementById("temperature");
const city = document.getElementById("city");
const weatherDescription = document.getElementById("weatherDescription");
const weatherForecast = document.getElementById("weatherForecast");
const sunContainer = document.getElementById("sunContainer");
const mainContainer = document.getElementById("mainContainer");
const timeInHr = new Date().getHours();
const API_URL =
"https://api.openweathermap.org/data/2.5/weather?q=Stockholm,Sweden&units=metric&APPID=856500266ed2a8bc92cf454b0800d15c";
const API_Weather_URL =
"https://api.openweathermap.org/data/2.5/forecast?q=Stockholm,Sweden&units=metric&APPID=856500266ed2a8bc92cf454b0800d15c";
const london =
"https://api.openweathermap.org/data/2.5/weather?q=London&units=metric&APPID=856500266ed2a8bc92cf454b0800d15c";
const dubai =
"https://api.openweathermap.org/data/2.5/weather?q=Dubai&units=metric&APPID=856500266ed2a8bc92cf454b0800d15c";
const bangkok =
"https://api.openweathermap.org/data/2.5/weather?q=Bangkok&units=metric&APPID=856500266ed2a8bc92cf454b0800d15c";
const sydney =
"https://api.openweathermap.org/data/2.5/weather?q=Sydney&units=metric&APPID=856500266ed2a8bc92cf454b0800d15c";
// Fetching the stockholm weather
let timezone;
fetch(API_URL) //this is when we send something to BE
.then((res) => res.json()) //this is when we receive the data from BE
.then((data) => {
const icon = data.weather[0].icon;
const temp = data.main.temp.toFixed(0);
weatherContainer.innerHTML = `
<div class= "icon-day-or-night" id="iconDayOrNight"></div>
<div class="temperature" id="temperature">${temp}°C</div>
<div class="cityToday" id="city">${data.name} </div>
<div class= "weather-description id="weatherDescription">${data.weather[0].description} </div>
`;
/* sunrise & sunset */
const sunriseSec = data.sys.sunrise;
const sunsetSec = data.sys.sunset;
timezone = data.timezone;
const sunrise = convertUTCToSunTime(sunriseSec, data.timezone);
const sunset = convertUTCToSunTime(sunsetSec, data.timezone);
sunContainer.innerHTML = `
<div class="sunrise" id="sunRise" > Sunrise ${sunrise} </div>
<div class="sunset" id="sunSet"> Sunset ${sunset}</div>
`;
const iconDayOrNight = document.getElementById("iconDayOrNight")
if (timeInHr >= 6 && timeInHr <= 17) {
mainContainer.style.background = "linear-gradient(90deg, #8a8dff, #9daaff)";
iconDayOrNight.innerHTML= `
<img src="/images/sun (1).png" alt="daytime">
`
} else {
mainContainer.style.background = "linear-gradient(180deg, #242552, #7a7dc9)";
iconDayOrNight.innerHTML= `
<img src="/images/moon.png" alt="night time">
`
}
});
// setting bg Image based on day/night
fetch(API_Weather_URL)
.then((res) => res.json())
.then((data) => {
const dataOfFiveDays = data.list;
const daysFromData = dataOfFiveDays.map((data) => {
return convertUTCToDate(data.dt);
});
const uniqueDays = [...new Set(daysFromData)];
//step 2. get Weekdays
const weekdays = uniqueDays.map((day) =>
new Date(day).toLocaleDateString("en-GB", { weekday: "short" })
);
//step 3. split data based on dates
const dayOne = splitDataByDay(0);
const dayTwo = splitDataByDay(1);
const dayThree = splitDataByDay(2);
const dayFour = splitDataByDay(3);
const dayFive = splitDataByDay(4);
function splitDataByDay(uniqueDayIndex) {
return dataOfFiveDays.filter((data) => {
const date = convertUTCToDate(data.dt);
if (date === uniqueDays[uniqueDayIndex]) {
return data;
}
});
}
//stpe 3. get Weather icon by current hour
const dayOneIcon = getIcon(dayOne);
const dayTwoIcon = getIcon(dayTwo);
const dayThreeIcon = getIcon(dayThree);
const dayFourIcon = getIcon(dayFour);
const dayFiveIcon = getIcon(dayFive);
const iconsArr = [
dayOneIcon,
dayTwoIcon,
dayThreeIcon,
dayFourIcon,
dayFiveIcon,
];
function getIcon(dataOfDay) {
let icon;
const currHour = new Date().getHours();
dataOfDay.forEach((data) => {
const dataHour = convertUTCToHours(data.dt);
if (dataHour <= currHour && dataHour + 3 > currHour) {
icon = data.weather[0].icon;
}
});
if (!icon) {
icon = dataOfDay[0].weather[0].icon;
}
return icon;
}
//step 4. get Min max temperature of each day
const dayOneMinMax = getMinMax(dayOne);
const dayTwoMinMax = getMinMax(dayTwo);
const dayThreeMinMax = getMinMax(dayThree);
const dayFourMinMax = getMinMax(dayFour);
const dayFiveMinMax = getMinMax(dayFive);
function getMinMax(dataOfDay) {
let minTemp = [];
let maxTemp = [];
dataOfDay.forEach((data) => {
minTemp.push(data.main.temp_min);
maxTemp.push(data.main.temp_max);
});
const minTemperature = Math.min(...minTemp).toFixed(0);
const maxTemperature = Math.max(...maxTemp).toFixed(0);
return { maxTemperature, minTemperature };
}
const minMaxArr = [
dayOneMinMax,
dayTwoMinMax,
dayThreeMinMax,
dayFourMinMax,
dayFiveMinMax,
];
//step 5. display
for (let i = 0; i < 5; i++) {
const { maxTemperature, minTemperature } = minMaxArr[i];
weatherForecast.innerHTML += `
<div class="weekly-weather">
<span class="weekday">${weekdays[i]}</span>
<img class="temperature-icon"src="http://openweathermap.org/img/wn/${iconsArr[i]}.png" alt="weather icon"/>
<span class="temperature2">${maxTemperature} ° / ${minTemperature} °C</span>
</div>
`;
}
});
function convertUTCToSunTime(UTCsec, timezone) {
const UTCstring = new Date(
(UTCsec + timezone + new Date().getTimezoneOffset() * 60) * 1000
).toTimeString();
const timeWithSec = UTCstring.split(":");
return `${timeWithSec[0]}: ${timeWithSec[1]}`;
}
function convertUTCToDate(UTCsec) {
const UTCstring = new Date(
(UTCsec + timezone + new Date().getTimezoneOffset() * 60) * 1000
).toDateString();
return UTCstring;
}
function convertUTCToHours(UTCsec) {
const UTCstring = new Date(
(UTCsec + timezone + new Date().getTimezoneOffset() * 60) * 1000
).getHours();
return UTCstring;
}
// Different cities
fetch(london)
.then((response) => response.json())
.then((data2) => {
const todaysTemp = data2.main.temp_max.toFixed(0)
console.log('london', data2)
otherCity.innerHTML += `
<div class="city-weather">
<p class="city" id="london">${data2.name}</p>
<p class="city" id="london">${todaysTemp}°C</p>
<p class="city" id="london">${data2.weather[0].description}</p>
</div>
`
})
fetch(dubai)
.then((response) => response.json())
.then((data3) => {
const todaysTemp = data3.main.temp_max.toFixed(0)
console.log('dubai', data3)
otherCity.innerHTML += `
<div class="city-weather">
<p class="city" id="dubai">${data3.name}</p>
<p class="city" id="dubai" >${todaysTemp}°C</p>
<p class="city" id="dubai">${data3.weather[0].description}</p>
</div>`
})
fetch(bangkok)
.then((response) => response.json())
.then((data4) => {
const todaysTemp = data4.main.temp_max.toFixed(0)
console.log('bangkok', data4)
otherCity.innerHTML += `
<div class="city-weather">
<p class="city" id="bangkok">${data4.name}</p>
<p class="city" id="bangkok">${todaysTemp}°C</p>
<p class="city" id="bangkok">${data4.weather[0].description}</p>
</div>`
})
fetch(sydney)
.then((response) => response.json())
.then((data5) => {
const todaysTemp = data5.main.temp_max.toFixed(0)
console.log('sydney', data5)
otherCity.innerHTML += `
<div class="city-weather">
<p class="city" id="sydney">${data5.name}</p>
<p class="city" id="sydney">${todaysTemp}°C</p>
<p class="city" id="sydney">${data5.weather[0].description}</p>
</div>`
})