Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save QuincyLarson/a9db8513fba3bca32d37ed421a414de7 to your computer and use it in GitHub Desktop.
Save QuincyLarson/a9db8513fba3bca32d37ed421a414de7 to your computer and use it in GitHub Desktop.
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