forked from Technigo/project-weather-app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
273 lines (231 loc) · 14.9 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
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
//All global varibles
const tempToday = document.getElementById('tempToday')
const tempTextCelsius = document.querySelector('.tempTextCelsius')
const cityName = document.getElementById('cityName')
const localTime = document.getElementById('localTime');
const weatherDescription = document.getElementById('weatherDescription')
const mainIcon = document.getElementById('mainIcon')
const sunriseText = document.getElementById('sunriseText');
const sunsetText = document.getElementById('sunsetText');
const weatherFeature = document.getElementById('weatherFeature')
const featureImage = document.querySelector('.feature-image');
const backgroundTopGradient = document.getElementById('backgroundTopGradient');
const searchMenuBtn = document.getElementById('searchMenuBtn');
const closeSearchMenu = document.getElementById('closeSearchMenu');
const searchBtn = document.getElementById('searchBtn');
const inputField = document.getElementById('inputField');
const switchFavoriteCity = document.getElementById('switchBtn')
const weatherForecast = document.getElementById('weatherForecast')
const forecastWeekdays = document.getElementById('forecastWeekdays')
const forecastIcon = document.getElementById('forecastIcon')
const forecastDescription = document.getElementById('forecastDescription')
const forecastTemp = document.getElementById('forecastTemp')
const forecastWind = document.getElementById('forecastWind')
const cityText = document.querySelector('.cityText');
const tempText = document.querySelector('.tempText');
const describeText = document.querySelector('.describeText');
//Variables we can use later to automate API-fethcing:
const apiKey = 'c480de5f69ca98d1993a4dae3213642e';
let city = 'Stockholm';
const todaysWeatherFeature = (city) => {
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&APPID=${apiKey}`)
.then((response) => {
return response.json();
})
.then((json) => {
//Get today's weather
let { icon } = json.weather[0];
cityName.innerText = `${json.name}`;
tempToday.innerText = `${json.main.temp.toFixed(0)}`;
tempTextCelsius.innerText = `°C`;
weatherDescription.innerText = `${json.weather[0].description}`;
mainIcon.innerHTML = `<img src="http://openweathermap.org/img/wn/${icon}@2x.png" alt="weather icon" class="main-icon">`
// Get current time (hours/minutes) in time zome
const currentLocalTime = new Date((json.dt+json.timezone) * 1000);
const localTimeValue = currentLocalTime.toLocaleTimeString(['en-GB'], { timeStyle: 'short', timeZone:'UTC'});
//Modifying the HTML based on the output of current time in hours/minutes
localTime.innerHTML = `Time: ${localTimeValue}`;
// Get sunrise and sunset time with the city's timezone
const sunriseTime = new Date((json.sys.sunrise + json.timezone) * 1000); //Gives us the time in "human" form (as a date), mult. by 1000 to get it in ms.
sunriseTime.setMinutes(sunriseTime.getMinutes() + sunriseTime.getTimezoneOffset())
const sunriseShort = sunriseTime.toLocaleTimeString(['en-GB'], { timeStyle: 'short' }); //Transforms it into just the Hour/minutes. Select the short variant to get the time with minutes and not seconds.
const sunsetTime = new Date((json.sys.sunset + json.timezone) * 1000);
sunsetTime.setMinutes(sunsetTime.getMinutes() + sunsetTime.getTimezoneOffset())
const sunsetShort = sunsetTime.toLocaleTimeString(['en-GB'], { timeStyle: 'short' });
//Modifying the HTML based on our input:
sunriseText.innerHTML = `<p>sunrise</p>
<p class="time-data">${sunriseShort}</p>`;
sunsetText.innerHTML = `<p>sunset</p>
<p class="time-data">${sunsetShort}</p>`;
//Today's weather varible
const todaysWeather = json.weather[0].main
//Get current local time with timezone
const getTime = new Date()
getTime.setMinutes(getTime.getMinutes() + getTime.getTimezoneOffset())
getTime.setSeconds(getTime.getSeconds() + json.timezone)
//Get time in hours to compare
const currentTime = getTime.getHours();
const sunriseTimeHour = sunriseTime.getHours();
const sunsetTimeHour = sunsetTime.getHours();
//Image feature
if (currentTime >= sunriseTimeHour && currentTime <= sunsetTimeHour) {
//During daytime => show image depending on weather
if (todaysWeather === 'Clear') {
featureImage.style.backgroundImage = "url('https://images.unsplash.com/photo-1613931189161-1f4d2660bd1e?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2Nzc0MDczNDI&ixlib=rb-4.0.3&q=80')"
} else if (todaysWeather === 'Clouds') {
featureImage.style.backgroundImage = "url('https://images.unsplash.com/photo-1424111113808-b7be56a9f3d6?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2NzcyNTczNTA&ixlib=rb-4.0.3&q=80')"
} else if (todaysWeather === 'Rain') {
featureImage.style.backgroundImage = "url('https://images.unsplash.com/photo-1424111113808-b7be56a9f3d6?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2NzcyNTczNTA&ixlib=rb-4.0.3&q=80')"
} else if (todaysWeather === 'Snow') {
featureImage.style.backgroundImage = "url('https://images.unsplash.com/photo-1610486549369-585a0e6d8cc4?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2Nzc0MDc4NDE&ixlib=rb-4.0.3&q=80')"
} else if (todaysWeather === 'Thunderstorm') {
featureImage.style.backgroundImage = "url('https://images.unsplash.com/photo-1602088501827-7912e1b4a7bd?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2Nzc0MDc5ODI&ixlib=rb-4.0.3&q=80')"
} else if (todaysWeather === 'Drizzle') {
featureImage.style.backgroundImage = "url('https://images.unsplash.com/photo-1554039362-6daf559ddb63?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2Nzc0MDgyNDk&ixlib=rb-4.0.3&q=80')"
} else if (todaysWeather === 'Atmosphere' || todaysWeather === 'Haze') {
featureImage.style.backgroundImage = "url('https://images.unsplash.com/photo-1543226549-10d29b2cfaf0?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2Nzc0MDg0MTI&ixlib=rb-4.0.3&q=80')"
} else {
featureImage.style.backgroundImage = "url('https://images.unsplash.com/photo-1424111113808-b7be56a9f3d6?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2NzcyNTczNTA&ixlib=rb-4.0.3&q=80')"
}
} else {
//During nighttime => show image depending on weather
if (todaysWeather === 'Clear') {
featureImage.style.backgroundImage = "url('https://images.unsplash.com/photo-1620055374842-145f66ec4652?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2Nzc0MDkxMTY&ixlib=rb-4.0.3&q=80')"
} else if (todaysWeather === 'Snow') {
featureImage.style.backgroundImage = "url('https://images.unsplash.com/photo-1602857731804-80e82120ff27?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2Nzc0MDkzNzQ&ixlib=rb-4.0.3&q=80')"
} else if (todaysWeather === 'Rain') {
featureImage.style.backgroundImage = "url('https://images.unsplash.com/photo-1505144992585-d281c0e2cff8?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2Nzc0MDk1OTA&ixlib=rb-4.0.3&q=80')"
} else if (todaysWeather === 'Clouds') {
featureImage.style.backgroundImage = "url('https://images.unsplash.com/photo-1518352724948-729151797553?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2Nzc0MDk1OTA&ixlib=rb-4.0.3&q=80')"
} else {
featureImage.style.backgroundImage = "url(' https://images.unsplash.com/photo-1604083142449-79b1babd12d4?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2Nzc0MTIwOTM&ixlib=rb-4.0.3&q=80')"
};
}
const gradientDayNight = () => {
//Remove all classes to be added later
const gradientStyle = ['morning', 'midday', 'afternoon', 'duskSunset', 'evening', 'midnight', 'dawnSunrise']
backgroundTopGradient.classList.remove(...gradientStyle)
if (currentTime > 7 && currentTime <= 11) {
backgroundTopGradient.classList.add('morning');
} else if (currentTime > 11 && currentTime <= 14) {
backgroundTopGradient.classList.add('midday');
} else if (currentTime > 14 && currentTime < 17) {
backgroundTopGradient.classList.add('afternoon');
} else if (currentTime >= 17 && currentTime <= 19) {
backgroundTopGradient.classList.add('duskSunset');
} else if (currentTime > 19 && currentTime <= 23) {
backgroundTopGradient.classList.add('evening');
} else if (currentTime > 23 || currentTime < 5) {
backgroundTopGradient.classList.add('midnight');
} else {
backgroundTopGradient.classList.add('dawnSunrise');
}
}
gradientDayNight()
})
};
const weatherForecastData = (city) => {
fetch(
`https://api.openweathermap.org/data/2.5/forecast?q=${city}&units=metric&APPID=${apiKey}`
)
.then((forecastResponse) => {
return forecastResponse.json();
})
.then((result) => {
if (result.cod !== '404') { //If the user did NOT search for a city that does not exist
const todaysDate = new Date().toString().split(' ')[0]; //Today's date in text form
const filterData = result.list.filter(weatherDay => weatherDay.dt_txt.includes('12:00')); //Filters out the data at 12:00 every day
filterData.forEach(date => {
const weekDay = new Date(date.dt * 1000).toString().split(' ')[0]; //All the five days dates' convertet from numbers to text
if (weekDay !== todaysDate) {
let {icon} = date.weather[0];
forecastWeekdays.innerHTML += `<p>${weekDay}</p>`
forecastIcon.innerHTML += `<img src="http://openweathermap.org/img/wn/${icon}@2x.png" alt="weather icon" class="weather-icons">`
forecastTemp.innerHTML += `<p>${date.main.temp.toFixed(0)}°C</p>`
forecastWind.innerHTML += `<p>${date.wind.speed}m/s</p>`
}
});
} else { //If the user DID search for a city that does not exist. Alerts user that the city cant be found. Then runs Stockholm again.
alert('Oops, city not found! Check your spelling please.');
todaysWeatherFeature('Stockholm');
weatherForecastData('Stockholm');
}
})
};
const toggleSearchField = () => {
//This just controls the toggling between opening and closing the search field
const searchToggler = document.getElementById('search-toggler');
searchToggler.classList.toggle('hidden');
closeSearchMenu.classList.toggle('hidden');
searchMenuBtn.classList.toggle('hidden');
};
const searchFunction = () => {
//This is for storing the user input from the search and pushing it into our fetching weather function later on
let searchedCity = inputField.value
//Use the city searched and inject into ""fetchWeather""" function:
//weather.fetchWeather(searchedCity); //Skriv om till den vi använder
todaysWeatherFeature(searchedCity);
weatherForecastData(searchedCity);
//Clears field & hides the input field:
inputField.value = "";
//Reset the weather forecast:
forecastWeekdays.innerHTML = "";
forecastIcon.innerHTML = "";
forecastWind.innerHTML = "";
forecastTemp.innerHTML = "";
};
const getNextCity = () => {
//Change default favorite city
//Reset the weather forecast:
forecastWeekdays.innerHTML = "";
forecastIcon.innerHTML = "";
forecastTemp.innerHTML = "";
forecastWind.innerHTML = "";
if (city === 'Stockholm') {
todaysWeatherFeature('Madrid')
weatherForecastData('Madrid');
city = 'Madrid'
} else if (city === 'Madrid') {
todaysWeatherFeature('Singapore')
weatherForecastData('Singapore');
city = 'Singapore'
} else if (city === 'Singapore') {
todaysWeatherFeature('San Francisco')
weatherForecastData('San Francisco');
city = 'San Francisco'
} else if (city === 'San Francisco') {
todaysWeatherFeature('Cairo')
weatherForecastData('Cairo');
city = 'Cairo'
} else if (city === 'Cairo') {
todaysWeatherFeature('Sidney')
weatherForecastData('Sidney');
city = 'Sidney'
} else if (city === 'Sidney') {
todaysWeatherFeature('Bogota')
weatherForecastData('Bogota');
city = 'Bogota'
} else {
todaysWeatherFeature('Stockholm')
weatherForecastData('Stockholm');
city = 'Stockholm'
}
};
// Start:
todaysWeatherFeature('Stockholm');
weatherForecastData('Stockholm');
//All global eventListeners
//Eventlistener to invoke getNextCity function
switchBtn.addEventListener('click', getNextCity)
//Eventlistener to toggle search field:
searchMenuBtn.addEventListener('click', toggleSearchField)
closeSearchMenu.addEventListener('click', toggleSearchField)
//Eventlistener to search through enter key also
searchBtn.addEventListener('click', searchFunction)
//Eventlistener to search through enter key also
inputField.addEventListener('keyup', function (event) {
if (event.key == "Enter") {
searchFunction();
}
}
);