From b3aff0bf81260bb5ff6ba38e62824a3121d42bd9 Mon Sep 17 00:00:00 2001 From: Kai Vogelgesang Date: Sat, 18 Jul 2020 00:31:41 +0200 Subject: [PATCH] Implement error message --- static/js/user.js | 27 ++++++++++++++--- user.html | 75 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 74 insertions(+), 28 deletions(-) diff --git a/static/js/user.js b/static/js/user.js index 7f940b7..83b9430 100644 --- a/static/js/user.js +++ b/static/js/user.js @@ -7,7 +7,11 @@ const api_path = `../../api/token${token}/`; const greeting = document.getElementById("greeting"), currentcoins = document.getElementById("currentcoins"), maxcoins = document.getElementById("maxcoins"), - sellvalue = document.getElementById("sellvalue"); + sellvalue = document.getElementById("sellvalue"), + + errormesssage = document.getElementById("errormessage"), + errortext = document.getElementById("errortext"), + errorclose = document.getElementById("errorclose"); async function redraw() { @@ -27,7 +31,7 @@ async function redraw() { currentcoins.innerText = user.score; maxcoins.innerText = user.maxscore; sellvalue.max = user.maxscore - user.score; - + sellvalue.value = Math.min(sellvalue.value, sellvalue.max); } redraw(); @@ -36,7 +40,6 @@ 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", @@ -46,9 +49,25 @@ button.addEventListener("click", async () => { }); if (r.status !== 200) { - console.log("something went wrong"); + + if (r.status !== 400) { + console.log("Something went very very wrong!"); + return; + } + + const data = await r.json(); + + if (data.error) { + errortext.innerText = data.error; + errormesssage.style.display = "flex"; + } + return; } redraw(); +}) + +errorclose.addEventListener("click", () => { + errormesssage.style.display = "none"; }) \ No newline at end of file diff --git a/user.html b/user.html index 8f329b5..0bffa27 100644 --- a/user.html +++ b/user.html @@ -3,30 +3,52 @@ - + - Waschmarken.io - + Waschmarken.io + - - + + - + #thetable { + margin-bottom: 2rem; + } + + .flex { + display: flex; + } + + .biceps { + justify-content: space-around; + } + + input { + width: 8rem; + } + + #errormessage { + display: none; + /* flex if visible */ + justify-content: space-between; + width: 100%; + border-left: 3px solid red; + padding-left: 1rem; + background: #460000; + color: red; + } + + #errorclose { + margin-right: 1rem; + font-weight: bold; + cursor: pointer; + } + @@ -48,15 +70,20 @@ - -
+ +
- +
+
+ + × +
+ - + \ No newline at end of file