From 349dbbc2af22bfc1304154b6b3b7159461baa3e2 Mon Sep 17 00:00:00 2001 From: MrRenter Date: Sat, 18 Apr 2015 02:40:48 -0400 Subject: [PATCH] Added functuality to the edit button. Added router to /commets/:id/edit as well as commentEdit method in story js. --- app.js | 5 +++++ controllers/story.js | 35 ++++++++++++++++++++++++++++++++ views/stories/comments.jade | 40 +++++++++++++++++++++++++++++++++---- 3 files changed, 76 insertions(+), 4 deletions(-) mode change 100644 => 100755 app.js mode change 100644 => 100755 controllers/story.js mode change 100644 => 100755 views/stories/comments.jade diff --git a/app.js b/app.js old mode 100644 new mode 100755 index f03046711b..eadf61888e --- a/app.js +++ b/app.js @@ -470,6 +470,11 @@ app.post( storyController.commentOnCommentSubmit ); +app.post( + '/stories/comment/:id/edit', + storyController.commentEdit +); + app.get( '/stories/submit', storyController.submitNew diff --git a/controllers/story.js b/controllers/story.js old mode 100644 new mode 100755 index 34b8693890..4ab8351393 --- a/controllers/story.js +++ b/controllers/story.js @@ -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) { diff --git a/views/stories/comments.jade b/views/stories/comments.jade old mode 100644 new mode 100755 index a47a969c95..43d1701288 --- a/views/stories/comments.jade +++ b/views/stories/comments.jade @@ -24,7 +24,7 @@ var editButton = ""; if (commentDetails.author.username === user.profile.username){ if ((Date.now() - commentDetails.commentOn) < 600000){ - editButton = "Edit · "; + editButton = "Edit · "; } } $(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 @@ "
" + "" + "" + - "" + + "" + "" + "
" + "" + @@ -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); - });// + }); } } })