-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
54 lines (46 loc) · 1.38 KB
/
app.js
File metadata and controls
54 lines (46 loc) · 1.38 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
let pagina = 1;
const btnAnterior = document.getElementById("btnAnterior");
const btnSiguiente = document.getElementById("btnSiguiente");
btnSiguiente.addEventListener("click", () => {
if (pagina < 1000) {
pagina += 1;
cargarPeliculas();
}
});
btnAnterior.addEventListener("click", () => {
if (pagina > 1) {
pagina -= 1;
cargarPeliculas();
}
});
const cargarPeliculas = async () => {
try {
const respuesta = await fetch(
`https://api.themoviedb.org/3/movie/popular?api_key=192e0b9821564f26f52949758ea3c473&language=es-MX&page=${pagina}`
);
console.log(respuesta);
// Si la respuesta es correcta
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("Pusiste la llave mal");
} else if (respuesta.status === 404) {
console.log("La pelicula que buscas no existe");
} else {
console.log("Hubo un error y no sabemos que paso");
}
} catch (error) {
console.log(error);
}
};
cargarPeliculas();