Merge pull request #3557 from FreeCodeCamp/feature/make-completed-challenges-unique
feature make completedChallenges uniq
This commit is contained in:
@ -88,39 +88,6 @@
|
|||||||
"google": {
|
"google": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
"completedBonfires": {
|
|
||||||
"type": [
|
|
||||||
{
|
|
||||||
"id": "string",
|
|
||||||
"name": "string",
|
|
||||||
"completedWith": "string",
|
|
||||||
"completedDate": "string",
|
|
||||||
"solution": "string"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"default": []
|
|
||||||
},
|
|
||||||
"uncompletedCoursewares": {
|
|
||||||
"type": "array",
|
|
||||||
"default": []
|
|
||||||
},
|
|
||||||
"completedCoursewares": {
|
|
||||||
"type": [
|
|
||||||
{
|
|
||||||
"completedDate": {
|
|
||||||
"type": "string",
|
|
||||||
"defaultFn": "now"
|
|
||||||
},
|
|
||||||
"id": "string",
|
|
||||||
"name": "string",
|
|
||||||
"completedWith": "string",
|
|
||||||
"solution": "string",
|
|
||||||
"githubLink": "string",
|
|
||||||
"verified": "boolean"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"default": []
|
|
||||||
},
|
|
||||||
"currentStreak": {
|
"currentStreak": {
|
||||||
"type": "number",
|
"type": "number",
|
||||||
"default": 0
|
"default": 0
|
||||||
@ -140,10 +107,15 @@
|
|||||||
"currentChallenge": {
|
"currentChallenge": {
|
||||||
"type": {}
|
"type": {}
|
||||||
},
|
},
|
||||||
|
"isUniqMigrated": {
|
||||||
|
"type": "boolean",
|
||||||
|
"default": false
|
||||||
|
},
|
||||||
"completedChallenges": {
|
"completedChallenges": {
|
||||||
"type": [
|
"type": [
|
||||||
{
|
{
|
||||||
"completedDate": "number",
|
"completedDate": "number",
|
||||||
|
"lastUpdated": "number",
|
||||||
"id": "string",
|
"id": "string",
|
||||||
"name": "string",
|
"name": "string",
|
||||||
"completedWith": "string",
|
"completedWith": "string",
|
||||||
|
@ -34,22 +34,52 @@ const dasherize = utils.dasherize;
|
|||||||
const unDasherize = utils.unDasherize;
|
const unDasherize = utils.unDasherize;
|
||||||
const getMDNLinks = utils.getMDNLinks;
|
const getMDNLinks = utils.getMDNLinks;
|
||||||
|
|
||||||
|
function makeChallengesUnique(challengeArr) {
|
||||||
|
// clone and reverse challenges
|
||||||
|
// then filter by unique id's
|
||||||
|
// then reverse again
|
||||||
|
return _.uniq(challengeArr.slice().reverse(), 'id').reverse();
|
||||||
|
}
|
||||||
function numberWithCommas(x) {
|
function numberWithCommas(x) {
|
||||||
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateUserProgress(user, challengeId, completedChallenge) {
|
function updateUserProgress(user, challengeId, completedChallenge) {
|
||||||
const alreadyCompleted = user.completedChallenges.some(({ id }) => {
|
let { completedChallenges } = user;
|
||||||
return id === challengeId;
|
|
||||||
|
// migrate user challenges object to remove
|
||||||
|
if (!user.isUniqMigrated) {
|
||||||
|
user.isUniqMigrated = true;
|
||||||
|
|
||||||
|
completedChallenges = user.completedChallenges =
|
||||||
|
makeChallengesUnique(completedChallenges);
|
||||||
|
}
|
||||||
|
|
||||||
|
const indexOfChallenge = _.findIndex(completedChallenges, {
|
||||||
|
id: challengeId
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const alreadyCompleted = indexOfChallenge !== -1;
|
||||||
|
|
||||||
if (!alreadyCompleted) {
|
if (!alreadyCompleted) {
|
||||||
user.progressTimestamps.push({
|
user.progressTimestamps.push({
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
completedChallenge
|
completedChallenge: challengeId
|
||||||
});
|
});
|
||||||
|
user.completedChallenges.push(completedChallenge);
|
||||||
|
return user;
|
||||||
}
|
}
|
||||||
user.completedChallenges.push(completedChallenge);
|
|
||||||
|
const oldCompletedChallenge = completedChallenges[indexOfChallenge];
|
||||||
|
user.completedChallenges[indexOfChallenge] =
|
||||||
|
Object.assign(
|
||||||
|
{},
|
||||||
|
completedChallenge,
|
||||||
|
{
|
||||||
|
completedDate: oldCompletedChallenge.completedDate,
|
||||||
|
lastUpdated: completedChallenge.completedDate
|
||||||
|
}
|
||||||
|
);
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user