-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
192 lines (179 loc) · 5.7 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
const API_KEY = "1b869b3ccf57d089047ded4b1de007b8";
const BASE_URL = `https://api.themoviedb.org/3/discover/movie?api_key=${API_KEY}&language=en-US&page=1`;
const GENRE_URL = `https://api.themoviedb.org/3/genre/movie/list?api_key=${API_KEY}&language=en-US`;
const POSTER_URL = "https://image.tmdb.org/t/p/w500";
const selector = document.querySelector("#results");
const sortButton = document.querySelectorAll(".sort");
const icon = document.querySelectorAll(".icon");
const form = document.querySelector("form");
const thumbControl = document.querySelector(".thumb");
const caption = document.querySelector("#caption");
const toggleIcon = document.querySelector(".toggle-icon");
const { elements } = form;
let order = false;
let thumb = false;
fetch(BASE_URL)
.then(data => data.json())
.then(({ results }) => {
renderToPage(results);
thumbControl.addEventListener("click", e => {
thumb = !thumb;
renderToPage(results);
});
// Render results berdasarkan tombol sorting
sortButton.forEach(button => {
button.addEventListener("click", e => {
const target = button.dataset.id;
order = !order;
order
? (results.sort((a, b) => (a[target] < b[target] ? -1 : 1)),
icon.forEach(v => {
let str = "";
str += "arrow_downward";
return (v.innerHTML = str);
}))
: (results.reverse(),
icon.forEach(v => {
let str = "";
str += "arrow_upward";
return (v.innerHTML = str);
}));
renderToPage(results);
});
});
// Render results berdasarkan date range
form.addEventListener("submit", e => {
e.preventDefault();
const start = [elements["start"].value];
const end = [elements["end"].value];
const eachDay = dateFns.eachDay(start[0], end[0]);
const newFormat = eachDay.map(v => {
return dateFns.format(v, [(format = "YYYY-MM-DD")]);
});
renderToPage(results.filter(v => newFormat.includes(v.release_date)));
form.reset();
});
});
// Render reults, dan masukan ke dalam DOM
function renderToPage(results) {
thumb
? (toggleIcon.innerHTML = "view_agenda")
: (toggleIcon.innerHTML = "dashboard");
const reducer = results.map(async val => {
const genreResult = await getGenre(val.genre_ids);
caption.innerHTML = `${results.length} ${
results.length > 1 ? "Results" : "Result"
} found`;
return !thumb
? `
<ul class="collection">
<li class="collection-item avatar">
<div class="img-avatar">
<img class="circle" style="object-fit:cover;width:60px;height:60px;margin-top:10px" src="${POSTER_URL}${
val.poster_path
}">
</div>
<div class="content">
<span class="title">${truncate(val.original_title, 40)}</span>
<p>${dateFns.format(new Date(val.release_date), [
(format = "d MMM YYYY")
])}
<br>
${genreResult.reduce((acc, v) => {
return (acc += `<div class="chip list">
${v}
</div>`);
}, "")}
</p>
</div>
</li>
</ul>`
: `<div class="col s12 m4">
<div class="card">
<div class="card-image waves-effect waves-block waves-light">
<img style="height:375px;object-fit:cover" class="activator" src="${POSTER_URL}${
val.poster_path
}">
</div>
<div class="card-content">
<p>${dateFns.format(new Date(val.release_date), [
(format = "d MMM YYYY")
])}</p>
<span class="card-title activator grey-text text-darken-4">${truncate(
val.original_title,
10
)}<i class="material-icons right">more_vert</i></span>
</div>
<div class="card-reveal">
<span class="card-title grey-text text-darken-4">${
val.original_title
}<i class="material-icons right">close</i></span>
<h5>Overview</h5>
<p>${val.overview}</p>
<hr>
<h5>Genres</h5>
${genreResult.reduce((acc, v) => {
return (acc += `<div class="chip">
${v}
</div>`);
}, "")}
<hr>
<h5>Popularity</h5>
${val.popularity}
<hr>
<h5>Vote Count</h5>
${val.vote_count}
</div>
</div>
</div>
`;
});
function getTracks() {
//Append playlist name to modal header
const modalTitle = document.querySelector("h4.title");
const buttons = document.querySelectorAll("a.modal-trigger");
buttons.forEach(button => {
button.addEventListener("click", e => {
console.log("clicked");
// Append Playlist name to modal title
modalTitle.innerHTML = button.dataset.title;
// Get the playlist url
const url = button.dataset.link;
const options = {
headers: {
Authorization: `Bearer ${_token}`
}
};
// Fetch playlist url
fetch(url, options)
.then(response => response.json())
.then(data => appendToModal(data));
});
});
}
Promise.all(reducer).then(values => {
getTracks();
selector.innerHTML = values.join("");
});
}
function getGenre(arr) {
return fetch(GENRE_URL)
.then(results => results.json())
.then(data => {
const { genres } = data;
const names = arr.map(v => {
const { name } = genres.find(val => val.id === v);
return name;
});
return names;
});
}
function truncate(str, length) {
return str.length > length ? `${str.substring(0, length)} ..` : str;
}
document.addEventListener("DOMContentLoaded", () => {
var elems = document.querySelectorAll(".datepicker");
var instances = M.Datepicker.init(elems, {
format: "yyyy-mm-dd"
});
});