-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTv.js
More file actions
86 lines (70 loc) · 2.44 KB
/
Copy pathTv.js
File metadata and controls
86 lines (70 loc) · 2.44 KB
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
import { getMovie, apiKey,imageUrl,baseUrl,getColors,showMovie,prev,next,current,currentPage,prevPage,nextPage,totalPage,lastUrl,form,submit,filter} from "./app.js";
const urlTV = baseUrl + "discover/tv?sort_by=popularity.desc&" + apiKey + '&include_adult=true'
const searchUrl = baseUrl + 'search/tv?' + apiKey
getMovie(urlTV)
form.addEventListener('submit',(e)=>{
e.preventDefault()
const searchTerm = search.value
if (searchTerm) {
getMovie(searchUrl + '&query=' + searchTerm + '&include_adult=true')
}
})
submit.addEventListener('click',()=>{
const searchTerm = search.value
if (searchTerm) {
getMovie(searchUrl + '&query=' + searchTerm)
}
})
const genreUrl = baseUrl + 'genre/tv/list?' + apiKey
getGenre(genreUrl)
function getGenre(url) {
fetch(url)
.then(res => res.json())
.then(data => {
showGenre(data.genres)
})
}
function showGenre(data) {
filter.innerHTML = ''
data.forEach(genres => {
const genre = document.createElement('button')
genre.classList.add('filterB')
genre.textContent = genres.name
filter.appendChild(genre)
genre.addEventListener('click',()=>{
toggleGenreSelection(genres.id)
genre.classList.toggle('active')
getFilteredMovie()
})
})
}
const genreSelected = []
function toggleGenreSelection(genreId) {
const index = genreSelected.indexOf(genreId)
if (index > -1) {
genreSelected.splice(index,1)
}else{
genreSelected.push(genreId)
}
getFilteredMovie()
}
function getFilteredMovie() {
const selectedGenreString = genreSelected.join(',')
getMovie(baseUrl +'discover/tv?' + apiKey + `&with_genres=${selectedGenreString}` + '&include_adult=true' )
}
const topRated = document.querySelector('#topRatedTv')
topRated.addEventListener('click',()=>{
getMovie('https://api.themoviedb.org/3/tv/top_rated?' + apiKey)
})
const trending = document.querySelector('#trendingTv')
trending.addEventListener('click',()=>{
getMovie('https://api.themoviedb.org/3/trending/tv/day?' + apiKey)
})
const airingToday = document.querySelector('#airingToday')
airingToday.addEventListener('click',()=>{
getMovie('https://api.themoviedb.org/3/tv/airing_today?' + apiKey)
})
const onTheAir = document.querySelector('#onTheAir')
onTheAir.addEventListener('click',()=>{
getMovie('https://api.themoviedb.org/3/tv/on_the_air?' + apiKey)
})