const api_url = "http://127.0.0.1:8000"
var all_owned_games
async function makeRequest(url) {
try {
const url_request = new Request(url)
const response = await fetch(url_request);
const result = await response.json();
return result
} catch (error) {
console.error("Error:", error);
}
}
function add_boardgame_row(html_tbody, boardgame_json) {
var row = html_tbody.insertRow();
var icon_cell = row.insertCell();
var name_cell = row.insertCell();
var description_cell = row.insertCell();
var weight_cell = row.insertCell()
var icon_image = document.createElement('img')
icon_image.src = boardgame_json.thumbnail_url
icon_image.classList.add("img-thumbnail")
icon_cell.appendChild(icon_image)
name_cell.innerHTML = boardgame_json.name
description_cell.innerHTML = boardgame_json.description
weight_cell.innerHTML = boardgame_json.weight
row.onclick = function(){ window.location.href = '/boardgame?id=' + boardgame_json.id}
}
async function loadWishlistedGames() {
var wishlist_priorities = [1,2,3,4]
jQuery.each(wishlist_priorities, function(index, item){
var boardgame_datatable = new DataTable('#wishlist_table'+item, {
ajax: {
url: api_url + '/wishlist?priority='+item,
dataSrc: ''
},
columns: [
{
data: 'wishlist_priority'
},
{
data: 'thumbnail_url',
render: function (data,type){
return '
'
}
},
{
data: 'name'
},
{
data: 'description'
},
{
data: 'weight'
}
],
columnDefs: [
{
target: 0,
visible: false
}
],
order: [[2, 'asc']]
});
$('#wishlist_table'+item).on('click', 'tbody tr', function() {
var boardgame_id = boardgame_datatable.row(this).data().id;
window.location.href = "/boardgame?id=" + boardgame_id
})
});
}
async function loadOwnedGames() {
var boardgame_datatable = new DataTable('.boardgame_table', {
ajax: {
url: api_url + '/owned',
dataSrc: ''
},
columns: [
{
data: 'thumbnail_url',
render: function (data,type){
return '
'
}
},
{
data: 'name'
},
{
data: 'description'
},
{
data: 'weight'
}
],
order: [[1, 'asc']]
});
$('.boardgame_table').on('click', 'tbody tr', function() {
var boardgame_id = boardgame_datatable.row(this).data().id;
window.location.href = "/boardgame?id=" + boardgame_id
})
}
async function loadGame() {
let params = new URLSearchParams(document.location.search);
let boardgame_id = params.get("id");
var loadGameURL = api_url + '/boardgame/' + boardgame_id
var requested_game = await makeRequest(loadGameURL)
$('#boardgame_image').attr('src', requested_game.image_url)
$('#boardgame_name').text(requested_game.name)
$('#boardgame_weight').text(requested_game.weight)
$('#boardgame_description').text(requested_game.description)
$('#boardgame_link').attr('href', 'https://boardgamegeek.com/boardgame/' + boardgame_id)
}