Added functuality to the edit button. Added router to /commets/:id/edit as well as commentEdit method in story js.

This commit is contained in:
MrRenter
2015-04-18 02:40:48 -04:00
parent 47bb301a91
commit 349dbbc2af
3 changed files with 76 additions and 4 deletions

5
app.js Normal file → Executable file
View File

@ -470,6 +470,11 @@ app.post(
storyController.commentOnCommentSubmit
);
app.post(
'/stories/comment/:id/edit',
storyController.commentEdit
);
app.get(
'/stories/submit',
storyController.submitNew

35
controllers/story.js Normal file → Executable file
View File

@ -423,6 +423,41 @@ exports.commentOnCommentSubmit = function(req, res, next) {
commentSave(comment, Comment, res, next);
};
exports.commentEdit = function(req, res, next){
if (req.user._id.toString() !== req.body.data.author.userId.toString()) {
return next(new Error('Not authorized'));
}
var data = req.params.id;
var sanitizedBody = sanitizeHtml(req.body.data.body,{
allowedTags: [],
allowedAttributes: []
}).replace(/"/g, '"');
if (req.body.data.body !== sanitizedBody) {
req.flash('errors', {
msg: 'HTML is not allowed'
});
return res.send(true);
}
Comment.find({'_id': data}, function(err, cmt) {
if (err) {
return next(err);
}
cmt = cmt.pop();
cmt.body = sanitizedBody;
cmt.commentOn = Date.now();
cmt.save(function (err) {
if (err) {
return next(err);
}
res.send(true);
});
//commentSave(comment, Comment, res, next);
});
};
function commentSave(comment, Context, res, next) {
comment.save(function(err, data) {
if (err) {

40
views/stories/comments.jade Normal file → Executable file
View File

@ -24,7 +24,7 @@
var editButton = "";
if (commentDetails.author.username === user.profile.username){
if ((Date.now() - commentDetails.commentOn) < 600000){
editButton = "<a class='btn btn-no-shadow btn-primary btn-xs btn-primary-ghost' id='test-" + commentDetails._id + "'>Edit</a> · ";
editButton = "<a class='btn btn-no-shadow btn-primary btn-xs btn-primary-ghost edit-btn' id='" + commentDetails._id + "'>Edit</a> · ";
}
}
$(div)
@ -61,7 +61,12 @@
complete: function () {
sentinel--;
if (!sentinel) {
$('.comment-a-comment').on('click', 'a', function () {
$('.comment-a-comment').on('click', 'a', function() {
alert($(this).hasClass("edit-btn"));
var editOrComment = 'comment';
if ($(this).hasClass("edit-btn")){
editOrComment = 'edit';
}
if (typeof user == "undefined" || !user) {
window.location.href = '/signin';
return;
@ -77,7 +82,7 @@
"<div class='input-group'>" +
"<input class='form-control big-text-field field-responsive' id='comment-to-comment-textinput' type='text' maxlength='140' autofocus />" +
"<span class='input-group-btn'>" +
"<button id='submit-comment-to-comment' class='btn btn-big btn-primary btn-responsive'>Send</button>" +
"<button id='submit-comment-to-" + editOrComment + "' class='btn btn-big btn-primary btn-responsive'>Send</button>" +
"</span>" +
"</div>" +
"</div>" +
@ -122,9 +127,36 @@
});
};
var submitCommentForEditToCommentHandler = function submitCommentForEditToCommentHandler() {
$('#submit-comment-to-edit').unbind('click');
console.log('in comments.jade', originalStoryAuthorEmail);
$.post('/stories/comment/' + commentId + '/edit',
{
data: {
associatedPost: commentId,
originalStoryLink: originalStoryLink,
originalStoryAuthorEmail: originalStoryAuthorEmail,
body: $('#comment-to-comment-textinput').val(),
author: {
picture: user.profile.picture,
userId: user._id,
username: user.profile.username,
email: user.email
}
}
})
.fail(function (xhr, textStatus, errorThrown) {
$('#submit-comment-to-edit').bind('click', submitCommentForEditToCommentHandler);
})
.done(function (data, textStatus, xhr) {
window.location.reload();
});
};
$('#submit-comment-to-edit').on('click', submitCommentForEditToCommentHandler)
$('#submit-comment-to-comment').on('click', submitCommentToCommentHandler);
});//
});
}
}
})