Skip to content

Commit fa6f43e

Browse files
committed
Fix state comparison bug
1 parent 1d0571b commit fa6f43e

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

static/js/passedState.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class PassedState {
1616
const rawState = this._prepareState(initialState);
1717

1818
// 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);
2020
this._save(state);
2121
this._state = state;
2222
return
@@ -84,12 +84,16 @@ class PassedState {
8484
throw new Error('one of the new state and the old state is required.');
8585
}
8686

87-
if (!newState) {
87+
if (!oldState && newState) {
88+
return newState;
89+
}
90+
91+
if (!newState && oldState) {
8892
return oldState;
8993
}
9094

9195
let mergedState = {};
92-
const levels = ['basic', 'intermediate', 'advanced', 'expert'];
96+
const levels = ['basic', 'intermediate', 'advanced', 'extreme'];
9397

9498
for (const level of levels) {
9599
// Initialize an empty array for merged challenges
@@ -101,13 +105,8 @@ class PassedState {
101105

102106
// Add or update challenges from the newState
103107
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);
108+
let hasPassed = oldChallengesMap.get(name)?.passed || newChallenge.passed;
109+
mergedChallenges.push({ ...newChallenge, passed: hasPassed });
111110
}
112111

113112
// Set the merged challenges for the current level in the mergedState

templates/components/challenge_sidebar.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,7 @@ <h5 class="challenge-level">{{ level }}</h5>
146146
c.classList.remove('active-challenge');
147147
}
148148
}
149+
150+
const initialState = {{ challenges_groupby_level | tojson }}
151+
const passedState = new PassedState(initialState);
149152
</script>

0 commit comments

Comments
 (0)