From 8f6b096d1797ba48c0511de9262db8edd8a32eac Mon Sep 17 00:00:00 2001 From: Tristan Schneider Date: Thu, 1 Feb 2024 16:00:41 +0100 Subject: [PATCH] user cookie is set when sending word --- owe.js | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/owe.js b/owe.js index 83f536b..005e186 100644 --- a/owe.js +++ b/owe.js @@ -63,18 +63,31 @@ function onReloadClicked() { }, 100); } -function sendWord() { - var data = new FormData(); - data.append('submitted', encodeURI(document.getElementById("wordinput").value)); - data.append('user', getNextPlayer()); - const Http = new XMLHttpRequest(); - Http.open('POST', '.'); - Http.onreadystatechange=(e)=>{ - document.getElementById("wordinput").value = ""; - loadSentences(false, false); - resetUnread(); - } - Http.send(data); +async function sendWord() { + try { + var data = new FormData(); + var wordInput = document.getElementById("wordinput"); + data.append('submitted', encodeURI(wordInput.value)); + + // Use await to get the next player asynchronously + var nextPlayer = await getNextPlayer(); + data.append('user', nextPlayer); + + const Http = new XMLHttpRequest(); + Http.open('POST', '.'); + Http.onreadystatechange = (e) => { + wordInput.value = ""; + loadSentences(false, false); + resetUnread(); + }; + + // Set the 'owe_user' cookie with the value obtained from getNextPlayer + document.cookie = `owe_user=${encodeURIComponent(nextPlayer)}; path=/`; + + Http.send(data); + } catch (error) { + console.error("Error in sendWord:", error); + } } function displayNotification(word) {