*: switch sessionId to server-side

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2023-09-28 21:19:58 -04:00
parent 67826bb20d
commit 380ec821b1
Signed by: vbatts
GPG key ID: E30EFAA812C6E5ED
6 changed files with 2755 additions and 34 deletions

View file

@ -2,14 +2,6 @@
let sessionStartTime;
let lastButtonClickTime;
// Function to generate a random session ID
function generateSessionId() {
return (
Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15)
);
}
// Function to fetch a random animal name from the server
async function getNextAnimal() {
try {
@ -28,17 +20,20 @@ function getSessionId() {
"$1",
);
if (!sessionId) {
const newSessionId = generateSessionId();
document.cookie = `sessionId=${newSessionId}`;
return newSessionId;
return newSession();
}
return sessionId;
}
function clearSessionId() {
const newSessionId = generateSessionId();
document.cookie = `sessionId=${newSessionId}`;
async function newSession() {
setSessionStartTime();
try {
const response = await fetch("/newSession");
const data = await response.json();
document.getElementById("animal-name").textContent = data.animalName;
} catch (error) {
console.error("Error fetching data:", error);
}
getSessionId();
}
@ -56,20 +51,6 @@ function displayTimeDifference() {
}
}
// Add click event listeners to the buttons
document.getElementById("isCritterButton").addEventListener("click", () => {
recordButtonClick("is critter", getSessionId());
});
document.getElementById("isNotCritterButton").addEventListener("click", () => {
recordButtonClick("is not critter", getSessionId());
});
document.getElementById("startOverButton").addEventListener("click", () => {
clearSessionId();
getNextAnimal();
});
// Function to record button clicks on the server
async function recordButtonClick(buttonName, sessionId) {
try {
@ -103,6 +84,20 @@ async function recordButtonClick(buttonName, sessionId) {
}
}
// Add click event listeners to the buttons
document.getElementById("isCritterButton").addEventListener("click", () => {
recordButtonClick("is critter", getSessionId());
});
document.getElementById("isNotCritterButton").addEventListener("click", () => {
recordButtonClick("is not critter", getSessionId());
});
document.getElementById("startOverButton").addEventListener("click", () => {
newSession();
getNextAnimal();
});
// Initial random animal load and session start time
getNextAnimal();
setSessionStartTime();