forked from liza-p/project-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
154 lines (125 loc) · 4.38 KB
/
script.js
File metadata and controls
154 lines (125 loc) · 4.38 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
function showActivities(){
var table = new Tabulator("#activity-list", {
height:205,
data: allActivities.map(function(activity){
activity.formattedTime = formatTime(activity.time)
return activity;
} ),
layout:"fitColumns",
columns:[
{title:"Activity", field:"name", hozAlign:"center"},
{title:"Time", field:"formattedTime", hozAlign:"center"}
],
});
}
function formatTime(time) {
var timeMap = {
0: "12AM",
1: "1AM",
2: "2AM",
3: "3AM",
4: "4AM",
5: "5AM",
6: "6AM",
7: "7AM",
8: "8AM",
9: "9AM",
10: "10AM",
11: "11AM",
12: "12PM",
13: "1PM",
14: "2PM",
15: "3PM",
16: "4PM",
17: "5PM",
18: "6PM",
19: "7PM",
20: "8PM",
21: "9PM",
22: "10PM",
23: "11PM"
};
return timeMap[time];
}
var genres= [ "1996494362", // rap
"4994552284", //Pop
"1306931615", //Rock
"1132760061", //Alternative
"7391033164" // Jazz
];
var addedGenres = [];
// created a var to store the list of all saved activities in one array instead of creating a new item for each activity in localStor.This way all activities can be fetched all together from activityPlaylist
var allActivities=JSON.parse(localStorage.getItem("activityPlaylist"));
if(allActivities === null) {
allActivities =[];
}
$(document).ready(function (){
// Event listener
$('#current-date').text(moment().format('MMMM Do YYYY, h:mm a'));
$('#timePeriod').val(moment().format("H"))
$(".collection-item").on("click", function(){
console.log($(this).data("value"));
var playlistID = $(this).data("value");
$(this).addClass("darken-4");
addedGenres.push(playlistID.toString());
console.log(addedGenres);
$(this).off('click');
})
$("#submitActivity").on("click",function(){
var songs = [];
for(var i =0; i<5; i++){
var genreChosen = addedGenres[Math.floor(Math.random()*addedGenres.length)]
console.log(genreChosen);
var settings = {
"async": true,
"crossDomain": true,
"url": "https://deezerdevs-deezer.p.rapidapi.com/playlist/"+genreChosen+"/tracks",
"method": "GET",
"headers": {
"x-rapidapi-host": "deezerdevs-deezer.p.rapidapi.com",
"x-rapidapi-key": "a1a9ddf06bmsh40db7cac402f3cbp12b1a6jsnda2cd6aadae3"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
var songIndex = Math.floor(Math.random()*25)
console.log(songIndex);
console.log(response.data[songIndex])
var song= {
title: response.data[songIndex].title,
artist: response.data[songIndex].artist.name,
album: response.data[songIndex].album.title,
duration: response.data[songIndex].duration,
songId: response.data[songIndex].id
}
console.log(song)
songs.push(JSON.stringify(song));
console.log(songs)
});
}
// set up 2 sec wait to give some time for all 5 api calls to return data
setTimeout(function() {
console.log(songs);
var activity = {
name:$("#activityName").val(),
time: $("#timePeriod").val(),
playlist:songs
}
// pushin new added activities to the allActivities arr
allActivities.push(activity);
showActivities();
console.log(activity);
//storing the stringified version of allActivities in parsedAllActivities
var parsedAllActivities = JSON.stringify(allActivities);
console.log(parsedAllActivities);
localStorage.setItem("activityPlaylist",parsedAllActivities);
resetPage();
}, 2000)
})
showActivities();
});
function resetPage(){
$("#activityName").val("");
addedGenres = [];
$(".collection-item").removeClass("darken-4");
}