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

[Admin] Add the ability to configure batch action confirmation #5702

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
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,21 @@ def batch_actions
action: solidus_admin.products_path,
method: :delete,
icon: 'delete-bin-7-line',
require_confirmation: true,
},
{
label: t('.batch_actions.discontinue'),
action: solidus_admin.discontinue_products_path,
method: :put,
icon: 'pause-circle-line',
require_confirmation: true,
},
{
label: t('.batch_actions.activate'),
action: solidus_admin.activate_products_path,
method: :put,
icon: 'play-circle-line',
require_confirmation: true,
},
]
end
Expand Down
23 changes: 22 additions & 1 deletion admin/app/components/solidus_admin/ui/table/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,29 @@ export default class extends Controller {
}
}

selectedRows() {
return this.checkboxTargets.filter((checkbox) => checkbox.checked)
}

confirmAction(event) {
const message = event.params.message
.replace(
"${count}",
this.selectedRows().length
).replace(
"${resource}",
this.selectedRows().length > 1 ?
event.params.resourcePlural :
event.params.resourceSingular
)

if (!confirm(message)) {
event.preventDefault()
}
}

render() {
const selectedRows = this.checkboxTargets.filter((checkbox) => checkbox.checked)
const selectedRows = this.selectedRows()

if (this.hasSearchFieldTarget) {
this.searchToolbarTarget.toggleAttribute("hidden", this.modeValue !== "search")
Expand Down
22 changes: 19 additions & 3 deletions admin/app/components/solidus_admin/ui/table/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class SolidusAdmin::UI::Table::Component < SolidusAdmin::BaseComponent
Sortable = Struct.new(:url, :param, :animation, :handle, keyword_init: true)
Scope = Struct.new(:label, :name, :default, keyword_init: true)
Filter = Struct.new(:label, :combinator, :attribute, :predicate, :options, keyword_init: true)
BatchAction = Struct.new(:label, :icon, :action, :method, keyword_init: true) # rubocop:disable Lint/StructNewOverride
BatchAction = Struct.new(:label, :icon, :action, :require_confirmation, :method, keyword_init: true) # rubocop:disable Lint/StructNewOverride
private_constant :BatchAction, :Column, :Filter, :Scope, :Sortable

class Data < Struct.new(:rows, :class, :url, :prev, :next, :columns, :fade, :batch_actions, keyword_init: true) # rubocop:disable Lint/StructNewOverride,Style/StructInheritance
Expand All @@ -18,6 +18,10 @@ def initialize(**args)
self.batch_actions = batch_actions.to_a.map { |action| BatchAction.new(**action) }
end

def singular_name
self[:class].model_name.human if self[:class]
end

def plural_name
self[:class].model_name.human.pluralize if self[:class]
end
Expand Down Expand Up @@ -97,7 +101,7 @@ def search_form_id
end

def render_batch_action_button(batch_action)
render component("ui/button").new(
params = {
name: request_forgery_protection_token,
value: form_authenticity_token(form_options: {
action: batch_action.action,
Expand All @@ -110,7 +114,19 @@ def render_batch_action_button(batch_action)
icon: batch_action.icon,
text: batch_action.label,
scheme: :secondary,
)
}

if batch_action.require_confirmation
params["data-action"] = "click->#{stimulus_id}#confirmAction"
params["data-#{stimulus_id}-message-param"] = t(
".action_confirmation",
action: batch_action.label.downcase
)
params["data-#{stimulus_id}-resource-singular-param"] = @data.singular_name.downcase
params["data-#{stimulus_id}-resource-plural-param"] = @data.plural_name.downcase
end

render component("ui/button").new(**params)
end

def render_ransack_filter_dropdown(filter, index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ en:
select_row: 'Select row'
filter: 'Filter'
search_placeholder: 'Search all %{resources}'
action_confirmation: 'Are you sure you want to %{action} ${count} ${resource}?'
refine_search: 'Refine Search'
batch_actions: Batch actions
cancel: Cancel
15 changes: 12 additions & 3 deletions admin/spec/features/products_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@

visit "/admin/products"
select_row("Just a product")
click_button "Delete"

accept_confirm("Are you sure you want to delete 1 product?") do
click_button "Delete"
end

expect(page).to have_content("Products were successfully removed.", wait: 5)
expect(page).not_to have_content("Just a product")
Expand All @@ -44,7 +47,10 @@

visit "/admin/products"
find('main tbody tr:nth-child(2)').find('input').check
click_button "Discontinue"

accept_confirm("Are you sure you want to discontinue 1 product?") do
click_button "Discontinue"
end

expect(page).to have_content("Products were successfully discontinued.", wait: 5)
within('main tbody tr:nth-child(2)') {
Expand All @@ -59,7 +65,10 @@
}

find('main tbody tr:nth-child(2)').find('input').check
click_button "Activate"

accept_confirm("Are you sure you want to activate 1 product?") do
click_button "Activate"
end

expect(page).to have_content("Products were successfully activated.", wait: 5)
within('tbody') do
Expand Down
Loading