diff --git a/app/assets/javascripts/moderation/comment.coffee b/app/assets/javascripts/moderation/comment.coffee index 393b1744..27d7544c 100644 --- a/app/assets/javascripts/moderation/comment.coffee +++ b/app/assets/javascripts/moderation/comment.coffee @@ -63,3 +63,24 @@ $(document).on "input", "input[name=mod-comment-new]", (evt) -> else ctr.removeClass 'text-danger' ctr.addClass 'text-muted' + + +# destroy +$(document).on "click", "a[data-action=mod-comment-destroy]", (ev) -> + ev.preventDefault() + if confirm 'Are you sure?' + btn = $(this) + cid = btn[0].dataset.id + $.ajax + url: '/ajax/mod/destroy_comment' + type: 'POST' + data: + comment: cid + success: (data, status, jqxhr) -> + if data.success + $("li[data-comment-id=#{cid}]").slideUp() + showNotification data.message, data.success + error: (jqxhr, status, error) -> + console.log jqxhr, status, error + showNotification "An error occurred, a developer should check the console for details", false + complete: (jqxhr, status) -> diff --git a/app/controllers/ajax/moderation_controller.rb b/app/controllers/ajax/moderation_controller.rb index 1b6aff36..32f6895c 100644 --- a/app/controllers/ajax/moderation_controller.rb +++ b/app/controllers/ajax/moderation_controller.rb @@ -85,6 +85,23 @@ class Ajax::ModerationController < ApplicationController end def destroy_comment + params.require :comment + @status = :err + @success = false + comment = ModerationComment.find(params[:comment]) + + unless current_user == comment.user + @status = :nopriv + @message = "can't delete other people's comments" + @success = false + return + end + + comment.destroy + + @status = :okay + @message = "Successfully deleted comment." + @success = true end end diff --git a/app/views/moderation/_discussion.html.haml b/app/views/moderation/_discussion.html.haml index f2b8b707..0a027595 100644 --- a/app/views/moderation/_discussion.html.haml +++ b/app/views/moderation/_discussion.html.haml @@ -9,15 +9,16 @@ %img.img-rounded.answerbox--img{src: gravatar_url(comment.user)} .media-body.comments--body %h6.media-heading.answerbox--question-user= user_screen_name comment.user - .pull-right - .btn-group - %button.btn.btn-link.btn-sm.dropdown-toggle{data: { toggle: :dropdown }, aria: { expanded: :false }} - %span.caret - %ul.dropdown-menu.dropdown-menu-right{role: :menu} - %li.text-danger - %a{href: '#', name: 'mod-comment-destroy', data: { id: comment.id }} - %i.fa.fa-trash-o - Delete + - if comment.user == current_user + .pull-right + .btn-group + %button.btn.btn-link.btn-sm.dropdown-toggle{data: { toggle: :dropdown }, aria: { expanded: :false }} + %span.caret + %ul.dropdown-menu.dropdown-menu-right{role: :menu} + %li.text-danger + %a{href: '#', data: { action: 'mod-comment-destroy', id: comment.id }} + %i.fa.fa-trash-o + Delete %p.comments--content= comment.content .form-group.has-feedback{name: 'mod-comment-new-group', data: { id: report.id }} %input.form-control.comments--box{type: :text, placeholder: 'Comment...', name: 'mod-comment-new', data: { id: report.id }}