Commit 137f747e authored by Jakob Moser's avatar Jakob Moser
Browse files

Monkey-patch in different realms for state

parent 582933d9
Loading
Loading
Loading
Loading
+16 −6
Original line number Diff line number Diff line
/** Read and modify the current state of the application. Allows saving to local storage. */

export class State {

    /**
     * Create a new state object, backed by local storage. The realm parameter allows to store
     * several different states side-by-side, without interfering.
     * @param {string} realm
     */
    constructor(realm) {
        this.realm = realm
    }

    /**
     * Return if this is the first time the application was started
     */
    get firstStart() {
        const firstStart = localStorage.getItem("firstStart")
        const firstStart = localStorage.getItem(`${this.realm}.firstStart`)
        // firstStart === null: If the key firstStart doesn't even exist, we certainly started for the first time
        // firstStart === "true": Local storage can only store strings, so this is our way of casting to boolean
        return firstStart === null || firstStart === "true"
@@ -19,14 +29,14 @@ export class State {
            throw new Error("value must be a boolean")
        }

        localStorage.setItem("firstStart", value)
        localStorage.setItem(`${this.realm}.firstStart`, value)
    }

    /**
     * Get the index of the exercise the user is currently working on
     */
    get exerciseIndex() {
        const exerciseIndex = localStorage.getItem("exerciseIndex")
        const exerciseIndex = localStorage.getItem(`${this.realm}.exerciseIndex`)
        if (exerciseIndex === null) {
            return null
        } else {
@@ -46,14 +56,14 @@ export class State {
        // TODO Verify that index is in range, else
        // throw new Error(`Exercise index must be >= 0 and < exercises.length (${pooltest.exercises.length})`)

        localStorage.setItem("exerciseIndex", index)
        localStorage.setItem(`${this.realm}.exerciseIndex`, index)
    }

    /**
     * Get a list of indices of all solved exercises
     */
    get solvedExercises() {
        return JSON.parse(localStorage.getItem("solvedExercises")) || []
        return JSON.parse(localStorage.getItem(`${this.realm}.solvedExercises`)) || []
    }

    /**
@@ -61,7 +71,7 @@ export class State {
     * @param {Array} indices Array of indicies of exercises that are solved
     */
    _setSolvedExercises(indices) {
        localStorage.setItem("solvedExercises", JSON.stringify(indices))
        localStorage.setItem(`${this.realm}.solvedExercises`, JSON.stringify(indices))
    }

    /**
+4 −3
Original line number Diff line number Diff line
import { State } from "./state.mjs"
import * as exercises from "./exercises.mjs"
import * as tests from "./exercises.mjs"
import { createExerciseCard, displayAsSolved, displayAsNonCurrent, displayAsCurrent } from "./exercises.cards.mjs"
import { Exercise } from "./exercises.api.mjs"

const currentTest = exercises[new URLSearchParams(location.search).get("id")]
const currentState = new State()
const testId = new URLSearchParams(location.search).get("id")
const currentTest = tests[testId]
const currentState = new State(testId)

/**
 * Create a DOM card for the given exercise, displays is as solved if solved and inits event listeners