-
Notifications
You must be signed in to change notification settings - Fork 33
feat: Record passed status for challenges #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a938178
Add passed status manager
100gle 4684a29
Add basic method for passedState
100gle 1d0571b
Update compare logic for old and new state
100gle 9ed7e52
Fix state comparison bug
100gle 2f357fc
Sync data when challenge passed
100gle da169fd
Add finishment for challenges
100gle db1198a
Rename the custom js file
100gle 35a1d68
Add docs for methods
100gle 9b414ea
Fix sidebar item style
100gle ca6c060
Remove unnecessary assigment
100gle f48e964
Fix some reviewed issues
100gle 2056f8f
Comment special lines
100gle b0eb967
Restore the old padding for sidebar items
100gle 2e6f206
Remove internal `_state` variable
100gle 4ec55c9
Resolve reviewed issues
100gle 6146d07
Format and modularize `passed-state.js`
100gle 0d1c222
Fix the active highlight bug when modularization
100gle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
class PassedState { | ||
_key = 'python-type-challenges'; | ||
|
||
/** | ||
* Initializing when there is no state in the local storage. If there is no state in the local storage, the initial state is required. | ||
* this function will check the new state and the old state whether is undefined or not and updated the old state based on new state. | ||
* | ||
* @param {object} initialState - the initial state of the challenges which grouped by the level. | ||
* @returns void | ||
*/ | ||
init(initialState) { | ||
const currentState = this.get(); | ||
// initialize the state when there is no state in the local storage. | ||
if (!currentState && !initialState) { | ||
throw new Error('initial state is required when there is no state in the local storage.'); | ||
} | ||
|
||
// add passed property to the challenges in the initial state. | ||
const rawState = this._prepareState(initialState); | ||
100gle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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); | ||
this._save(state); | ||
} | ||
/** | ||
* prepare the state for initialization. | ||
* | ||
* @param {object} rawState | ||
* @returns state - the state contains the challenge name and whether the challenge is passed. | ||
*/ | ||
_prepareState(rawState) { | ||
100gle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!rawState) { | ||
return {}; | ||
} | ||
|
||
const state = {}; | ||
for (const level in rawState) { | ||
const challenges = []; | ||
for (const challengeName of rawState[level]) { | ||
challenges.push({ | ||
name: challengeName, | ||
passed: false | ||
}); | ||
} | ||
state[level] = challenges; | ||
} | ||
|
||
return state; | ||
} | ||
|
||
get() { | ||
const currentState = localStorage.getItem(this._key); | ||
return JSON.parse(currentState); | ||
} | ||
|
||
/** | ||
* Save the state to the local storage with JSON format. | ||
* @param {object} state - the state contains the challenge name and whether the challenge is passed. | ||
*/ | ||
_save(state) { | ||
localStorage.setItem(this._key, JSON.stringify(state)); | ||
} | ||
|
||
/** | ||
* Set the target challenge as passed in the state. | ||
* | ||
* @param {'basic' | 'intermediate' | 'advanced' | 'extreme'} level - the level of the challenge. | ||
* @param {string} challengeName - the name of the challenge. | ||
* @returns void | ||
*/ | ||
setPassed(level, challengeName) { | ||
let state = this.get(); | ||
|
||
const challenges = state[level]; | ||
laike9m marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (const challenge of challenges) { | ||
if (challenge.name === challengeName) { | ||
challenge.passed = true; | ||
break; | ||
} | ||
} | ||
|
||
this._save(state); | ||
} | ||
|
||
/** | ||
* Merge the new state and the current state. | ||
* this function will compare the new state with the current state and finally overwrite the current state based on the new state: | ||
* - If the old key in the current state isn't in the new state, the old key will be removed from the current state. | ||
* - If the new key in the new state isn't in the current state, the new key will be added to the current state. | ||
* | ||
* @param {object} oldState - the current state stored in the local storage. | ||
* @param {object} newState - the latest state from the server. | ||
* @returns mergedState - the merged state. | ||
*/ | ||
_checkAndMerge(oldState, newState) { | ||
laike9m marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!newState && !oldState) { | ||
throw new Error('one of the new state and the old state is required.'); | ||
} | ||
|
||
if (!oldState && newState) { | ||
return newState; | ||
} | ||
|
||
if (!newState && oldState) { | ||
return oldState; | ||
} | ||
|
||
let mergedState = {}; | ||
const levels = ['basic', 'intermediate', 'advanced', 'extreme']; | ||
|
||
for (const level of levels) { | ||
// Initialize an empty array for merged challenges | ||
let mergedChallenges = []; | ||
|
||
// Create a map for quick lookup of challenges by name | ||
const oldChallengesMap = new Map(oldState[level].map(challenge => [challenge.name, challenge])); | ||
const newChallengesMap = new Map(newState[level].map(challenge => [challenge.name, challenge])); | ||
|
||
// Add or update challenges from the newState | ||
for (const [name, newChallenge] of newChallengesMap.entries()) { | ||
let hasPassed = oldChallengesMap.get(name)?.passed || newChallenge.passed; | ||
mergedChallenges.push({ ...newChallenge, passed: hasPassed }); | ||
} | ||
|
||
// Set the merged challenges for the current level in the mergedState | ||
mergedState[level] = mergedChallenges; | ||
} | ||
|
||
return mergedState; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.