refactor returnIndividualBonfires

This commit is contained in:
Berkeley Martinez
2015-06-20 11:43:12 -07:00
parent 50a0ad3595
commit 43ae70465b
2 changed files with 90 additions and 139 deletions

View File

@ -32,7 +32,9 @@
var R = require('ramda'), var R = require('ramda'),
utils = require('../utils'), utils = require('../utils'),
userMigration = require('../utils/middleware').userMigration; saveUser = require('../utils/rx').saveUser,
userMigration = require('../utils/middleware').userMigration,
ifNoUserRedirectTo = require('../utils/middleware').ifNoUserRedirectTo;
var challengeMapWithNames = utils.getChallengeMapWithNames(); var challengeMapWithNames = utils.getChallengeMapWithNames();
var challengeMapWithIds = utils.getChallengeMapWithIds(); var challengeMapWithIds = utils.getChallengeMapWithIds();
@ -51,17 +53,24 @@ module.exports = function(app) {
// the follow routes are covered by userMigration // the follow routes are covered by userMigration
router.use(userMigration); router.use(userMigration);
router.get('/challenges/next-challenge', returnNextChallenge);
router.get('/challenges/:challengeName', returnIndividualChallenge);
router.get('/challenges/', returnCurrentChallenge);
router.get('/map', challengeMap); router.get('/map', challengeMap);
router.get(
'/challenges/next-challenge',
ifNoUserRedirectTo('../challenges/learn-how-free-code-camp-works'),
returnNextChallenge
);
router.get('/challenges/:challengeName', returnIndividualChallenge);
router.get(
'/challenges/',
ifNoUserRedirectTo('../challenges/learn-how-free-code-camp-works'),
returnCurrentChallenge
);
app.use(router); app.use(router);
function returnNextChallenge(req, res, next) { function returnNextChallenge(req, res, next) {
if (!req.user) {
return res.redirect('../challenges/learn-how-free-code-camp-works');
}
var completed = req.user.completedChallenges.map(function (elem) { var completed = req.user.completedChallenges.map(function (elem) {
return elem.id; return elem.id;
}); });
@ -100,18 +109,17 @@ module.exports = function(app) {
nextChallengeName = R.head(challengeMapWithDashedNames[0].challenges); nextChallengeName = R.head(challengeMapWithDashedNames[0].challenges);
} }
req.user.save(function(err) { saveUser(req.user)
if (err) { .subscribe(
return next(err); function() {},
next,
function() {
res.redirect('../challenges/' + nextChallengeName);
} }
return res.redirect('../challenges/' + nextChallengeName); );
});
} }
function returnCurrentChallenge(req, res, next) { function returnCurrentChallenge(req, res, next) {
if (!req.user) {
return res.redirect('../challenges/learn-how-free-code-camp-works');
}
var completed = req.user.completedChallenges.map(function (elem) { var completed = req.user.completedChallenges.map(function (elem) {
return elem.id; return elem.id;
}); });
@ -133,12 +141,14 @@ module.exports = function(app) {
var nameString = req.user.currentChallenge.dashedName; var nameString = req.user.currentChallenge.dashedName;
req.user.save(function(err) { saveUser(req.user)
if (err) { .subscribe(
return next(err); function() {},
next,
function() {
res.redirect('../challenges/' + nameString);
} }
return res.redirect('../challenges/' + nameString); );
});
} }
function returnIndividualChallenge(req, res, next) { function returnIndividualChallenge(req, res, next) {
@ -152,8 +162,10 @@ module.exports = function(app) {
// Handle not found // Handle not found
if (!challenge) { if (!challenge) {
req.flash('errors', { req.flash('errors', {
msg: '404: We couldn\'t find a challenge with that name. ' + msg:
'Please double check the name.' '404: We couldn\'t find a challenge with the name `' +
dashedName +
'` Please double check the name.'
}); });
return res.redirect('/challenges'); return res.redirect('/challenges');
} }
@ -167,8 +179,9 @@ module.exports = function(app) {
map(function (key) { map(function (key) {
return challengeMapWithIds[key] return challengeMapWithIds[key]
.filter(function (elem) { .filter(function (elem) {
return String(elem) === String(challenge.id); return String(elem) === challenge.id;
}).map(function () { })
.map(function () {
return key; return key;
}); });
}) })
@ -176,13 +189,10 @@ module.exports = function(app) {
}; };
} }
var challengeType = { var commonLocals = {
0: function() {
res.render('coursewares/showHTML', {
title: challenge.name, title: challenge.name,
dashedName: dashedName, dashedName: dashedName,
name: challenge.name, name: challenge.name,
brief: challenge.description[0],
details: challenge.description.slice(1), details: challenge.description.slice(1),
tests: challenge.tests, tests: challenge.tests,
challengeSeed: challenge.challengeSeed, challengeSeed: challenge.challengeSeed,
@ -190,106 +200,37 @@ module.exports = function(app) {
phrase: utils.randomPhrase(), phrase: utils.randomPhrase(),
compliment: utils.randomCompliment(), compliment: utils.randomCompliment(),
challengeId: challenge.id, challengeId: challenge.id,
environment: utils.whichEnvironment(), challengeType: challenge.challengeType,
challengeType: challenge.challengeType // video challenges
});
},
1: function() {
res.render('coursewares/showJS', {
title: challenge.name,
dashedName: dashedName,
name: challenge.name,
brief: challenge.description[0],
details: challenge.description.slice(1),
tests: challenge.tests,
challengeSeed: challenge.challengeSeed,
verb: utils.randomVerb(),
phrase: utils.randomPhrase(),
compliment: utils.randomCompliment(),
challengeId: challenge.id,
challengeType: challenge.challengeType
});
},
2: function() {
res.render('coursewares/showVideo', {
title: challenge.name,
dashedName: dashedName,
name: challenge.name,
details: challenge.description,
tests: challenge.tests,
video: challenge.challengeSeed[0], video: challenge.challengeSeed[0],
verb: utils.randomVerb(), // bonfires specific
phrase: utils.randomPhrase(),
compliment: utils.randomCompliment(),
challengeId: challenge.id,
challengeType: challenge.challengeType
});
},
3: function() {
res.render('coursewares/showZiplineOrBasejump', {
title: challenge.name,
dashedName: dashedName,
name: challenge.name,
details: challenge.description,
video: challenge.challengeSeed[0],
verb: utils.randomVerb(),
phrase: utils.randomPhrase(),
compliment: utils.randomCompliment(),
challengeId: challenge.id,
challengeType: challenge.challengeType
});
},
4: function() {
res.render('coursewares/showZiplineOrBasejump', {
title: challenge.name,
dashedName: dashedName,
name: challenge.name,
details: challenge.description,
video: challenge.challengeSeed[0],
verb: utils.randomVerb(),
phrase: utils.randomPhrase(),
compliment: utils.randomCompliment(),
challengeId: challenge.id,
challengeType: challenge.challengeType
});
},
5: function() {
res.render('coursewares/showBonfire', {
completedWith: null,
title: challenge.name,
dashedName: dashedName,
name: challenge.name,
difficulty: Math.floor(+challenge.difficulty), difficulty: Math.floor(+challenge.difficulty),
brief: challenge.description.shift(), brief: challenge.description.shift(),
details: challenge.description,
tests: challenge.tests,
challengeSeed: challenge.challengeSeed,
verb: utils.randomVerb(),
phrase: utils.randomPhrase(),
compliment: utils.randomCompliment(),
bonfires: challenge, bonfires: challenge,
challengeId: challenge.id,
MDNkeys: challenge.MDNlinks, MDNkeys: challenge.MDNlinks,
MDNlinks: getMDNLinks(challenge.MDNlinks), MDNlinks: getMDNLinks(challenge.MDNlinks),
challengeType: challenge.challengeType // htmls specific
}); environment: utils.whichEnvironment()
}
}; };
if (req.user) {
req.user.save(function (err) { var challengeView = {
if (err) { 0: 'coursewares/showHTML',
return next(err); 1: 'coursewares/showJS',
} 2: 'coursewares/showVideo',
return challengeType[challenge.challengeType](); 3: 'coursewares/showZiplineOrBasejump',
}); 4: 'coursewares/showZiplineOrBasejump',
} else { 5: 'coursewares/showBonfire'
return challengeType[challenge.challengeType](); };
saveUser(req.user)
.subscribe(
function() {},
next,
function() {
var view = challengeView[challenge.challengeType];
res.render(view, commonLocals);
} }
);
}); });
} }

View File

@ -33,3 +33,13 @@ exports.userMigration = function userMigration(req, res, next) {
); );
return next(); return next();
}; };
exports.ifNoUserRedirectTo = function ifNoUserRedirectTo(url) {
return function(req, res, next) {
if (req.user) {
return next();
}
return res.redirect(url);
};
};