From 1ba34c59d0c46d3b3ad168b0e0eb2d8a63fca5bf Mon Sep 17 00:00:00 2001 From: nilsding Date: Sun, 21 Dec 2014 13:41:57 +0100 Subject: [PATCH] added "delete all questions" button thing --- app/assets/javascripts/inbox.coffee | 25 ++++++++++++++++++++++++ app/controllers/ajax/inbox_controller.rb | 16 +++++++++++++++ app/views/inbox/show.html.haml | 23 +++++++++++----------- config/routes.rb | 1 + 4 files changed, 53 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/inbox.coffee b/app/assets/javascripts/inbox.coffee index 87bd87e9..100f5d5c 100644 --- a/app/assets/javascripts/inbox.coffee +++ b/app/assets/javascripts/inbox.coffee @@ -14,12 +14,36 @@ complete: (jqxhr, status) -> btn.button "reset" + +($ document).on "click", "button#ib-delete-all", -> + if confirm 'Are you sure?' + btn = ($ this) + btn.button "loading" + $.ajax + url: '/ajax/delete_all_inbox' + type: 'POST' + dataType: 'json' + success: (data, status, jqxhr) -> + if data.success + entries = ($ "div#entries") + entries.slideUp 400, -> + entries.html("Nothing to see here.") + entries.fadeIn() + btn.attr("disabled", "disabled") + 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) -> + btn.button "reset" + + $(document).on "keydown", "textarea[name=ib-answer]", (evt) -> iid = $(this)[0].dataset.id if evt.keyCode == 13 and evt.ctrlKey # trigger warning: $("button[name=ib-answer][data-ib-id=#{iid}]").trigger 'click' + $(document).on "click", "button[name=ib-answer]", -> btn = $(this) btn.button "loading" @@ -48,6 +72,7 @@ $(document).on "click", "button[name=ib-answer]", -> btn.button "reset" $("textarea[name=ib-answer][data-id=#{iid}]").removeAttr "readonly" + $(document).on "click", "button[name=ib-destroy]", -> if confirm 'Are you sure?' btn = $(this) diff --git a/app/controllers/ajax/inbox_controller.rb b/app/controllers/ajax/inbox_controller.rb index 88393659..21ff83dc 100644 --- a/app/controllers/ajax/inbox_controller.rb +++ b/app/controllers/ajax/inbox_controller.rb @@ -85,4 +85,20 @@ class Ajax::InboxController < ApplicationController @message = "Successfully deleted question." @success = true end + + def remove_all + begin + Inbox.where(user: current_user).each { |i| i.remove } + rescue + @status = :err + @message = "An error occurred" + @success = false + return + end + + @status = :okay + @message = "Successfully deleted questions." + @success = true + render 'ajax/inbox/remove' + end end diff --git a/app/views/inbox/show.html.haml b/app/views/inbox/show.html.haml index d1e91e77..4a432471 100644 --- a/app/views/inbox/show.html.haml +++ b/app/views/inbox/show.html.haml @@ -6,21 +6,20 @@ %h3 Out of questions? .panel-body %button.btn.btn-block.btn-info{type: :button, id: 'ib-generate-question'} Get new question - / - .panel.panel-default.warning--panel - .panel-heading - %h3 Actions - .panel-body - %button.btn.btn-block.btn-danger{type: :button, id: 'ib-delete-all'} Delete all questions + .panel.panel-default.warning--panel + .panel-heading + %h3 Actions + .panel-body + %button.btn.btn-block.btn-danger{type: :button, id: 'ib-delete-all'} Delete all questions .col-md-9.col-xs-12.col-sm-9 - = render 'layouts/messages' - #entries - - @inbox.each do |i| - = render 'inbox/entry', i: i + = render 'layouts/messages' + #entries + - @inbox.each do |i| + = render 'inbox/entry', i: i - - if @inbox.empty? + - if @inbox.empty? - Nothing to see here. + Nothing to see here. = render "shared/links" - @inbox.update_all(new: false) \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 663b0705..cc6ccb8a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -46,6 +46,7 @@ Rails.application.routes.draw do match '/answer', to: 'inbox#destroy', via: :post, as: :answer 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 match '/destroy_answer', to: 'answer#destroy', via: :post, as: :destroy_answer match '/create_friend', to: 'friend#create', via: :post, as: :create_friend match '/destroy_friend', to: 'friend#destroy', via: :post, as: :destroy_friend