Skip to content

Commit

Permalink
Fix state comparison bug
Browse files Browse the repository at this point in the history
  • Loading branch information
100gle committed Dec 26, 2023
1 parent 1d0571b commit fa6f43e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
19 changes: 9 additions & 10 deletions static/js/passedState.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PassedState {
const rawState = this._prepareState(initialState);

// check new state and old state whether is undefined or not. and merge the new state to the old state.
const state = this._checkAndMerge(currentState, rawState);
const state = this._checkAndMerge(JSON.parse(currentState), rawState);
this._save(state);
this._state = state;
return
Expand Down Expand Up @@ -84,12 +84,16 @@ class PassedState {
throw new Error('one of the new state and the old state is required.');
}

if (!newState) {
if (!oldState && newState) {
return newState;
}

if (!newState && oldState) {
return oldState;
}

let mergedState = {};
const levels = ['basic', 'intermediate', 'advanced', 'expert'];
const levels = ['basic', 'intermediate', 'advanced', 'extreme'];

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

// Add or update challenges from the newState
for (const [name, newChallenge] of newChallengesMap.entries()) {
mergedChallenges.push({ ...newChallenge, passed: oldChallengesMap.get(name)?.passed });
oldChallengesMap.delete(name); // Remove the challenge from oldChallengesMap since it's updated
}

// Add remaining challenges from the oldState that are not updated (not present in newState)
for (const oldChallenge of oldChallengesMap.values()) {
mergedChallenges.push(oldChallenge);
let hasPassed = oldChallengesMap.get(name)?.passed || newChallenge.passed;
mergedChallenges.push({ ...newChallenge, passed: hasPassed });
}

// Set the merged challenges for the current level in the mergedState
Expand Down
3 changes: 3 additions & 0 deletions templates/components/challenge_sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,7 @@ <h5 class="challenge-level">{{ level }}</h5>
c.classList.remove('active-challenge');
}
}

const initialState = {{ challenges_groupby_level | tojson }}
const passedState = new PassedState(initialState);
</script>

0 comments on commit fa6f43e

Please sign in to comment.