Table now loads owned board games

This commit is contained in:
Yarne Coppens 2024-08-08 16:14:55 +02:00
parent 204f8a4f22
commit f944e320ef
2 changed files with 47 additions and 1 deletions

View file

@ -11,7 +11,18 @@
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
<body onload="loadOwnedGames()">
<table id="boardgame_table" class="table table-striped">
<thead>
<tr>
<th scope="col">Naam</th>
<th scope="col">Beschrijving</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>

View file

@ -0,0 +1,35 @@
const api_url = "http://127.0.0.1:8000"
var all_owned_games
async function makeAPIRequest(request) {
try {
const response = await fetch(request);
const result = await response.json();
return result
} catch (error) {
console.error("Error:", error);
}
}
function add_boardgame_row(html_table, boardgame_json) {
var row = html_table.insertRow()
var name_cell = row.insertCell();
var description_cell = row.insertCell()
name_cell.innerHTML = boardgame_json.name
description_cell.innerHTML = boardgame_json.description
}
async function loadOwnedGames() {
const loadGamesRequest = new Request(api_url + "/owned")
all_owned_games = await makeAPIRequest(loadGamesRequest)
console.log("Loaded owned games:", all_owned_games)
var boardgame_table = document.getElementById("boardgame_table")
for (index = 0; index < all_owned_games.length; index++) {
add_boardgame_row(boardgame_table, all_owned_games[index])
}
}