boardgame_site_v2/scripts/main.js

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2024-08-08 16:14:55 +02:00
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);
}
}
2024-08-08 16:32:05 +02:00
function add_boardgame_row(html_tbody, boardgame_json) {
var row = html_tbody.insertRow();
var icon_cell = row.insertCell();
2024-08-08 16:14:55 +02:00
var name_cell = row.insertCell();
2024-08-08 16:32:05 +02:00
var description_cell = row.insertCell();
2024-08-08 16:14:55 +02:00
2024-08-08 16:32:05 +02:00
var icon_image = document.createElement('img')
icon_image.src = boardgame_json.thumbnail_url
icon_image.classList.add("img-thumbnail")
icon_cell.appendChild(icon_image)
2024-08-08 16:14:55 +02:00
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)
2024-08-08 16:32:05 +02:00
var boardgame_tbody = document.getElementById("boardgame_table_tbody")
2024-08-08 16:14:55 +02:00
for (index = 0; index < all_owned_games.length; index++) {
2024-08-08 16:32:05 +02:00
add_boardgame_row(boardgame_tbody, all_owned_games[index])
2024-08-08 16:14:55 +02:00
}
}