Theme back to previous functionality with dev server

This commit is contained in:
Neale Pickett 2020-02-28 16:01:35 -07:00
parent 94acae13ca
commit 4a73bb0910
1 changed files with 39 additions and 34 deletions

View File

@ -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)
}