Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: redirect on action when no_confirmation = true #2038

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/javascript/js/controllers/action_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Controller } from '@hotwired/stimulus'
import { castBoolean } from '../helpers/cast_boolean'

export default class extends Controller {
static targets = ['controllerDiv', 'resourceIds', 'form', 'selectedAllQuery']
static targets = ['controllerDiv', 'resourceIds', 'submit', 'selectedAllQuery']

connect() {
this.resourceIdsTarget.value = this.resourceIds
Expand All @@ -13,7 +13,7 @@ export default class extends Controller {
}

if (this.noConfirmation) {
this.formTarget.submit()
this.submitTarget.click()
} else {
this.controllerDivTarget.classList.remove('hidden')
}
Expand Down
12 changes: 7 additions & 5 deletions lib/avo/base_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,21 @@ class << self
def form_data_attributes
# We can't respond with a file download from Turbo se we disable it on the form
if may_download_file
{turbo: turbo || false, remote: false, action_target: :form}
{turbo: turbo || false, remote: false}
else
{turbo: turbo, turbo_frame: :_top, action_target: :form}.compact
{turbo: turbo, turbo_frame: :_top}.compact
end
end

# We can't respond with a file download from Turbo se we disable close the modal manually after a while (it's a hack, we know)
def submit_button_data_attributes
attributes = { action_target: "submit" }

if may_download_file
{action: "click->modal#delayedClose"}
else
{}
attributes[:action] = "click->modal#delayedClose"
end

attributes
end
end

Expand Down
29 changes: 29 additions & 0 deletions spec/system/avo/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,35 @@
end
end

describe "redirects when no confirmation" do
it "redirects to hey page" do
no_confirmation = Sub::DummyAction.no_confirmation

Sub::DummyAction.class_eval do
self.no_confirmation = true

define_method(:redirect_handle) do |**args|
redirect_to main_app.hey_path
end

alias_method :handle, :redirect_handle
end

visit "/admin/resources/users"

click_on "Actions"
click_on "Dummy action"

expect(page).to have_text "hey en"


Sub::DummyAction.class_eval do
undef_method :redirect_handle
end

Sub::DummyAction.no_confirmation = no_confirmation
end
end
# let!(:roles) { { admin: false, manager: false, writer: false } }
# let!(:user) { create :user, active: true, roles: roles }

Expand Down
Loading