diff --git a/app/assets/javascripts/application.js.erb.coffee b/app/assets/javascripts/application.js.erb.coffee index 909de2e9..a79c6106 100644 --- a/app/assets/javascripts/application.js.erb.coffee +++ b/app/assets/javascripts/application.js.erb.coffee @@ -33,7 +33,41 @@ $(document).on "click", "button[name=qb-ask]", -> $("div#question-box").hide() $("div#question-box-promote").show() $.snackbar # allahu snackbar - content: "Successfully asked question" + content: data.message + style: "snackbar" + timeout: 5000 + else + console.log data, status, jqxhr + $.snackbar # allahu snackbar + content: "An error occurred, a developer should check the console for details" + style: "snackbar" + timeout: 5000 + error: (jqxhr, status, error) -> + console.log jqxhr, status, error + $.snackbar # allahu snackbar + content: "An error occurred, a developer should check the console for details" + style: "snackbar" + timeout: 5000 + complete: (jqxhr, status) -> + btn.button "reset" + $("textarea[name=qb-question]").removeAttr "readonly" + +$(document).on "click", "button[name=ib-answer]", -> + btn = $(this) + btn.button "loading" + iid = $("input[name=ib-id]").val() + $.ajax + url: '/ajax/inbox' # TODO: find a way to use rake routes instead of hardcoding them here + type: 'POST' + data: + id: iid + answer: $("textarea[name=ib-answer]").val() + success: (data, status, jqxhr) -> + if data.success + $("div#inbox-box[data-id=#{iid}]").val '' + $("div#inbox-box").slideUp() + $.snackbar # allahu snackbar + content: data.message style: "snackbar" timeout: 5000 else diff --git a/app/controllers/ajax/inbox_controller.rb b/app/controllers/ajax/inbox_controller.rb new file mode 100644 index 00000000..1fb0dd48 --- /dev/null +++ b/app/controllers/ajax/inbox_controller.rb @@ -0,0 +1,33 @@ +class Ajax::InboxController < ApplicationController + def destroy + params.require :id + params.require :answer + + question = Question.create!(content: params[:question], + author_is_anonymous: params[:anonymousQuestion], + user: current_user) + + unless current_user.nil? + current_user.increment! :answered_count + end + + inbox = Inbox.find(params[:id]).first + + unless current_user.id == Inbox.user_id + @status = :fail + @message = "question not in your inbox" + @success = false + return + end + + answer = Answer.create(content: params[:answer], + user: current_user, + question: inbox.question) + + Inbox.destroy inbox.id + + @status = :okay + @message = "Successfully answered question." + @success = true + end +end diff --git a/app/views/ajax/inbox/destroy.json.jbuilder b/app/views/ajax/inbox/destroy.json.jbuilder new file mode 100644 index 00000000..2c193af4 --- /dev/null +++ b/app/views/ajax/inbox/destroy.json.jbuilder @@ -0,0 +1 @@ +json.partial! 'ajax/shared/status' \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index fa8b9a98..04250ffc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -25,6 +25,7 @@ Rails.application.routes.draw do namespace :ajax do match '/ask', to: 'question#create', via: :post, as: :ask + match '/answer', to: 'inbox#destroy', via: :post, as: :answer end match '/inbox', to: 'inbox#show', via: 'get' diff --git a/test/controllers/ajax/inbox_controller_test.rb b/test/controllers/ajax/inbox_controller_test.rb new file mode 100644 index 00000000..7af54c8f --- /dev/null +++ b/test/controllers/ajax/inbox_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class Ajax::InboxControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/helpers/ajax/inbox_helper_test.rb b/test/helpers/ajax/inbox_helper_test.rb new file mode 100644 index 00000000..1eea02c1 --- /dev/null +++ b/test/helpers/ajax/inbox_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class Ajax::InboxHelperTest < ActionView::TestCase +end