-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
55 lines (43 loc) · 1.25 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
let pagina = 1;
const btnAnterior = document.getElementById("btnAnterior");
const btnSiguiente = document.getElementById("btnSiguiente");
btnSiguiente.addEventListener("click", () => {
if(pagina < 1000){
pagina += 1;
optenerPeliculas();
}
})
btnAnterior.addEventListener("click", () => {
if(pagina > 1){
pagina -= 1;
optenerPeliculas();
}
})
const optenerPeliculas = async () => {
try {
const respuesta = await fetch(`https://api.themoviedb.org/3/movie/popular?api_key=d5c775389c73a0b2a2bc815d05093528&language=english-MX&page=${pagina}`)
console.log(respuesta)
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("key erroneo");
} else if(respuesta.status === 404){
console.log("Pelicula no encontrada")
} else {
console.log("Hubo un erro inesperado")
}
} catch(error){
console.log(error);
}
}
optenerPeliculas ()