posting moderation comments works now

This commit is contained in:
nilsding 2014-12-29 00:50:14 +01:00
parent b7b5ee960e
commit a0c0d68e43
8 changed files with 112 additions and 11 deletions

View File

@ -0,0 +1,65 @@
# Toggle button
$(document).on "click", "button[name=mod-comments]", ->
btn = $(this)
id = btn[0].dataset.id
state = btn[0].dataset.state
commentBox = $("#mod-comments-section-#{id}")
switch state
when 'hidden'
commentBox.slideDown()
btn[0].dataset.state = 'shown'
when 'shown'
commentBox.slideUp()
btn[0].dataset.state = 'hidden'
$(document).on "keyup", "input[name=mod-comment-new]", (evt) ->
input = $(this)
id = input[0].dataset.id
ctr = $("span#mod-comment-charcount-#{id}")
cbox = $("div[name=mod-comment-new-group][data-id=#{id}]")
if evt.which == 13 # return key
evt.preventDefault()
return cbox.addClass "has-error" if input.val().length > 160 || input.val().trim().length == 0
input.attr 'disabled', 'disabled'
$.ajax
url: '/ajax/mod/create_comment'
type: 'POST'
data:
id: id
comment: input.val()
dataType: 'json' # jQuery can't guess the datatype correctly here...
success: (data, status, jqxhr) ->
console.log data
if data.success
$("#mod-comments-#{id}").html data.render
input.val ''
ctr.html 160
$("span#mod-comment-count-#{id}").html data.count
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) ->
input.removeAttr 'disabled'
# character count
$(document).on "input", "input[name=mod-comment-new]", (evt) ->
input = $(this)
id = input[0].dataset.id
ctr = $("span#mod-comment-charcount-#{id}")
cbox = $("div[name=mod-comment-new-group][data-id=#{id}]")
cbox.removeClass "has-error" if cbox.hasClass "has-error"
ctr.html 160 - input.val().length
if Number(ctr.html()) < 0
ctr.removeClass 'text-muted'
ctr.addClass 'text-danger'
else
ctr.removeClass 'text-danger'
ctr.addClass 'text-muted'

View File

@ -60,4 +60,31 @@ class Ajax::ModerationController < ApplicationController
@message = "WHERE DID IT GO??? OH NO!!!"
@success = true
end
def create_comment
params.require :id
params.require :comment
report = Report.find(params[:id])
@success = false
begin
current_user.report_comment(report, params[:comment])
rescue ActiveRecord::RecordInvalid
@status = :rec_inv
@message = "Your comment is too long."
return
end
@status = :okay
@message = "Comment posted successfully."
@success = true
@render = render_to_string(partial: 'moderation/discussion', locals: { report: report })
@count = report.moderation_comments.all.count
end
def destroy_comment
end
end

View File

@ -146,4 +146,8 @@ class User < ActiveRecord::Base
report.moderation_votes.where(upvote: upvote).each { |s| return true if s.user_id == self.id }
false
end
def report_comment(report, content)
ModerationComment.create!(user: self, report: report, content: content)
end
end

View File

@ -0,0 +1,3 @@
json.partial! 'ajax/shared/status'
json.render @render if @render
json.count @count if @count

View File

@ -0,0 +1,2 @@
json.partial! 'ajax/shared/status'
json.count @count if @count

View File

@ -15,10 +15,10 @@
%span.caret
%ul.dropdown-menu.dropdown-menu-right{role: :menu}
%li.text-danger
%a{href: '#', name: 'ab-destroy', data: { a_id: comment.id }}
%a{href: '#', name: 'mod-comment-destroy', data: { id: comment.id }}
%i.fa.fa-trash-o
Delete
%p.comments--content= comment.content
.form-group.has-feedback
%input.form-control.comments--box{type: :text, placeholder: 'Comment...'}
%span.text-muted.form-control-feedback.comments--count 160
.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 }}
%span.text-muted.form-control-feedback.comments--count{id: "mod-comment-charcount-#{report.id}"} 160

View File

@ -25,10 +25,10 @@
%i.fa.fa-thumbs-up
%button.btn.btn-danger.btn-sm{type: :button, name: "mod-vote", disabled: current_user.report_x_voted?(report, false) ? 'disabled' : nil, data: { id: report.id, action: current_user.report_voted?(report) ? 'unvote' : 'vote', vote_type: 'downvote' }}
%i.fa.fa-thumbs-down
%button.btn.btn-primary.btn-sm{type: :button}
%button.btn.btn-primary.btn-sm{type: :button, name: 'mod-comments', data: { id: report.id, state: :hidden }}
%i.fa.fa-comments
%span 0
%span{id: "mod-comment-count-#{report.id}"}= report.moderation_comments.all.count
%button.btn.btn-default.btn-sm{type: :button, name: "mod-delete-report", data: { id: report.id }}
%i.fa.fa-trash-o
.panel-footer
= render 'moderation/discussion', report: report
.panel-footer{id: "mod-comments-section-#{report.id}", style: 'display: none'}
%div{id: "mod-comments-#{report.id}"}= render 'moderation/discussion', report: report

View File

@ -15,10 +15,10 @@ Rails.application.routes.draw do
(req.env['warden'].user.admin? or req.env['warden'].user.moderator?) } do
match '/moderation', to: 'moderation#index', via: :get, as: :moderation
namespace :ajax do
match '/mod/create_comment', to: 'moderation#comment', via: :post, as: :mod_comment
match '/mod/destroy_comment', to: 'moderation#destroy_comment', via: :post, as: :mod_destroy_comment
match '/mod/destroy_report', to: 'moderation#destroy_report', via: :post, as: :mod_destroy_report
match '/mod/create_vote', to: 'moderation#vote', via: :post, as: :mod_vote
match '/mod/create_comment', to: 'moderation#create_comment', via: :post, as: :mod_create_comment
match '/mod/destroy_comment', to: 'moderation#destroy_comment', via: :post, as: :mod_destroy_comment
match '/mod/create_vote', to: 'moderation#vote', via: :post, as: :mod_create_vote
match '/mod/destroy_vote', to: 'moderation#destroy_vote', via: :post, as: :mod_destroy_vote
end
end