Skip to content

Commit e2328ad

Browse files
committed
Add passed status manager
1 parent 95fef31 commit e2328ad

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

static/js/passedState.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
3+
class PassedState {
4+
constructor(initialState = {}) {
5+
const currentState = localStorage.getItem('passedState');
6+
// initialize the state when there is no state in the local storage.
7+
if (!currentState) {
8+
if (!initialState) {
9+
throw new Error('initial state is required when there is no state in the local storage.');
10+
}
11+
12+
const state = this._prepareState(initialState);
13+
localStorage.setItem('passedState', JSON.stringify(state));
14+
this.state = state;
15+
return;
16+
}
17+
18+
if (currentState) {
19+
this.state = JSON.parse(currentState);
20+
}
21+
}
22+
23+
/**
24+
* prepare the state for initialization.
25+
* @param {object} rawState
26+
* @returns state - the state contains the challenge name and whether the challenge is passed.
27+
*/
28+
_prepareState(rawState) {
29+
const state = {};
30+
for (const level in rawState) {
31+
const challenges = [];
32+
for (const challengeName of rawState[level]) {
33+
challenges.push({
34+
name: challengeName,
35+
passed: false
36+
});
37+
}
38+
state[level] = challenges;
39+
}
40+
41+
return state;
42+
}
43+
}

0 commit comments

Comments
 (0)