-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiTransaction.js
56 lines (45 loc) · 1.79 KB
/
apiTransaction.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
// Initial Values
const MOVIE_DB_API = 'd8bf019d0cca372bd804735f172f67e8';
const MOVIE_DB_ENDPOINT = 'https://api.themoviedb.org';
const MOVIE_DB_IMAGE_ENDPOINT = 'https://image.tmdb.org/t/p/w500';
const DEFAULT_POST_IMAGE = 'https://via.placeholder.com/150';
function requestMovies(url, onComplete, onError) {
fetch(url)
.then((res) => res.json())
.then(onComplete)
.catch(onError);
}
function generateMovieDBUrl(path) {
const url = `${MOVIE_DB_ENDPOINT}/3${path}?api_key=${MOVIE_DB_API}`;
return url;
}
function getTopRatedMovies() {
const url = generateMovieDBUrl(`/movie/top_rated`);
const render = renderMovies.bind({ title: 'Top Rated Movies' })
requestMovies(url, render, handleGeneralError);
}
function getTrendingMovies() {
const url = generateMovieDBUrl('/trending/movie/day');
const render = renderMovies.bind({ title: 'Trending Movies' })
requestMovies(url, render, handleGeneralError);
}
function searchUpcomingMovies() {
const url = generateMovieDBUrl('/movie/upcoming');
const render = renderMovies.bind({ title: 'Upcoming Movies' })
requestMovies(url, render, handleGeneralError);
}
function searchPopularMovie() {
const url = generateMovieDBUrl('/movie/popular');
const render = renderMovies.bind({ title: 'Popular Movies' });
requestMovies(url, render, handleGeneralError);
}
// Invoke a different function for search movies
function searchMovie(value) {
const url = generateMovieDBUrl('/search/movie') + '&query=' + value;
requestMovies(url, renderSearchMovies, handleGeneralError);
}
function getVideosByMovieId(movieId, content) {
const url = generateMovieDBUrl(`/movie/${movieId}/videos`);
const render = createVideoTemplate.bind({ content });
requestMovies(url, render, handleGeneralError);
}