From 4a73bb09100c6a4fe4ce6d94732a93f0e2bd5342 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Fri, 28 Feb 2020 16:01:35 -0700 Subject: [PATCH] Theme back to previous functionality with dev server --- theme/moth.js | 73 +++++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/theme/moth.js b/theme/moth.js index 7383388..3c81546 100644 --- a/theme/moth.js +++ b/theme/moth.js @@ -25,6 +25,8 @@ function renderNotices(obj) { function renderPuzzles(obj) { let puzzlesElement = document.createElement('div') + document.getElementById("login").style.display = "none" + // Create a sorted list of category names let cats = Object.keys(obj) cats.sort() @@ -91,13 +93,20 @@ function renderPuzzles(obj) { function renderState(obj) { devel = obj.config.devel - console.log(obj) - renderPuzzles(obj.puzzles) + if (devel) { + sessionStorage.id = "1234" + sessionStorage.pid = "rodney" + } + if (Object.keys(obj.puzzles).length > 0) { + renderPuzzles(obj.puzzles) + } renderNotices(obj.messages) } -function heartbeat(teamId, participantId) { +function heartbeat() { + let teamId = sessionStorage.id + let participantId = sessionStorage.pid let url = new URL("state", window.location) url.searchParams.set("id", teamId) if (participantId) { @@ -111,34 +120,29 @@ function heartbeat(teamId, participantId) { resp.json() .then(renderState) .catch(err => { - toast("Error fetching recent puzzles. I'll try again in a moment.") + toast("Error fetching recent state. I'll try again in a moment.") console.log(err) }) } }) .catch(err => { - toast("Error fetching recent puzzles. I'll try again in a moment.") + toast("Error fetching recent state. I'll try again in a moment.") console.log(err) }) } -function showPuzzles(teamId, participantId) { +function showPuzzles() { let spinner = document.createElement("span") spinner.classList.add("spinner") - sessionStorage.setItem("id", teamId) - if (participantId) { - sessionStorage.setItem("pid", participantId) - } - document.getElementById("login").style.display = "none" document.getElementById("puzzles").appendChild(spinner) - heartbeat(teamId, participantId) - setInterval(e => { heartbeat(teamId) }, 40000) - drawCacheButton(teamId) + heartbeat() + drawCacheButton() } -function drawCacheButton(teamId) { +function drawCacheButton() { + let teamId = sessionStorage.id let cacher = document.querySelector("#cacheButton") function updateCacheButton() { @@ -165,7 +169,7 @@ function drawCacheButton(teamId) { } async function fetchAll() { - let teamId = sessionStorage.getItem("id") + let teamId = sessionStorage.id let headers = new Headers() headers.append("pragma", "no-cache") headers.append("cache-control", "no-cache") @@ -206,14 +210,16 @@ async function fetchAll() { } let puzzles = categories[cat_name] for (let puzzle of puzzles) { - let url = new URL("puzzle.html", window.location) - url.searchParams.set("cat", cat_name) - url.searchParams.set("points", puzzle[0]) - url.searchParams.set("pid", puzzle[1]) - requests.push( fetch(url) - .then(e => { - console.log("Fetched " + url) - })) + let url = new URL("puzzle.html", window.location) + url.searchParams.set("cat", cat_name) + url.searchParams.set("points", puzzle[0]) + url.searchParams.set("pid", puzzle[1]) + requests.push( + fetch(url) + .then(e => { + console.log("Fetched " + url) + }) + ) } } } @@ -236,12 +242,11 @@ function login(e) { if (resp.ok) { resp.json() .then(obj => { - if (obj.status == "success") { - toast("Team registered") - showPuzzles(teamId, participantId) - } else if (obj.data.short == "Already registered") { - toast("Logged in with previously-registered team name") - showPuzzles(teamId, participantId) + if ((obj.status == "success") || (obj.data.short == "Already registered")) { + toast("Logged in") + sessionStorage.id = teamId + sessionStorage.pid = participantId + showPuzzles() } else { toast(obj.data.description) } @@ -263,11 +268,11 @@ function login(e) { function init() { // Already signed in? - let teamId = sessionStorage.getItem("id") - let participantId = sessionStorage.getItem("pid") - if (teamId) { - showPuzzles(teamId, participantId) + if (sessionStorage.id) { + showPuzzles() } + heartbeat() + setInterval(e => heartbeat(), 40000) document.getElementById("login").addEventListener("submit", login) }