-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
149 lines (133 loc) · 4.86 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
'use strict';
// API key
const API_KEY = 'f88c3a45cec61d60561069a2513612bc';
// Total pages of movie data - may be changeable
const TOTAL_PAGES = 500;
// Generate a random number between 0 - 500
const generateRandomNumber = ( results, sum ) => {
return Math.floor( Math.random() * results + sum )
};
// Generate URL for fetch call
const generateDatabaseUrl = (key, randNum) => `https://api.themoviedb.org/3/discover/movie?api_key=${key}&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=${randNum}`;
// Fetch data from database
const fetchMovieData = async url => {
const res = await fetch(url)
const data = await res.json();
return data;
}
// Display search warning when filters are high
let ratingVal = 0;
let yearVal = 0;
const displaySearchWarning = (valueType, value) => {
if ( valueType === 'rating' ) {
ratingVal = parseInt(value);
} else if ( valueType === 'year' ) {
yearVal = parseInt(value);
}
const SEARCH_ALERT = document.querySelector('#search-alert');
if ( ratingVal > 7 || yearVal > 11 || ratingVal + yearVal > 17 ) {
SEARCH_ALERT.style.marginTop = '0';
SEARCH_ALERT.style.opacity = '1';
} else {
SEARCH_ALERT.style.marginTop = '-7.5em';
SEARCH_ALERT.style.opacity = '0';
}
}
// Display range slider value
const displayRangeValueOutput = (sliderId, outputId, type = '') => {
const SLIDER = document.querySelector(sliderId);
const RANGE_OUTPUT = document.querySelector(outputId);
RANGE_OUTPUT.innerText = 'ALL';
switch ( type ) {
case 'year':
SLIDER.oninput = function() {
SLIDER.value === '0' ? (
RANGE_OUTPUT.innerText = 'ALL'
) : (
displaySearchWarning('year', SLIDER.value),
RANGE_OUTPUT.innerHTML =
`${( this.value * 10 ) + 1900} +`
)
}
break;
default:
SLIDER.oninput = function() {
SLIDER.value === '0' ? (
RANGE_OUTPUT.innerText = 'ALL'
) : (
displaySearchWarning('rating', SLIDER.value),
RANGE_OUTPUT.innerHTML = this.value
)
}
break;
}
return SLIDER
};
const RATING_SLIDER = displayRangeValueOutput('#min-rating', '#range-output');
const YEAR_SLIDER = displayRangeValueOutput('#min-year', '#year-output', 'year');
// Scroll through array on timeout
const timedScroll = array => {
let i = 0;
function scrollTitles() {
setTimeout(() => {
if ( i < array.length - 1 ) {
setTimeout(() => {
document.querySelector('#movie-title')
.innerText = array[i];
}, 0)
i++;
scrollTitles();
}
}, 50);
}
scrollTitles();
};
// Generate information for final movie
const finalMovieDetails = (arr, a) => {
document.querySelector('#movie-title')
.innerText = arr[a].title;
document.querySelector('#movie-image')
.innerHTML = `<img src="https://image.tmdb.org/t/p/w600_and_h900_bestv2${arr[a].poster_path}" alt="Movie Poster" />`;
document.querySelector('#description')
.innerText = arr[a].overview;
document.querySelector('#rating')
.innerText = arr[a].vote_average;
document.querySelector('#year')
.innerText = arr[a].release_date.substring(0, 4);
}
// Display description, year and rating of movie
const finalMovie = (array, randNum) => {
if (
array[randNum].vote_average < parseInt( RATING_SLIDER.value ) ||
parseInt(array[randNum].release_date.substring(0, 4)) < ( YEAR_SLIDER.value * 10 ) + 1900
) {
for ( let i = 0 ; i < array.length ; i++ ) {
if (
array[i].vote_average >= parseInt( RATING_SLIDER.value) &&
parseInt(array[randNum].release_date.substring(0, 4)) >= ( YEAR_SLIDER.value * 10 ) + 1900
) {
finalMovieDetails(array, i);
break;
} else {
randomMovie();
break;
}
}
} else {
finalMovieDetails(array, randNum)
}
};
// Choose random movie from fetch page
const randomMovie = () => {
fetchMovieData(generateDatabaseUrl(API_KEY, generateRandomNumber(TOTAL_PAGES, 1)))
.then(function(result) {
const MOVIE_DATA_ARR = result.results.map(movie => movie);
return MOVIE_DATA_ARR;
})
.then(function(result) {
const MOVIE_TITLE_ARR = result.map(movie => movie.title);
timedScroll(MOVIE_TITLE_ARR);
setTimeout(finalMovie, 1000, result, generateRandomNumber(result.length, 0));
})
};
document.querySelector('#get-random-movie').addEventListener('click', randomMovie, false);