@@ -5,7 +5,7 @@ class PassedState {
5
5
_key = 'passedState' ;
6
6
_state = null ;
7
7
8
- constructor ( initialState = { } ) {
8
+ init ( initialState ) {
9
9
const currentState = localStorage . getItem ( this . _key ) ;
10
10
// initialize the state when there is no state in the local storage.
11
11
if ( ! currentState && ! initialState ) {
@@ -16,12 +16,11 @@ class PassedState {
16
16
const rawState = this . _prepareState ( initialState ) ;
17
17
18
18
// check new state and old state whether is undefined or not. and merge the new state to the old state.
19
- const state = this . _checkAndMerge ( currentState , rawState ) ;
19
+ const state = this . _checkAndMerge ( JSON . parse ( currentState ) , rawState ) ;
20
20
this . _save ( state ) ;
21
21
this . _state = state ;
22
22
return
23
23
}
24
-
25
24
/**
26
25
* prepare the state for initialization.
27
26
* @param {object } rawState
@@ -48,6 +47,10 @@ class PassedState {
48
47
}
49
48
50
49
get ( ) {
50
+ if ( ! this . _state ) {
51
+ const currentState = localStorage . getItem ( this . _key ) ;
52
+ this . _state = JSON . parse ( currentState ) ;
53
+ }
51
54
return this . _state ;
52
55
}
53
56
@@ -62,6 +65,10 @@ class PassedState {
62
65
* @returns void
63
66
*/
64
67
setPassed ( level , challengeName ) {
68
+ if ( ! this . _state ) {
69
+ this . get ( )
70
+ }
71
+
65
72
const challenges = this . _state [ level ] ;
66
73
for ( const challenge of challenges ) {
67
74
if ( challenge . name === challengeName ) {
@@ -84,12 +91,16 @@ class PassedState {
84
91
throw new Error ( 'one of the new state and the old state is required.' ) ;
85
92
}
86
93
87
- if ( ! newState ) {
94
+ if ( ! oldState && newState ) {
95
+ return newState ;
96
+ }
97
+
98
+ if ( ! newState && oldState ) {
88
99
return oldState ;
89
100
}
90
101
91
102
let mergedState = { } ;
92
- const levels = [ 'basic' , 'intermediate' , 'advanced' , 'expert ' ] ;
103
+ const levels = [ 'basic' , 'intermediate' , 'advanced' , 'extreme ' ] ;
93
104
94
105
for ( const level of levels ) {
95
106
// Initialize an empty array for merged challenges
@@ -101,13 +112,8 @@ class PassedState {
101
112
102
113
// Add or update challenges from the newState
103
114
for ( const [ name , newChallenge ] of newChallengesMap . entries ( ) ) {
104
- mergedChallenges . push ( { ...newChallenge , passed : oldChallengesMap . get ( name ) ?. passed } ) ;
105
- oldChallengesMap . delete ( name ) ; // Remove the challenge from oldChallengesMap since it's updated
106
- }
107
-
108
- // Add remaining challenges from the oldState that are not updated (not present in newState)
109
- for ( const oldChallenge of oldChallengesMap . values ( ) ) {
110
- mergedChallenges . push ( oldChallenge ) ;
115
+ let hasPassed = oldChallengesMap . get ( name ) ?. passed || newChallenge . passed ;
116
+ mergedChallenges . push ( { ...newChallenge , passed : hasPassed } ) ;
111
117
}
112
118
113
119
// Set the merged challenges for the current level in the mergedState
0 commit comments