-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |