create infrastructure for advancing through wiki
This commit is contained in:
13
app.js
13
app.js
@ -515,15 +515,12 @@ app.post('/completed-bonfire/', bonfireController.completedBonfire);
|
||||
* Wiki related routes
|
||||
*/
|
||||
|
||||
app.get(
|
||||
'/wiki/:wikiName',
|
||||
wikiController.returnIndividualWiki
|
||||
);
|
||||
|
||||
app.get(
|
||||
'/wiki',
|
||||
wikiController.returnHomeWiki
|
||||
);
|
||||
app.get('/wiki/:wikiName', wikiController.returnIndividualWiki);
|
||||
|
||||
app.get('/wiki', wikiController.returnNextWiki);
|
||||
|
||||
app.post('/completed-wiki/', wikiController.completedWiki);
|
||||
|
||||
|
||||
/**
|
||||
|
@ -239,6 +239,17 @@ module.exports = {
|
||||
});
|
||||
},
|
||||
|
||||
allWikiIds: function() {
|
||||
return wikis.map(function(elem) {
|
||||
return {
|
||||
_id: elem._id,
|
||||
}
|
||||
})
|
||||
.map(function(elem) {
|
||||
return elem._id;
|
||||
});
|
||||
},
|
||||
|
||||
allBonfireNames: function() {
|
||||
return bonfires.map(function(elem) {
|
||||
return {
|
||||
|
@ -29,6 +29,7 @@ exports.returnIndividualWiki = function(req, res, next) {
|
||||
}
|
||||
res.render('wiki/show', {
|
||||
title: wiki.name,
|
||||
wikiId: wiki._id,
|
||||
description: wiki.description.join('')
|
||||
});
|
||||
});
|
||||
@ -40,30 +41,57 @@ exports.showAllWikis = function(req, res) {
|
||||
res.send(data);
|
||||
};
|
||||
|
||||
exports.returnHomeWiki = function(req, res) {
|
||||
var dashedName = req.params.wikiName;
|
||||
exports.returnNextWiki = function(req, res, next) {
|
||||
if (!req.user) {
|
||||
return res.redirect('../wiki/a-guide-to-our-wiki');
|
||||
}
|
||||
|
||||
Wiki.find({'name': 'A Guide to our Wiki'}, function(err, wiki) {
|
||||
if (err) {
|
||||
next(err);
|
||||
}
|
||||
var completed = req.user.completedWikis;
|
||||
|
||||
if (wiki.length < 1) {
|
||||
req.flash('errors', {
|
||||
msg: "404: We couldn't find a wiki entry with that name. Please double check the name."
|
||||
});
|
||||
req.user.uncompletedWikis = resources.allWikiIds().filter(function (elem) {
|
||||
if (completed.indexOf(elem) === -1) {
|
||||
return elem;
|
||||
}
|
||||
});
|
||||
req.user.save();
|
||||
|
||||
return res.redirect('/wiki');
|
||||
}
|
||||
var uncompletedWikis = req.user.uncompletedWikis;
|
||||
|
||||
wiki = wiki.pop();
|
||||
var dashedNameFull = wiki.name.toLowerCase().replace(/\s/g, '-');
|
||||
if (dashedNameFull != dashedName) {
|
||||
return res.redirect('../wiki/' + dashedNameFull);
|
||||
}
|
||||
res.render('wiki/show', {
|
||||
title: wiki.name,
|
||||
description: wiki.description.join('')
|
||||
});
|
||||
});
|
||||
var displayedWikis = Wiki.find({'_id': uncompletedWikis[0]});
|
||||
displayedWikis.exec(function(err, wiki) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
wiki = wiki.pop();
|
||||
if (wiki === undefined) {
|
||||
req.flash('errors', {
|
||||
msg: "It looks like you've read all our current Wiki entries. Let us know if you'd like to contribute to our wiki!"
|
||||
});
|
||||
return res.redirect('../wiki/a-guide-to-our-wiki');
|
||||
}
|
||||
var nameString = wiki.name.toLowerCase().replace(/\s/g, '-');
|
||||
return res.redirect('../wiki/' + nameString);
|
||||
});
|
||||
};
|
||||
|
||||
exports.completedWiki = function (req, res, next) {
|
||||
debug('params in completedWiki', req.params);
|
||||
var wikiId = req.body.wikiInfo.wikiId;
|
||||
|
||||
req.user.completedWikis.push(wikiId);
|
||||
|
||||
var index = req.user.uncompletedWikis.indexOf(wikiId);
|
||||
if (index > -1) {
|
||||
req.user.progressTimestamps.push(Date.now() || 0);
|
||||
req.user.uncompletedWikis.splice(index, 1);
|
||||
}
|
||||
|
||||
req.user.save(function (err, user) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
if (user) {
|
||||
res.send(true);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
@ -127,6 +127,8 @@ var userSchema = new mongoose.Schema({
|
||||
verified: Boolean
|
||||
}
|
||||
],
|
||||
completedWikis: [],
|
||||
uncompletedWikis: [],
|
||||
currentStreak: {
|
||||
type: Number,
|
||||
default: 0
|
||||
|
@ -52,6 +52,23 @@ $(document).ready(function() {
|
||||
}
|
||||
}
|
||||
|
||||
function completedWiki(wikiId) {
|
||||
if ($('.signup-btn-nav').length < 1) {
|
||||
$.post(
|
||||
'/completed-wiki',
|
||||
{
|
||||
wikiInfo: {
|
||||
wikiId: wikiId
|
||||
}
|
||||
},
|
||||
function(res) {
|
||||
if (res) {
|
||||
window.location.href = '/wiki'
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
$('.next-bonfire-button').on('click', function() {
|
||||
var bonfireSolution = myCodeMirror.getValue();
|
||||
var thisBonfireHash = passedBonfireHash || null;
|
||||
@ -61,6 +78,11 @@ $(document).ready(function() {
|
||||
|
||||
});
|
||||
|
||||
$('.next-wiki-button').on('click', function() {
|
||||
var wikiId = $('#wikiId').text();
|
||||
completedWiki(wikiId);
|
||||
});
|
||||
|
||||
$("img").error(function () {
|
||||
$(this).unbind("error").attr("src", "https://s3.amazonaws.com/freecodecamp/camper-image-placeholder.png");
|
||||
});
|
||||
|
@ -10,7 +10,9 @@ block content
|
||||
div!= description
|
||||
.spacer
|
||||
.text-center
|
||||
#showAllButton.btn.btn-info.btn-big Show all Wiki Articles
|
||||
.next-wiki-button.btn.btn-primary.btn-big Take me to the next article
|
||||
.ten-pixel-break
|
||||
#showAllButton.btn.btn-info.btn-big Show all wiki articles
|
||||
.spacer
|
||||
.row
|
||||
.col-xs-12.text-center
|
||||
@ -24,3 +26,4 @@ block content
|
||||
a.close.closing-x(href='#', data-dismiss='modal', aria-hidden='true') ×
|
||||
.modal-body
|
||||
include ../partials/wikis
|
||||
#wikiId.hidden= wikiId
|
||||
|
Reference in New Issue
Block a user