diff --git a/app/controllers/answer_controller.rb b/app/controllers/answer_controller.rb index b63be49e..33057ceb 100644 --- a/app/controllers/answer_controller.rb +++ b/app/controllers/answer_controller.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class AnswerController < ApplicationController + before_action :authenticate_user!, only: :pin + def show @answer = Answer.includes(comments: %i[user smiles], question: [:user], smiles: [:user]).find(params[:id]) @display_all = true @@ -16,4 +18,14 @@ class AnswerController < ApplicationController notif.update_all(new: false) unless notif.empty? end end + + def pin + answer = Answer.includes(:user).find(params[:id]) + UseCase::Answer::Pin.call(user: current_user, answer:) + + respond_to do |format| + format.html { redirect_to(user_path(username: current_user.screen_name)) } + format.turbo_stream { render "pin", locals: { answer: } } + end + end end diff --git a/app/views/actions/_answer.html.haml b/app/views/actions/_answer.html.haml index 9f778bbe..de81c031 100644 --- a/app/views/actions/_answer.html.haml +++ b/app/views/actions/_answer.html.haml @@ -17,14 +17,7 @@ %i.fa.fa-fw.fa-exclamation-triangle = t("voc.report") - else - - if answer.pinned? - %a.dropdown-item{ href: "#", data: { a_id: answer.id, action: "ab-unpin" } } - %i.fa.fa-fw.fa-thumbtack - = t(".unpin") - - else - %a.dropdown-item{ href: "#", data: { a_id: answer.id, action: "ab-pin" } } - %i.fa.fa-fw.fa-thumbtack - = t(".pin") + = render "actions/pin", answer: - if current_user.admin? %a.dropdown-item{ href: rails_admin_path_for_resource(answer), target: "_blank" } %i.fa.fa-fw.fa-gears diff --git a/app/views/actions/_pin.html.haml b/app/views/actions/_pin.html.haml new file mode 100644 index 00000000..aae54317 --- /dev/null +++ b/app/views/actions/_pin.html.haml @@ -0,0 +1,14 @@ +- if answer.pinned? + = button_to pin_answer_path(id: answer.id), + class: "dropdown-item", + method: :delete, + form: { id: "ab-pin-#{answer.id}", data: { turbo_stream: true } } do + %i.fa.fa-fw.fa-thumbtack + = t(".unpin") +- else + = button_to pin_answer_path(id: answer.id), + class: "dropdown-item", + method: :post, + form: { id: "ab-pin-#{answer.id}", data: { turbo_stream: true } } do + %i.fa.fa-fw.fa-thumbtack + = t(".pin") diff --git a/app/views/answer/pin.turbo_stream.haml b/app/views/answer/pin.turbo_stream.haml new file mode 100644 index 00000000..bc2c107a --- /dev/null +++ b/app/views/answer/pin.turbo_stream.haml @@ -0,0 +1,2 @@ += turbo_stream.update("ab-pin-#{answer.id}") do + = render "actions/pin", answer: diff --git a/config/locales/views.en.yml b/config/locales/views.en.yml index 4b06bb01..41652ab5 100644 --- a/config/locales/views.en.yml +++ b/config/locales/views.en.yml @@ -73,10 +73,11 @@ en: actions: answer: return: "Return to Inbox" - pin: "Pin to Profile" - unpin: "Unpin from Profile" comment: view_smiles: "View comment smiles" + pin: + pin: "Pin to Profile" + unpin: "Unpin from Profile" share: twitter: "Share on Twitter" tumblr: "Share on Tumblr" diff --git a/config/routes.rb b/config/routes.rb index 74ebc5b0..c8a12365 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -146,6 +146,7 @@ Rails.application.routes.draw do get "/user/:username", to: "user#show" get "/@:username", to: "user#show", as: :user get "/@:username/a/:id", to: "answer#show", as: :answer + post "/@:username/a/:id/pin", to: "answer#pin", as: :pin_answer get "/@:username/q/:id", to: "question#show", as: :question get "/@:username/followers", to: "user#followers", as: :show_user_followers get "/@:username/followings", to: "user#followings", as: :show_user_followings