Created
April 30, 2016 07:44
-
-
Save QuincyLarson/a9db8513fba3bca32d37ed421a414de7 to your computer and use it in GitHub Desktop.
This file contains 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
function redirectToNextChallenge(req, res, next) { | |
let challengeId = req.query.id || req.cookies.currentChallengeId; | |
if (badIdMap[challengeId]) { | |
challengeId = badIdMap[challengeId]; | |
} | |
if (!isMongoId('' + challengeId)) { | |
challengeId = null; | |
} | |
Observable.combineLatest( | |
firstChallenge$, | |
lastChallenge$ | |
) | |
.flatMap(([firstChallenge, { id: lastChallengeId } ]) => { | |
// no id supplied, load first challenge | |
if (!challengeId) { | |
return Observable.just(firstChallenge); | |
} | |
// camper just completed last challenge | |
if (challengeId === lastChallengeId) { | |
return Observable.just() | |
.doOnCompleted(() => { | |
req.flash('info', { | |
msg: dedent` | |
Once you have completed all of our challenges, you should | |
join our <a href="https://gitter.im/freecodecamp/HalfWayClub" | |
target="_blank">Half Way Club</a> and start getting | |
ready for our nonprofit projects. | |
`.split('\n').join(' ') | |
}); | |
return res.redirect('/map'); | |
}); | |
} | |
return getNextChallenge$(challenge$, blocks$, challengeId); | |
}) | |
.doOnNext(({ dashedName } = {}) => { | |
if (!dashedName) { | |
log('no challenge found for %s', challengeId); | |
res.redirect('/map'); | |
} | |
res.redirect('/challenges/' + dashedName); | |
}) | |
.subscribe(() => {}, next); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment