-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
64 lines (47 loc) · 1.73 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
window.onload = () => {
getOriginals()
getTrendingNow()
getTopRated()
}
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 => {
console.log(error)
})
}
fetchMovies('https://api.themoviedb.org/3/discover/tv?api_key=19f84e11932abbc79e6d83f82d6d1045&with_networks=213')
//Function that displays the movies to the DOM
showMovies = (movies, dom_element, path_type) => {
let moviesEl = document.querySelector(dom_element )
console.log(movies.results)
for (let movie of movies.results){
let imageElement = document.createElement('img')
imageElement.setAttribute('data-id', movie.id)
imageElement.src =
`https://image.tmdb.org/t/p/original${movie[path_type]}`
moviesEl.appendChild(imageElement)
}
}
//Function that fetches Netflix Originals
function getOriginals() {
let 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() {
let 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() {
let url = 'https://api.themoviedb.org/3/movie/top_rated?api_key=19f84e11932abbc79e6d83f82d6d1045&language=en-US&page=1'
fetchMovies(url, '#top_rated', 'backdrop_path')
}