-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (50 loc) · 1.95 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
56
57
const YOUR_API_KEY = "d20a2751cd454b50a27c99a99b6585f0";
const PAGE_SIZE = 12;
let currentPage = 1;
const PLATFORMS_URL = `https://api.rawg.io/api/platforms?key=${YOUR_API_KEY}`;
let GAMES_URL = `https://api.rawg.io/api/games?key=${YOUR_API_KEY}&dates=2019-09-01,2019-09-30&platforms=18,1,7&page_size=${PAGE_SIZE}&page=${currentPage}`;
function fetchData(url) {
return fetch(url, {
headers: {
Authorization: `Bearer ${YOUR_API_KEY}`,
},
})
.then((response) => response.json())
.catch((error) => {
console.error("Error al obtener datos de la API:", error);
});
}
function updateGames() {
fetchData(GAMES_URL).then((gameData) => {
let html = "";
gameData.results.forEach(function (game) {
html += `
<div class="card m-4" style="width: 15rem;">
<img src="${game.background_image}" class="card-img-top" alt="Portada de ${game.name}">
<div class="card-body">
<h5 class="card-title">${game.name}</h5>
<p class="card-text">Fecha de lanzamiento: ${game.released}</p>
<!-- Agrega más información según la estructura de tus datos -->
</div>
</div>`;
});
document.getElementById("cartas").innerHTML = html;
});
}
fetchData(PLATFORMS_URL).then((platformData) => {
console.log("Datos de plataformas:", platformData);
});
updateGames();
// Manejar clic en "Siguiente" y "Anterior"
document.getElementById("nextPage").addEventListener("click", function () {
currentPage++;
GAMES_URL = `https://api.rawg.io/api/games?key=${YOUR_API_KEY}&dates=2019-09-01,2019-09-30&platforms=18,1,7&page_size=${PAGE_SIZE}&page=${currentPage}`;
updateGames();
});
document.getElementById("prevPage").addEventListener("click", function () {
if (currentPage > 1) {
currentPage--;
GAMES_URL = `https://api.rawg.io/api/games?key=${YOUR_API_KEY}&dates=2019-09-01,2019-09-30&platforms=18,1,7&page_size=${PAGE_SIZE}&page=${currentPage}`;
updateGames();
}
});