File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments