Skip to content

Commit

Permalink
Add passed status manager
Browse files Browse the repository at this point in the history
  • Loading branch information
100gle committed Dec 24, 2023
1 parent 95fef31 commit e2328ad
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions static/js/passedState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@


class PassedState {
constructor(initialState = {}) {
const currentState = localStorage.getItem('passedState');
// initialize the state when there is no state in the local storage.
if (!currentState) {
if (!initialState) {
throw new Error('initial state is required when there is no state in the local storage.');
}

const state = this._prepareState(initialState);
localStorage.setItem('passedState', JSON.stringify(state));
this.state = state;
return;
}

if (currentState) {
this.state = JSON.parse(currentState);
}
}

/**
* prepare the state for initialization.
* @param {object} rawState
* @returns state - the state contains the challenge name and whether the challenge is passed.
*/
_prepareState(rawState) {
const state = {};
for (const level in rawState) {
const challenges = [];
for (const challengeName of rawState[level]) {
challenges.push({
name: challengeName,
passed: false
});
}
state[level] = challenges;
}

return state;
}
}

0 comments on commit e2328ad

Please sign in to comment.