Rewrite challenge controller to use dashed names, update index.js to include new utility function getDashedNames
This commit is contained in:
@ -37,6 +37,7 @@ var R = require('ramda'),
|
|||||||
|
|
||||||
var challengeMapWithNames = utils.getChallengeMapWithNames();
|
var challengeMapWithNames = utils.getChallengeMapWithNames();
|
||||||
var challengeMapWithIds = utils.getChallengeMapWithIds();
|
var challengeMapWithIds = utils.getChallengeMapWithIds();
|
||||||
|
var challengeMapWithDashedNames = utils.getChallengeMapWithDashedNames();
|
||||||
|
|
||||||
|
|
||||||
function getMDNlinks(links) {
|
function getMDNlinks(links) {
|
||||||
@ -108,26 +109,22 @@ module.exports = function(app) {
|
|||||||
if (indexOfChallenge + 1
|
if (indexOfChallenge + 1
|
||||||
< challengeMapWithIds[challengeBlock].length) {
|
< challengeMapWithIds[challengeBlock].length) {
|
||||||
nextChallengeName =
|
nextChallengeName =
|
||||||
challengeMapWithNames[challengeBlock][++indexOfChallenge];
|
challengeMapWithDashedNames[challengeBlock][++indexOfChallenge];
|
||||||
} else if (typeof challengeMapWithIds[++challengeBlock] !== 'undefined') {
|
} else if (typeof challengeMapWithIds[++challengeBlock] !== 'undefined') {
|
||||||
nextChallengeName = R.head(challengeMapWithNames[challengeBlock]);
|
nextChallengeName = R.head(challengeMapWithDashedNames[challengeBlock]);
|
||||||
} else {
|
} else {
|
||||||
req.flash('errors', {
|
req.flash('errors', {
|
||||||
msg: 'It looks like you have finished all of our challenges.' +
|
msg: 'It looks like you have finished all of our challenges.' +
|
||||||
' Great job! Now on to helping nonprofits!'
|
' 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) {
|
req.user.save(function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(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.challengeId = challengeMapWithIds['0'][0];
|
||||||
req.user.currentChallenge.challengeName = challengeMapWithNames['0'][0];
|
req.user.currentChallenge.challengeName = challengeMapWithNames['0'][0];
|
||||||
req.user.currentChallenge.challengeBlock = '0';
|
req.user.currentChallenge.challengeBlock = '0';
|
||||||
|
req.user.currentChallenge.dashedName =
|
||||||
|
challengeMapWithDashedNames['0'][0];
|
||||||
req.user.save(function(err) {
|
req.user.save(function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
var nameString = req.user.currentChallenge.challengeName.trim()
|
|
||||||
.toLowerCase()
|
var nameString = req.user.currentChallenge.dashedName =
|
||||||
.replace(/\s/g, '-')
|
challengeMapWithDashedNames['0'][0];
|
||||||
.replace(/[^a-z0-9\-\/.]/gi, '');
|
|
||||||
req.user.save(function(err) {
|
req.user.save(function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
@ -172,12 +171,12 @@ module.exports = function(app) {
|
|||||||
var dashedName = req.params.challengeName;
|
var dashedName = req.params.challengeName;
|
||||||
|
|
||||||
Challenge.findOne(
|
Challenge.findOne(
|
||||||
{ dashedName: challengeName },
|
{ where: { dashedName: dashedName }},
|
||||||
function(err, challenge) {
|
function(err, challenge) {
|
||||||
if (err) { return next(err); }
|
if (err) { return next(err); }
|
||||||
|
|
||||||
// Handle not found
|
// Handle not found
|
||||||
if (!challengeFromMongo) {
|
if (!challenge) {
|
||||||
req.flash('errors', {
|
req.flash('errors', {
|
||||||
msg: '404: We couldn\'t find a challenge with that name. ' +
|
msg: '404: We couldn\'t find a challenge with that name. ' +
|
||||||
'Please double check the name.'
|
'Please double check the name.'
|
||||||
@ -188,7 +187,8 @@ module.exports = function(app) {
|
|||||||
if (req.user) {
|
if (req.user) {
|
||||||
req.user.currentChallenge = {
|
req.user.currentChallenge = {
|
||||||
challengeId: challenge.id,
|
challengeId: challenge.id,
|
||||||
challengeName: challenge.dashedName,
|
challengeName: challenge.name,
|
||||||
|
dashedName: challenge.dashedName,
|
||||||
challengeBlock: R.head(R.flatten(Object.keys(challengeMapWithIds).
|
challengeBlock: R.head(R.flatten(Object.keys(challengeMapWithIds).
|
||||||
map(function (key) {
|
map(function (key) {
|
||||||
return challengeMapWithIds[key]
|
return challengeMapWithIds[key]
|
||||||
|
@ -16,7 +16,8 @@ var path = require('path'),
|
|||||||
*/
|
*/
|
||||||
var allFieldGuideIds, allFieldGuideNames, allNonprofitNames,
|
var allFieldGuideIds, allFieldGuideNames, allNonprofitNames,
|
||||||
challengeMap, challengeMapForDisplay, challengeMapWithIds,
|
challengeMap, challengeMapForDisplay, challengeMapWithIds,
|
||||||
challengeMapWithNames, allChallengeIds, allChallenges;
|
challengeMapWithNames, allChallengeIds, allChallenges,
|
||||||
|
challengeMapWithDashedNames;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET /
|
* GET /
|
||||||
@ -98,17 +99,6 @@ module.exports = {
|
|||||||
return allChallengeIds;
|
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 () {
|
getChallengeMapWithNames: function () {
|
||||||
if (!challengeMapWithNames) {
|
if (!challengeMapWithNames) {
|
||||||
challengeMapWithNames = {};
|
challengeMapWithNames = {};
|
||||||
@ -123,6 +113,20 @@ module.exports = {
|
|||||||
return challengeMapWithNames;
|
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 () {
|
randomPhrase: function () {
|
||||||
return resources.phrases[
|
return resources.phrases[
|
||||||
|
Reference in New Issue
Block a user