diff --git a/app/assets/javascripts/questionbox/destroy.coffee b/app/assets/javascripts/questionbox/destroy.coffee new file mode 100644 index 00000000..7fc1f005 --- /dev/null +++ b/app/assets/javascripts/questionbox/destroy.coffee @@ -0,0 +1,30 @@ +$(document).on "click", "a[data-action=ab-question-destroy]", (ev) -> + ev.preventDefault() + btn = $(this) + qid = btn[0].dataset.qId + swal + title: "Are you sure?" + text: "The question will be removed." + type: "warning" + showCancelButton: true + confirmButtonColor: "#DD6B55" + confirmButtonText: "Yes" + cancelButtonText: "No" + closeOnConfirm: true + , -> + $.ajax + url: '/ajax/destroy_question' # TODO: find a way to use rake routes instead of hardcoding them here + type: 'POST' + data: + question: qid + success: (data, status, jqxhr) -> + if data.success + if btn[0].dataset.redirect != undefined + window.location.href = btn[0].dataset.redirect + else + $("div.answerbox[data-qId=#{qid}], div.questionbox[data-id=#{qid}]").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/assets/javascripts/questionbox/report.coffee b/app/assets/javascripts/questionbox/report.coffee new file mode 100644 index 00000000..a1ab32de --- /dev/null +++ b/app/assets/javascripts/questionbox/report.coffee @@ -0,0 +1,5 @@ +$(document).on "click", "a[data-action=ab-question-report]", (ev) -> + ev.preventDefault() + btn = $(this) + qId = btn[0].dataset.qId + reportDialog "question", qId, -> btn.button "reset" diff --git a/app/assets/stylesheets/scss/panel.scss b/app/assets/stylesheets/scss/panel.scss index e32ff837..544ca3a9 100644 --- a/app/assets/stylesheets/scss/panel.scss +++ b/app/assets/stylesheets/scss/panel.scss @@ -11,4 +11,10 @@ .panel-primary .answerbox--question-text { color: #fff; -} \ No newline at end of file +} + +#questions .panel-body .media { + &, .media-body { + overflow: visible; + } +} diff --git a/app/controllers/ajax/question_controller.rb b/app/controllers/ajax/question_controller.rb index 9c62b620..9f3fc239 100644 --- a/app/controllers/ajax/question_controller.rb +++ b/app/controllers/ajax/question_controller.rb @@ -1,6 +1,24 @@ class Ajax::QuestionController < ApplicationController include MarkdownHelper + def destroy + params.require :question + + question = Question.find params[:question] + if question.nil? + @status = :not_found + @message = "Question does not exist" + @success = false + return + end + + question.destroy! + + @status = :okay + @message = "Successfully deleted question." + @success = true + end + def create params.require :question params.require :anonymousQuestion diff --git a/app/views/ajax/question/destroy.json.jbuilder b/app/views/ajax/question/destroy.json.jbuilder new file mode 100644 index 00000000..f98b3c38 --- /dev/null +++ b/app/views/ajax/question/destroy.json.jbuilder @@ -0,0 +1 @@ +json.partial! 'ajax/shared/status' diff --git a/app/views/question/show.html.haml b/app/views/question/show.html.haml index cf8564a1..c55a3fdc 100644 --- a/app/views/question/show.html.haml +++ b/app/views/question/show.html.haml @@ -11,11 +11,11 @@ %button.btn.btn-link.btn-sm.dropdown-toggle{data: { toggle: :dropdown }, aria: { expanded: :false }} %span.caret %ul.dropdown-menu.dropdown-menu-right{role: :menu} - - if current_user.mod? + - if current_user.mod? or @question.user == current_user %li.text-danger - %a{href: '#', data: { action: 'ab-question-destroy', q_id: @question.id }} + %a{href: '#', data: { action: 'ab-question-destroy', q_id: @question.id, redirect: show_user_questions_path(@question.user.screen_name) }} %i.fa.fa-trash-o - Delete + Delete Question - unless @question.user == current_user %li %a{href: '#', data: { action: 'ab-question-report', q_id: @question.id }} diff --git a/app/views/shared/_answerbox.html.haml b/app/views/shared/_answerbox.html.haml index bb391f2f..dbc38b29 100644 --- a/app/views/shared/_answerbox.html.haml +++ b/app/views/shared/_answerbox.html.haml @@ -1,4 +1,4 @@ -.panel.panel-default.answerbox{data: { id: a.id }} +.panel.panel-default.answerbox{data: { id: a.id, q_id: a.question.id }} - if @question.nil? .panel-heading .media.question-media @@ -12,11 +12,11 @@ %button.btn.btn-link.btn-sm.dropdown-toggle{data: { toggle: :dropdown }, aria: { expanded: :false }} %span.caret %ul.dropdown-menu.dropdown-menu-right{role: :menu} - - if current_user.mod? + - if current_user.mod? or a.question.user == current_user %li.text-danger %a{href: '#', data: { action: 'ab-question-destroy', q_id: a.question.id }} %i.fa.fa-trash-o - Delete + Delete Question - unless a.question.user == current_user %li %a{href: '#', data: { action: 'ab-question-report', q_id: a.question.id }} diff --git a/app/views/shared/_question.html.haml b/app/views/shared/_question.html.haml index f2f4da75..2a592f5b 100644 --- a/app/views/shared/_question.html.haml +++ b/app/views/shared/_question.html.haml @@ -1,7 +1,23 @@ -.panel.panel-default +.panel.panel-default.questionbox{data: { id: q.id }} .panel-body .media .media-body + - if user_signed_in? + .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} + - if current_user.mod? or q.user == current_user + %li.text-danger + %a{href: '#', data: { action: 'ab-question-destroy', q_id: q.id }} + %i.fa.fa-trash-o + Delete Question + - unless q.user == current_user + %li + %a{href: '#', data: { action: 'ab-question-report', q_id: q.id }} + %i.fa.fa-exclamation-triangle + Report %h6.media-heading.text-muted.answerbox--question-user = user_screen_name q.user asked @@ -14,4 +30,4 @@ %a{href: show_user_question_path(q.user.screen_name, q.id)} #{q.answer_count} answers %p.answerbox--question-text - = q.content \ No newline at end of file + = q.content diff --git a/config/routes.rb b/config/routes.rb index 475b516a..712f511f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -66,6 +66,7 @@ Rails.application.routes.draw do namespace :ajax do match '/ask', to: 'question#create', via: :post, as: :ask + match '/destroy_question', to: 'question#destroy', via: :post, as: :destroy_question match '/generate_question', to: 'inbox#create', via: :post, as: :generate_question match '/delete_inbox', to: 'inbox#remove', via: :post, as: :delete_inbox match '/delete_all_inbox', to: 'inbox#remove_all', via: :post, as: :delete_all_inbox