Rewrite challenge controller to use dashed names, update index.js to include new utility function getDashedNames

This commit is contained in:
terakilobyte
2015-06-15 18:50:18 -04:00
parent a334b9870c
commit c136895064
2 changed files with 31 additions and 27 deletions

View File

@ -37,6 +37,7 @@ var R = require('ramda'),
var challengeMapWithNames = utils.getChallengeMapWithNames();
var challengeMapWithIds = utils.getChallengeMapWithIds();
var challengeMapWithDashedNames = utils.getChallengeMapWithDashedNames();
function getMDNlinks(links) {
@ -108,26 +109,22 @@ module.exports = function(app) {
if (indexOfChallenge + 1
< challengeMapWithIds[challengeBlock].length) {
nextChallengeName =
challengeMapWithNames[challengeBlock][++indexOfChallenge];
challengeMapWithDashedNames[challengeBlock][++indexOfChallenge];
} else if (typeof challengeMapWithIds[++challengeBlock] !== 'undefined') {
nextChallengeName = R.head(challengeMapWithNames[challengeBlock]);
nextChallengeName = R.head(challengeMapWithDashedNames[challengeBlock]);
} else {
req.flash('errors', {
msg: 'It looks like you have finished all of our challenges.' +
' Great job! Now on to helping nonprofits!'
});
nextChallengeName = R.head(challengeMapWithNames[0].challenges);
nextChallengeName = R.head(challengeMapWithDashedNames[0].challenges);
}
var nameString = nextChallengeName.trim()
.toLowerCase()
.replace(/\s/g, '-');
req.user.save(function(err) {
if (err) {
return next(err);
}
return res.redirect('../challenges/' + nameString);
return res.redirect('../challenges/' + nextChallengeName);
});
}
@ -150,16 +147,18 @@ module.exports = function(app) {
req.user.currentChallenge.challengeId = challengeMapWithIds['0'][0];
req.user.currentChallenge.challengeName = challengeMapWithNames['0'][0];
req.user.currentChallenge.challengeBlock = '0';
req.user.currentChallenge.dashedName =
challengeMapWithDashedNames['0'][0];
req.user.save(function(err) {
if (err) {
return next(err);
}
});
}
var nameString = req.user.currentChallenge.challengeName.trim()
.toLowerCase()
.replace(/\s/g, '-')
.replace(/[^a-z0-9\-\/.]/gi, '');
var nameString = req.user.currentChallenge.dashedName =
challengeMapWithDashedNames['0'][0];
req.user.save(function(err) {
if (err) {
return next(err);
@ -172,12 +171,12 @@ module.exports = function(app) {
var dashedName = req.params.challengeName;
Challenge.findOne(
{ dashedName: challengeName },
{ where: { dashedName: dashedName }},
function(err, challenge) {
if (err) { return next(err); }
// Handle not found
if (!challengeFromMongo) {
if (!challenge) {
req.flash('errors', {
msg: '404: We couldn\'t find a challenge with that name. ' +
'Please double check the name.'
@ -188,7 +187,8 @@ module.exports = function(app) {
if (req.user) {
req.user.currentChallenge = {
challengeId: challenge.id,
challengeName: challenge.dashedName,
challengeName: challenge.name,
dashedName: challenge.dashedName,
challengeBlock: R.head(R.flatten(Object.keys(challengeMapWithIds).
map(function (key) {
return challengeMapWithIds[key]

View File

@ -16,7 +16,8 @@ var path = require('path'),
*/
var allFieldGuideIds, allFieldGuideNames, allNonprofitNames,
challengeMap, challengeMapForDisplay, challengeMapWithIds,
challengeMapWithNames, allChallengeIds, allChallenges;
challengeMapWithNames, allChallengeIds, allChallenges,
challengeMapWithDashedNames;
/**
* GET /
@ -98,17 +99,6 @@ module.exports = {
return allChallengeIds;
},
allChallenges: function () {
if (!allChallenges) {
allChallenges = [];
Object.keys(this.getChallengeMapWithNames()).forEach(function (key) {
allChallenges.push(challengeMap[key].challenges);
});
allChallenges = R.flatten(allChallenges);
}
return allChallenges;
},
getChallengeMapWithNames: function () {
if (!challengeMapWithNames) {
challengeMapWithNames = {};
@ -123,6 +113,20 @@ module.exports = {
return challengeMapWithNames;
},
getChallengeMapWithDashedNames: function() {
if (!challengeMapWithDashedNames) {
challengeMapWithDashedNames = {};
Object.keys(challengeMap).
forEach(function (key) {
var onlyNames = challengeMap[key].challenges.map(function (elem) {
return elem.dashedName;
});
challengeMapWithDashedNames[key] = onlyNames;
});
}
return challengeMapWithDashedNames;
},
randomPhrase: function () {
return resources.phrases[