Skip to content

Commit

Permalink
fix: action redirect loop (#3545)
Browse files Browse the repository at this point in the history
* fix: action redirect loop

* test

* comment
  • Loading branch information
Paul-Bob authored Dec 19, 2024
1 parent e9792ca commit d8e7728
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
22 changes: 16 additions & 6 deletions app/controllers/avo/actions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def respond
# Flash the messages collected from the action
flash_messages

# Always execute turbo_stream.avo_close_modal on all responses, including redirects
# Exclude response types intended to keep the modal open
# This ensures the modal frame refreshes, preventing it from retaining the SRC of the previous action
# and avoids re-triggering that SRC during back navigation
respond_to do |format|
format.turbo_stream do
turbo_response = case @response[:type]
Expand All @@ -117,11 +121,14 @@ def respond

turbo_stream.turbo_frame_set_src(Avo::MODAL_FRAME_ID, src)
when :redirect
turbo_stream.redirect_to(
Avo::ExecutionContext.new(target: @response[:path]).handle,
turbo_frame: @response[:redirect_args][:turbo_frame],
**@response[:redirect_args].except(:turbo_frame)
)
[
turbo_stream.avo_close_modal,
turbo_stream.redirect_to(
Avo::ExecutionContext.new(target: @response[:path]).handle,
turbo_frame: @response[:redirect_args][:turbo_frame],
**@response[:redirect_args].except(:turbo_frame)
)
]
when :close_modal
# Close the modal and flash the messages
[
Expand All @@ -132,7 +139,10 @@ def respond
# Reload the page
back_path = request.referer || params[:referrer].presence || resources_path(resource: @resource)

turbo_stream.redirect_to(back_path)
[
turbo_stream.avo_close_modal,
turbo_stream.redirect_to(back_path)
]
end

responses = if @action.appended_turbo_streams.present?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Avo::Actions::Test::NoConfirmationPostsRedirect < Avo::BaseAction
self.name = "Redirect to Posts"
self.no_confirmation = true
self.standalone = true

def handle(**args)
redirect_to avo.resources_posts_path
end
end
1 change: 1 addition & 0 deletions spec/dummy/app/avo/resources/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def actions
action Avo::Actions::Sub::DummyAction
action Avo::Actions::DownloadFile, icon: "heroicons/outline/arrow-left"
divider
action Avo::Actions::Test::NoConfirmationPostsRedirect
action Avo::Actions::Test::NoConfirmationRedirect
action Avo::Actions::Test::CloseModal
action Avo::Actions::Test::DoNothing
Expand Down
16 changes: 16 additions & 0 deletions spec/system/avo/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,22 @@

expect(page).to have_text "hey en"
end

it "redirects to posts and don't redirect when navigating back" do
visit avo.resources_users_path

click_on "Actions"
click_on "Redirect to Posts"

wait_for_loaded

expect(current_path).to eq avo.resources_posts_path

page.go_back
wait_for_loaded

expect(current_path).to eq avo.resources_users_path
end
end


Expand Down

0 comments on commit d8e7728

Please sign in to comment.