-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
515 lines (359 loc) · 15.4 KB
/
index.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
let city
let state
// Nav Bar Screen Pops Up halway through page
const navBar = document.querySelector("nav")
const inputDiv = document.querySelector("#input-div")
navBar.style.display = "none"
window.addEventListener("scroll", () => {
if (window.scrollY > 500) {
navBar.style.display = "block"
} else if (window.scrollY < 500) {
navBar.style.display = "none"
}
})
//Miami, Florida, United States
//Page Scroll from header to body
function pageScroll() {
setTimeout(() => {
document.getElementById("top-body-container").scrollIntoView({
behavior: "smooth", block: "end", inline: "nearest"
});
}, 7000
)
}
// Get the inputs of the location from the user
const addSubmitListener = () => {
const locationForm = document.querySelector('#location-input')
const weatherDisplay = document.querySelector(".weather-display")
const city_state = document.querySelector("#city-state h5")
const display_location = document.querySelector(".display-location")
locationForm.addEventListener('submit', (event) => {
event.preventDefault()
weatherDisplay.innerHTML = ""
let locationInput = event.target.location.value
let parseLocationInput = locationInput.split(', ') // Separate the string to have city, state, and country as separate elements in an array
city = parseLocationInput[0] // Save the city value to the city global variable
state = parseLocationInput[1] // Save the state value to the state global variable
city_state.textContent = `${city}, ${state}`// Add the city and state to the city-state element on the html file
display_location.textContent = `${city}, ${state}`// Add the city and state to the display location element on the html file
getCoordinates(parseLocationInput[0], parseLocationInput[1], parseLocationInput[2])
event.target.reset();
// Header Transition
inputDiv.style.display = "none"
pageScroll()
})
}
// Get the input to change the location in the navigation bar
const addChangeLocationSubmitListener = () => {
const changeLocationForm = document.querySelector('#change-location')
const weatherDisplay = document.querySelector(".weather-display")
const city_state = document.querySelector("#city-state")
const display_location = document.querySelector(".display-location")
changeLocationForm.addEventListener('submit', (event) => {
event.preventDefault()
weatherDisplay.innerHTML = ""
let locationInput = event.target.search.value
let parseLocationInput = locationInput.split(', ') // Separate the string to have city, state, and country as separate elements in an array
city = parseLocationInput[0] // Save the city value to the city global variable
state = parseLocationInput[1] // Save the state value to the state global variable
city_state.textContent = `${city}, ${state}`// Add the city and state to the city-state element on the html file
display_location.textContent = `${city}, ${state}`// Add the city and state to the display location element on the html file
getCoordinates(parseLocationInput[0], parseLocationInput[1], parseLocationInput[2])
event.target.reset();
})
}
// Get the latitude and longitude coordinates from the Weather API
const getCoordinates = (city, state, country) => {
let cityName = city.split(' ').join('+')
fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${cityName}&count=10&language=en`)
.then(response => {
// if response have a succcessful status code
if (response.ok) {
return response.json() // gets returned to the next .then
} else {
alert("Something went wrong")
}
})
.then(geocoding_data => {
geocoding_data.results.forEach(data => {
// Get the latitude and longitude of the location is in the United States
if ((data.name === city) && (data.admin1 === state)) {
getWeatherCode(data.latitude, data.longitude)
// Get the latitude and longitude of the location outside of the United States
} else if ((data.name === city) && (data.country === country) && (data.country_code !== "US")) {
getWeatherCode(data.latitude, data.longitude)
}
})
})
.catch(error => alert("Make sure to have a comma and space after city and state"))
}
// Get weather code base on the latitude and longitude from the weather api
const getWeatherCode = (latitude, longitude) => {
fetch(`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t=weather_code&temperature_unit=fahrenheit&forecast_days=1`)
.then(response => {
// if response have a succcessful status code
if (response.ok) {
return response.json() // gets returned to the next .then
} else {
alert("Something went wrong")
}
})
.then(weather_data => {
displayWeatherCondition(weather_data.current.weather_code)
getWeatherMood(weather_data.current.weather_code)
}
)
.catch(error => alert(error))
}
// Display the weather condition base on the weather code on the webpage
const displayWeatherCondition = (weather_code) => {
const weatherDisplay = document.querySelector(".weather-display")
const weatherMessage = document.querySelector('#start-screen-text')
let weatherImage = document.createElement("img")
let weatherImageTwo = document.createElement("img")
let weatherImageThree = document.createElement("img")
let weatherCondition
if (weather_code === 0) {
weatherImage.src = "./weather-icons-master/production/fill/all/clear-day.svg"
weatherCondition = "Sunny"
} else if ((weather_code >= 1) && (weather_code <= 3)) {
weatherImage.src = "./weather-icons-master/production/fill/all/partly-cloudy-day.svg"
weatherCondition = "Partly Cloudy"
} else if (weather_code === 4) {
weatherImage.src = "./weather-icons-master/production/fill/all/smoke.svg"
weatherCondition = "Smoke"
} else if (weather_code === 5) {
weatherImage.src = "./weather-icons-master/production/fill/all/haze.svg"
weatherCondition = "Haze"
} else if ((weather_code >= 6) && (weather_code <= 9)) {
weatherImage.src = "./weather-icons-master/production/fill/all/dust-wind.svg"
weatherCondition = "Dust Wind"
} else if ((weather_code >= 10) && (weather_code <= 11)) {
weatherImage.src = "./weather-icons-master/production/fill/all/mist.svg"
weatherCondition = "Mist"
} else if ((weather_code >= 12) && (weather_code <= 13)) {
weatherImage.src = "./weather-icons-master/production/fill/all/partly-cloudy-day.svg"
weatherCondition = "Partly Cloudy"
} else if ((weather_code >= 14) && (weather_code <= 16)) {
weatherImage.src = "./weather-icons-master/production/fill/all/partly-cloudy-rain.svg"
weatherCondition = "Partly Cloudy Rain"
} else if (weather_code === 17) {
weatherImage.src = "./weather-icons-master/production/fill/all/thunderstorms.svg"
weatherCondition = "Thunderstorms"
} else if ((weather_code >= 18) && (weather_code <= 19)) {
weatherImage.src = "./weather-icons-master/production/fill/all/overcast.svg"
weatherCondition = "Overcast"
} else if (((weather_code >= 20) && (weather_code <= 29)) || (((weather_code >= 60) && (weather_code <= 69))) || ((weather_code >= 78) && (weather_code <= 94))) {
weatherImage.src = "./weather-icons-master/production/fill/all/rain.svg"
weatherCondition = "Rain"
} else if ((weather_code >= 30) && (weather_code <= 39)) {
weatherImage.src = "./weather-icons-master/production/fill/all/dust-wind.svg"
weatherCondition = "Dust Wind"
} else if ((weather_code >= 40) && (weather_code <= 49)) {
weatherImage.src = "./weather-icons-master/production/fill/all/fog.svg"
weatherCondition = "Fog"
} else if ((weather_code >= 50) && (weather_code <= 59)) {
weatherImage.src = "./weather-icons-master/production/fill/all/drizzle.svg"
weatherCondition = "Dizzle"
} else if ((weather_code >= 70) && (weather_code <= 77)) {
weatherImage.src = "./weather-icons-master/production/fill/all/snow.svg"
weatherCondition = "Snow"
} else if ((weather_code >= 95) && (weather_code <= 99)) {
weatherImage.src = "./weather-icons-master/production/fill/all/thunderstorms.svg"
weatherCondition = "Thunderstorms"
}
weatherImageTwo.src = weatherImage.src
weatherImageThree.src = weatherImage.src
weatherImage.className = "weather-image"
weatherImageTwo.className = "weather-image-two"
weatherImageThree.className = "weather-image-three"
weatherMessage.textContent = `It look like it is ${weatherCondition} in ${city}!`
weatherDisplay.append(weatherImage)
weatherDisplay.append(weatherImageTwo)
weatherDisplay.append(weatherImageThree)
}
// Moods Moods: Somber, Excited, Content, Calming , Hopeful , Nostalgic
// Get the mood base on the weather code
const getWeatherMood = (weather_code) => {
let mood
if ((weather_code >= 0) && (weather_code <= 3)) {
mood = "Excited"
} else if (((weather_code >= 4) && (weather_code <= 10)) || (((weather_code >= 17) && (weather_code <= 19))) || ((weather_code >= 41) && (weather_code <= 49))) {
mood = "Calming"
} else if (((weather_code >= 11) && (weather_code <= 16)) || (((weather_code >= 70) && (weather_code <= 77)))) {
mood = "Content"
} else if (((weather_code >= 20) && (weather_code <= 29)) || (((weather_code >= 60) && (weather_code <= 69))) || ((weather_code >= 78) && (weather_code <= 94))) {
mood = "Nostalgic"
} else if (((weather_code >= 30) && (weather_code <= 39)) || (((weather_code >= 95) && (weather_code <= 99)))) {
mood = "Somber"
} else if (((weather_code >= 50) && (weather_code <= 59))) {
mood = "Hopeful"
}
getSong(mood)
}
const getSong = (mood) => {
fetch("http://localhost:3000/songs")
.then(response => {
if (response.ok) {
return response.json()
}
else {
alert("Something went wrong")
}
})
.then(song => {
// moodList is a list of songs --> Title,Name,Image,Url
const moodList = [];
song.map(data => {
// console.log(data.mood[0]);
if (data.mood[0] === mood) {
const songObj = {
songTitle: data.songTitle,
artist: data.artist,
image: data.image,
url: data.url.youtube,
favorite: data.favorite,
id: data.id
}
moodList.push(songObj);
}
})
const songChoice = getRandom(moodList);
//songChoice is a selected song song
console.log(songChoice, "songChoice");
centerDisplay(songChoice);
})
.catch(error => alert(`error with get song fetch --> ${error}`))
}
function getRandom(list) {
const randomNumber = Math.floor(Math.random() * list.length);
return list[randomNumber];
}
function playMusic(e, data) {
e.preventDefault()
e.stopPropagation()
console.log("Playbutton clicked")
window.open(`${data.url} , 'blank'`)
}
function playMusicPlaylist(e, data) {
e.preventDefault()
e.stopPropagation()
console.log("Playbutton clicked")
window.open(`${data.url.youtube} , 'blank'`)
}
//this function renders an song to the target div #ID,
//assuming target div is a string
function renderSong(song, targetDiv) {
const selectedDiv = document.getElementById(targetDiv)
const button = document.createElement("button")
const playButton = document.createElement('div')
const playButtonIcon = document.createElement("img")
const text = document.createElement("div")
const title = document.createElement("h4")
const songArtist = document.createElement("h4")
const favoriteButton = document.createElement("img")
button.id = "suggested-song";
button.className = "song-button";
playButton.id = "stock-album-cover";
playButtonIcon.src = "./blackYtLogo.png"
playButtonIcon.id = "yt-logo"
text.id = "song-text";
title.textContent = song.songTitle;
songArtist.textContent = song.artist;
favoriteButton.src = ""
favoriteButton.id = "favorite-button"
songArtist.value = song.artist;
selectedDiv.appendChild(button);
button.appendChild(playButton);
playButton.appendChild(playButtonIcon)
button.appendChild(text);
text.appendChild(title);
text.appendChild(songArtist);
button.appendChild(favoriteButton)
console.log(song, "<----this is the renderSong Obj")
// Shows correct like image depending on state
song.favorite === true ? favoriteButton.src = "./favoriteFilled.png" : favoriteButton.src = "./favoriteEmpty.png"
// On click send to youtube
playButton.addEventListener("click", (e) => playMusic(e, song))
favoriteButton.addEventListener("click", (e) => postFavorite(e, song))
}
//post favorite or Not to songsDb
function postFavorite(e, song) {
e.preventDefault()
fetch(`http://localhost:3000/songs/${song.id}`, {
method: "PATCH",
header: {
"content-Type": "application/json"
},
body: JSON.stringify({ "favorite": !song.favorite })
})
.then(response => {
if (response.ok) {
}
else {
alert("Something went wrong with favorite button")
}
})
}
const renderLikedSongsPlaylist = () => {
const selectedDiv = document.querySelector("#playlist-container")
fetch("http://localhost:3000/songs")
.then(response => {
// if response have a succcessful status code
if (response.ok) {
console.log(response, 'response')
return response.json() // gets returned to the next .then
} else {
alert("Something went wrong")
}
})
.then(song => {
song.forEach(song => {
if (song.favorite === true) {
console.log(song, "This is the song")
const selectedDiv = document.getElementById("playlist-container")
const button = document.createElement("button")
const playButton = document.createElement('div')
const playButtonIcon = document.createElement("img")
const text = document.createElement("div")
const title = document.createElement("h4")
const songArtist = document.createElement("h4")
const favoriteButton = document.createElement("img")
button.id = "suggested-song";
button.className = "song-button";
playButton.id = "stock-album-cover";
playButtonIcon.src = "./blackYtLogo.png"
playButtonIcon.id = "yt-logo"
text.id = "song-text";
title.textContent = song.songTitle;
songArtist.textContent = song.artist;
favoriteButton.src = ""
favoriteButton.id = "favorite-button"
songArtist.value = song.artist;
selectedDiv.appendChild(button);
button.appendChild(playButton);
playButton.appendChild(playButtonIcon)
button.appendChild(text);
text.appendChild(title);
text.appendChild(songArtist);
button.appendChild(favoriteButton)
console.log(song, "<----this is the renderSong Obj")
// Shows correct like image depending on state
song.favorite === true ? favoriteButton.src = "./favoriteFilled.png" : favoriteButton.src = "./favoriteEmpty.png"
// On click send to youtube
playButton.addEventListener("click", (e) => playMusicPlaylist(e, song))
favoriteButton.addEventListener("click", (e) => postFavorite(e, song))
}
})
})
}
// Shows The center Suggested Song
function centerDisplay(songObj) {
renderSong(songObj, "top-body-container")
}
function main() {
addSubmitListener()
addChangeLocationSubmitListener()
renderLikedSongsPlaylist()
}
main();