-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
51 lines (43 loc) · 1.23 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
let pagina = 1;
const btnAnterior = document.querySelector('#btnAnterior');
const btnSiguiente = document.querySelector('#btnSiguiente');
btnSiguiente.addEventListener('click' , () => {
if(pagina <= 500){
pagina += 1;
cargarPeliculas()
}
});
btnAnterior.addEventListener('click' , () => {
if(pagina > 1){
pagina -= 1;
cargarPeliculas()
}
});
const cargarPeliculas = async() => {
try {
//fetch devuelve una promesa
const respuesta = await fetch(`https://api.themoviedb.org/3/movie/popular?api_key=880e5d0d0643a40e8d9c5a13c0d19851&language=es-AR&page=${pagina}`);
if(respuesta.status === 200) {
const datos = await respuesta.json();
let peliculas = '';
datos.results.forEach(pelicula => {
peliculas +=`
<div class="pelicula">
<img class="poster" src="https://image.tmdb.org/t/p/w500/${pelicula.poster_path}">
<h3 class="titulo">${pelicula.title}</h3>
</div>
`
});
document.getElementById('contenedor').innerHTML = peliculas;
} else if(respuesta.status === 401) {
console.log("Api key erronea")
} else if(respuesta.status === 404) {
console.log("La pelicula no existe");
} else {
console.log("No sé que paso");
}
} catch (error) {
console.log(error);
}
}
cargarPeliculas();