Commit 09b15104 authored by Jakob Moser's avatar Jakob Moser
Browse files

Dynamically load test based on URL parameter

parent c8925969
Loading
Loading
Loading
Loading
+18 −1
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ import { pooltest } from "./exercises.mjs"
import { getHandInToken } from "./snakeoil.mjs"
import { createExerciseCard, displayAsSolved, displayAsNonCurrent, displayAsCurrent } from "./exercises.cards.mjs"

let currentTest = pooltest
let currentTest = null

/**
 * Create a DOM card for the given exercise, displays is as solved if solved and inits event listeners
@@ -93,7 +93,24 @@ function initActionLinks() {
    })
}

function redirectToPractice() {
    // TODO Use something nicer based on URLSearchParams
    location.search = `?test=practice`
}

export function main() {
    const params = new URLSearchParams(location.search)
    const testId = params.get("id")

    if(testId === "pooltest") {
        currentTest = pooltest
    } else if(testId === "practice") {
        currentTest = null
    } else {
        // When no testId is provided or it is an unknown one, redirect to something working
        redirectToPractice()
    }

    document.querySelector("#testName").textContent = currentTest.name
    document.querySelector("#exerciseCnt").textContent = currentTest.exercises.length
    document.title = `${currentTest.name} · ${document.title}`