-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
155 lines (124 loc) Β· 4.74 KB
/
script.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
/*
π APP: Make Netflix
Create a fetchMovies() function that will make a dynamic API call to what you need π
========================================
- fetchMovies()
** fetchMovies takes in an URL, a div id or class from the HTML, and a path (poster or backdrop)
These are the 3 main functions you must create π
========================================
- getOriginals()
- getTrendingNow()
- getTopRated()
** These functions will provide the URL you need to fetch movies of that genere **
These are all the DIV ID's you're gonna need access to π
========================================================
#1 CLASS π 'original__movies' = Div that holds Netflix Originals
#2 ID π 'trending' = Div that holds trending Movies
#3 ID π 'top_rated' = Div that holds top rated Movies
*/
// Call the main functions the page is loaded
window.onload = () => {
getOriginals()
getTrendingNow()
getTopRated()
}
// ** Helper function that makes dynamic API calls **
// path_type π (backdrop, poster)
// dom_element π (trending, top rated)
// fetchMovies('https://api.themoviedb.org/3/movie/top_rated?api_key=19f84e11932abbc79e6d83f82d6d1045&language=en-US&page=1', 'top_rated', 'backdrop_path')
function fetchMovies(url, dom_element, path_type) {
fetch(url)
.then(response => {
if (response.ok) {
return response.json()
} else {
throw new Error('something went wrong')
}
})
.then(data => {
showMovies(data, dom_element, path_type)
})
.catch(error_data => {
console.log(error_data)
})
}
// ** Function that displays the movies to the DOM **
showMovies = (movies, dom_element, path_type) => {
// Create a variable that grabs id or class
var moviesEl = document.querySelector(dom_element)
// Loop through object
for (var movie of movies.results) {
// Within loop create an img element
var imageElement = document.createElement('img')
// Set attribute
imageElement.setAttribute('data-id', movie.id)
// Set source
imageElement.src = `https://image.tmdb.org/t/p/original${movie[path_type]}`
// Add event listener to handleMovieSelection() onClick
imageElement.addEventListener('click', e => {
handleMovieSelection(e)
})
// Append the imageElement to the dom_element selected
moviesEl.appendChild(imageElement)
}
}
// ** Function that fetches Netflix Originals **
function getOriginals() {
var url =
'https://api.themoviedb.org/3/discover/tv?api_key=19f84e11932abbc79e6d83f82d6d1045&with_networks=213'
fetchMovies(url, '.original__movies', 'poster_path')
}
// ** Function that fetches Trending Movies **
function getTrendingNow() {
var url =
'https://api.themoviedb.org/3/trending/movie/week?api_key=19f84e11932abbc79e6d83f82d6d1045'
fetchMovies(url, '#trending', 'backdrop_path')
}
// ** Function that fetches Top Rated Movies **
function getTopRated() {
var url =
'https://api.themoviedb.org/3/movie/top_rated?api_key=19f84e11932abbc79e6d83f82d6d1045&language=en-US&page=1'
fetchMovies(url, '#top_rated', 'backdrop_path')
}
// ** BONUS **
async function getMovieTrailer(id) {
var url = `https://api.themoviedb.org/3/movie/${id}/videos?api_key=19f84e11932abbc79e6d83f82d6d1045&language=en-US`
return await fetch(url).then(response => {
if (response.ok) {
return response.json()
} else {
throw new Error('something went wrong')
}
})
}
const setTrailer = trailers => {
const iframe = document.getElementById('movieTrailer')
const movieNotFound = document.querySelector('.movieNotFound')
if (trailers.length > 0) {
movieNotFound.classList.add('d-none')
iframe.classList.remove('d-none')
iframe.src = `https://www.youtube.com/embed/${trailers[0].key}`
} else {
iframe.classList.add('d-none')
movieNotFound.classList.remove('d-none')
}
}
const handleMovieSelection = e => {
const id = e.target.getAttribute('data-id')
const iframe = document.getElementById('movieTrailer')
// here we need the id of the movie
getMovieTrailer(id).then(data => {
const results = data.results
const youtubeTrailers = results.filter(result => {
if (result.site == 'YouTube' && result.type == 'Trailer') {
return true
} else {
return false
}
})
setTrailer(youtubeTrailers)
})
// open modal
$('#trailerModal').modal('show')
// we need to call the api with the ID
}