2023-09-13 15:21:59 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-10-29 13:14:26 -07:00
|
|
|
class ReactionsController < ApplicationController
|
2023-10-31 15:32:36 -07:00
|
|
|
include TurboStreamable
|
|
|
|
|
|
|
|
before_action :authenticate_user!, only: %w[create destroy]
|
|
|
|
|
|
|
|
turbo_stream_actions :create, :destroy
|
|
|
|
|
2023-09-13 15:21:59 -07:00
|
|
|
def index
|
|
|
|
answer = Answer.includes([smiles: { user: :profile }]).find(params[:id])
|
|
|
|
|
|
|
|
render "index", locals: { a: answer }
|
|
|
|
end
|
2023-10-31 15:32:36 -07:00
|
|
|
|
|
|
|
def create
|
|
|
|
params.require :id
|
|
|
|
|
2023-11-05 02:45:51 -08:00
|
|
|
target = target_class.find(params[:id])
|
2023-10-31 15:32:36 -07:00
|
|
|
|
|
|
|
UseCase::Reaction::Create.call(
|
|
|
|
source_user: current_user,
|
2023-11-05 02:45:51 -08:00
|
|
|
target:,
|
2023-10-31 15:32:36 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
target.reload
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.turbo_stream do
|
|
|
|
render turbo_stream: [
|
|
|
|
turbo_stream.replace("reaction-#{params[:type]}-#{params[:id]}", partial: "reactions/destroy", locals: { type: params[:type], target: }),
|
|
|
|
render_toast(t(".#{params[:type].downcase}.success"))
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
format.html { redirect_back(fallback_location: root_path) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
params.require :id
|
|
|
|
|
2023-11-05 02:45:51 -08:00
|
|
|
target = target_class.find(params[:id])
|
2023-10-31 15:32:36 -07:00
|
|
|
|
|
|
|
UseCase::Reaction::Destroy.call(
|
|
|
|
source_user: current_user,
|
2023-11-05 02:45:51 -08:00
|
|
|
target:,
|
2023-10-31 15:32:36 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
target.reload
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.turbo_stream do
|
|
|
|
render turbo_stream: [
|
|
|
|
turbo_stream.replace("reaction-#{params[:type]}-#{params[:id]}", partial: "reactions/create", locals: { type: params[:type], target: }),
|
|
|
|
render_toast(t(".#{params[:type].downcase}.success"))
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
format.html { redirect_back(fallback_location: root_path) }
|
|
|
|
end
|
|
|
|
end
|
2023-11-05 02:16:11 -08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
ALLOWED_TYPES = %w[Answer Comment].freeze
|
|
|
|
private_constant :ALLOWED_TYPES
|
|
|
|
|
|
|
|
def target_class
|
|
|
|
params.require :type
|
|
|
|
raise NameError unless ALLOWED_TYPES.include?(params[:type])
|
|
|
|
|
|
|
|
params[:type].constantize
|
|
|
|
end
|
2023-09-13 15:21:59 -07:00
|
|
|
end
|