Skip to content

Commit 9ed7e52

Browse files
committed
Fix state comparison bug
1 parent 1d0571b commit 9ed7e52

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

static/js/passedState.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class PassedState {
55
_key = 'passedState';
66
_state = null;
77

8-
constructor(initialState = {}) {
8+
init(initialState) {
99
const currentState = localStorage.getItem(this._key);
1010
// initialize the state when there is no state in the local storage.
1111
if (!currentState && !initialState) {
@@ -16,12 +16,11 @@ 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
2323
}
24-
2524
/**
2625
* prepare the state for initialization.
2726
* @param {object} rawState
@@ -48,6 +47,10 @@ class PassedState {
4847
}
4948

5049
get() {
50+
if (!this._state) {
51+
const currentState = localStorage.getItem(this._key);
52+
this._state = JSON.parse(currentState);
53+
}
5154
return this._state;
5255
}
5356

@@ -62,6 +65,10 @@ class PassedState {
6265
* @returns void
6366
*/
6467
setPassed(level, challengeName) {
68+
if (!this._state) {
69+
this.get()
70+
}
71+
6572
const challenges = this._state[level];
6673
for (const challenge of challenges) {
6774
if (challenge.name === challengeName) {
@@ -84,12 +91,16 @@ class PassedState {
8491
throw new Error('one of the new state and the old state is required.');
8592
}
8693

87-
if (!newState) {
94+
if (!oldState && newState) {
95+
return newState;
96+
}
97+
98+
if (!newState && oldState) {
8899
return oldState;
89100
}
90101

91102
let mergedState = {};
92-
const levels = ['basic', 'intermediate', 'advanced', 'expert'];
103+
const levels = ['basic', 'intermediate', 'advanced', 'extreme'];
93104

94105
for (const level of levels) {
95106
// Initialize an empty array for merged challenges
@@ -101,13 +112,8 @@ class PassedState {
101112

102113
// Add or update challenges from the newState
103114
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 });
111117
}
112118

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

templates/components/challenge_sidebar.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ <h5 class="challenge-level">{{ level }}</h5>
124124
</nav>
125125
</aside>
126126

127+
<script type="text/javascript" src="{{ url_for('static', filename='js/passedState.js')}}"></script>
127128
<script>
128129
const sidebarTogglers = document.querySelectorAll('.sidebar-toggle');
129130
const drawer = document.querySelector('.drawer');
@@ -146,4 +147,8 @@ <h5 class="challenge-level">{{ level }}</h5>
146147
c.classList.remove('active-challenge');
147148
}
148149
}
150+
151+
const initialState = {{ challenges_groupby_level | tojson }};
152+
const passedState = new PassedState();
153+
passedState.init(initialState);
149154
</script>

0 commit comments

Comments
 (0)