-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
73 lines (49 loc) · 2.4 KB
/
app.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
let mainsec = document.querySelector('.movie-grid')
let btnSearch = document.querySelector('.search-btn')
let btnDelete = document.querySelector('.delete-btn')
//pegar o filme com o query e retornar o ID
async function getMovieId(query) {
//fetching movie by name
let request = await fetch(`https://api.themoviedb.org/3/search/movie?api_key=693afeb1b62f26148580aabf135da2b2&query=${query}`)
let reqFullfiled = await request.json()
return reqFullfiled
}
//pegando os detalhes do filme com o ID
async function getMovieDetails(movieId) {
let detRequest = await fetch(`https://api.themoviedb.org/3/movie/${movieId}?api_key=693afeb1b62f26148580aabf135da2b2&language=pt-BR`)
let reqSuccess = await detRequest.json()
return reqSuccess
}
function renderCard(movieData) {
//creating card
let elem = document.createElement('div') // criando a div do card
elem.classList.add('movie-card') // add class movie-card
elem.innerHTML = `
<p>${movieData.title}</p>
<button class="card-button">
<img src="https://image.tmdb.org/t/p/w300/${movieData.poster_path}" style="border-radius: 16px" class="card-img">
</button>
`
elem.style.cursor = 'pointer';
document.querySelectorAll('.card-img').forEach(img => { //condicional para tratar filmes sem imagens
if(img.src == "https://image.tmdb.org/t/p/w300/null") {
img.src = 'images/redx.svg'
}
})
return elem //retornando o elem
}
btnSearch.addEventListener('click', async () => {
let currentImages = document.querySelectorAll('.movie-card') //query nos cards atuais para remove-los
let movieInput = document.querySelector('#movie-input').value //valor do input
currentImages.forEach(currentImage => currentImage.remove()) //removendo os cards atuais
getMovieId(movieInput).then(response => response.results.forEach(movie => { //tratando a promise da async getMovieId
getMovieDetails(movie.id).then(data => renderCard(data)).then(r => {
mainsec.append(r)
return r //append dos cards no movie-grid
})}
)).catch(() => alert('Erro ao acessar a API - Certifique-se de preencher o campo adequadamente'))
})
btnDelete.addEventListener('click', () => {
let currentImages = document.querySelectorAll('.movie-card') //query nos cards atuais para remove-los
currentImages.forEach(currentImage => currentImage.remove())
})