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:
5
app.js
Normal file → Executable file
5
app.js
Normal file → Executable file
@ -470,6 +470,11 @@ app.post(
|
|||||||
storyController.commentOnCommentSubmit
|
storyController.commentOnCommentSubmit
|
||||||
);
|
);
|
||||||
|
|
||||||
|
app.post(
|
||||||
|
'/stories/comment/:id/edit',
|
||||||
|
storyController.commentEdit
|
||||||
|
);
|
||||||
|
|
||||||
app.get(
|
app.get(
|
||||||
'/stories/submit',
|
'/stories/submit',
|
||||||
storyController.submitNew
|
storyController.submitNew
|
||||||
|
35
controllers/story.js
Normal file → Executable file
35
controllers/story.js
Normal file → Executable file
@ -423,6 +423,41 @@ exports.commentOnCommentSubmit = function(req, res, next) {
|
|||||||
commentSave(comment, Comment, 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) {
|
function commentSave(comment, Context, res, next) {
|
||||||
comment.save(function(err, data) {
|
comment.save(function(err, data) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
38
views/stories/comments.jade
Normal file → Executable file
38
views/stories/comments.jade
Normal file → Executable file
@ -24,7 +24,7 @@
|
|||||||
var editButton = "";
|
var editButton = "";
|
||||||
if (commentDetails.author.username === user.profile.username){
|
if (commentDetails.author.username === user.profile.username){
|
||||||
if ((Date.now() - commentDetails.commentOn) < 600000){
|
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)
|
$(div)
|
||||||
@ -62,6 +62,11 @@
|
|||||||
sentinel--;
|
sentinel--;
|
||||||
if (!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) {
|
if (typeof user == "undefined" || !user) {
|
||||||
window.location.href = '/signin';
|
window.location.href = '/signin';
|
||||||
return;
|
return;
|
||||||
@ -77,7 +82,7 @@
|
|||||||
"<div class='input-group'>" +
|
"<div class='input-group'>" +
|
||||||
"<input class='form-control big-text-field field-responsive' id='comment-to-comment-textinput' type='text' maxlength='140' autofocus />" +
|
"<input class='form-control big-text-field field-responsive' id='comment-to-comment-textinput' type='text' maxlength='140' autofocus />" +
|
||||||
"<span class='input-group-btn'>" +
|
"<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>" +
|
"</span>" +
|
||||||
"</div>" +
|
"</div>" +
|
||||||
"</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);
|
$('#submit-comment-to-comment').on('click', submitCommentToCommentHandler);
|
||||||
});//
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user