waschmarken/static/js/user.js
2020-07-18 00:06:05 +02:00

54 lines
1.2 KiB
JavaScript

"use strict"
const token = document.location.pathname.split("/").pop();
const api_path = `../../api/token${token}/`;
const greeting = document.getElementById("greeting"),
currentcoins = document.getElementById("currentcoins"),
maxcoins = document.getElementById("maxcoins"),
sellvalue = document.getElementById("sellvalue");
async function redraw() {
const r = await fetch(api_path + 'get_user', {
method: 'POST',
body: JSON.stringify({}),
});
if (r.status !== 200) {
console.log("oh no");
return;
}
const user = await r.json();
greeting.innerText = `Hey, ${user.name}`;
currentcoins.innerText = user.score;
maxcoins.innerText = user.maxscore;
sellvalue.max = user.maxscore - user.score;
}
redraw();
const button = document.getElementById("sell");
button.addEventListener("click", async () => {
const newscore = parseInt(currentcoins.innerText) + parseInt(sellvalue.value);
console.log(newscore);
const r = await fetch(api_path + 'set_score', {
method: "POST",
body: JSON.stringify({
newscore: newscore,
}),
});
if (r.status !== 200) {
console.log("something went wrong");
return;
}
redraw();
})