From 2d96e7c9c69cc8ffef939df8fc8c11ada627bacf Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Wed, 12 Nov 2025 18:02:50 +0100 Subject: [PATCH] Use streams to update/remove reactions Prevents race condition causing flickering https://app.fizzy.do/5986089/cards/2906 This will prevent these from udpating live, but we will tackle that as part of live card updates with page refreshes, which I will tackle soon. --- .../cards/comments/reactions_controller.rb | 19 +++---------------- .../reactions/create.turbo_stream.erb | 1 + .../reactions/destroy.turbo_stream.erb | 1 + .../cards/comments/reactions/new.html.erb | 2 +- .../comments/reactions_controller_test.rb | 17 +++++++---------- 5 files changed, 13 insertions(+), 27 deletions(-) create mode 100644 app/views/cards/comments/reactions/create.turbo_stream.erb create mode 100644 app/views/cards/comments/reactions/destroy.turbo_stream.erb diff --git a/app/controllers/cards/comments/reactions_controller.rb b/app/controllers/cards/comments/reactions_controller.rb index 0a728fbb2..54f63dda7 100644 --- a/app/controllers/cards/comments/reactions_controller.rb +++ b/app/controllers/cards/comments/reactions_controller.rb @@ -10,29 +10,16 @@ class Cards::Comments::ReactionsController < ApplicationController end def create - reaction = @comment.reactions.create!(params.expect(reaction: :content)) - - broadcast_create(reaction) - redirect_to card_comment_reactions_path(@card, @comment) + @reaction = @comment.reactions.create!(params.expect(reaction: :content)) end def destroy - reaction = @comment.reactions.find(params[:id]) - reaction.destroy - - broadcast_remove(reaction) + @reaction = @comment.reactions.find(params[:id]) + @reaction.destroy end private def set_comment @comment = @card.comments.find(params[:comment_id]) end - - def broadcast_create(reaction) - reaction.broadcast_append_to @card, target: [ @comment, :reactions ], partial: "cards/comments/reactions/reaction" - end - - def broadcast_remove(reaction) - reaction.broadcast_remove_to @card - end end diff --git a/app/views/cards/comments/reactions/create.turbo_stream.erb b/app/views/cards/comments/reactions/create.turbo_stream.erb new file mode 100644 index 000000000..7ae1f1daa --- /dev/null +++ b/app/views/cards/comments/reactions/create.turbo_stream.erb @@ -0,0 +1 @@ +<%= turbo_stream.append([ @comment, :reactions ], partial: "cards/comments/reactions/reaction", locals: { reaction: @reaction }) %> diff --git a/app/views/cards/comments/reactions/destroy.turbo_stream.erb b/app/views/cards/comments/reactions/destroy.turbo_stream.erb new file mode 100644 index 000000000..936436e11 --- /dev/null +++ b/app/views/cards/comments/reactions/destroy.turbo_stream.erb @@ -0,0 +1 @@ +<%= turbo_stream.remove @reaction %> diff --git a/app/views/cards/comments/reactions/new.html.erb b/app/views/cards/comments/reactions/new.html.erb index 38c6daf98..e847acfbe 100644 --- a/app/views/cards/comments/reactions/new.html.erb +++ b/app/views/cards/comments/reactions/new.html.erb @@ -2,7 +2,7 @@ <%= form_with model: [ @comment.card, @comment, Reaction.new ], class: "reaction reaction__form expanded", html: { aria: { label: "New reaction" } }, - data: { controller: "form reaction-emoji", turbo_frame: dom_id(@comment, :reacting), action: "keydown.esc->form#cancel submit->form#preventEmptySubmit" } do |form| %> + data: { controller: "form reaction-emoji", turbo_frame: dom_id(@comment, :reacting), action: "keydown.esc->form#cancel submit->form#preventEmptySubmit turbo:submit-end->form#reset" } do |form| %>