-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecommend.js
75 lines (66 loc) · 2.31 KB
/
recommend.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
function displayRecommendedMedia(media, dataArray) {
if (dataArray[0] == undefined || dataArray[1] == undefined) {
recommendMedia(media);
return;
}
var parent = document.querySelector(".result");
var final_str = "";
dataArray.forEach(element => {
final_str += constructHTMLStr(element, media);
});
parent.innerHTML = final_str;
// listener for detail of media
addEventListenerToItems();
}
function getRandom(limit) {
var r = parseInt(Math.random() * limit);
return (r + 1) % (limit + 1);
}
function recommendMedia(media) {
// a random pages
var randPage = getRandom(25);
// two diff random items from the page
// 20 results on the page
var index1 = getRandom(20);
var index2 = index1;
while (index2 === index1) {
index2 = getRandom(20);
}
var filters = [orderby.vote_count, options.page(randPage)];
query_url = getQueryUrl("/discover/" + media)(filters);
fetch(query_url)
.then(checkResponse)
.then(function (response) {
console.log("All Ok!");
return response.json();
})
.then(function (data) {
displayRecommendedMedia(media, [data.results[index1], data.results[index2]]);
})
.catch(function (error) {
console.log(error);
});
}
window.addEventListener('DOMContentLoaded', (event) => {
var media = location.search.substring(1);
if(media === "movie" || media === "tv") {
recommendMedia(media);
} else {
noResult();
return;
}
// using a loader
var results = document.querySelector('.result');
var loader = document.querySelector('.loader');
var x = setInterval(function () {
if (results.childElementCount > 0) {
loader.style.display = 'none';
clearInterval(x);
} else {
loader.style.display = 'block';
}
}, 100);
});
function noResult () {
document.querySelector('.main-content').innerHTML = '<h1 style="text-align:center"><i style="color: #AFFC41" class="fa fa-frown-o" aria-hidden="true"></i> No results found!</h1>';
}