Skip to content

Commit

Permalink
Improve resource name pluralization in confirm dialog
Browse files Browse the repository at this point in the history
This change adds proper pluralization in the confirm dialog for the
resource being modified. Previously the dialog always used the plural
version of the resource.
  • Loading branch information
forkata committed Apr 2, 2024
1 parent eac89aa commit 2e70faf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
14 changes: 10 additions & 4 deletions admin/app/components/solidus_admin/ui/table/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,16 @@ export default class extends Controller {
}

confirmAction(event) {
const message = event.params.message.replace(
"${count}",
this.selectedRows().length
)
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()
Expand Down
9 changes: 7 additions & 2 deletions admin/app/components/solidus_admin/ui/table/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -116,9 +120,10 @@ def render_batch_action_button(batch_action)
params["data-action"] = "click->#{stimulus_id}#confirmAction"
params["data-#{stimulus_id}-message-param"] = t(
".action_confirmation",
action: batch_action.label.downcase,
resource: @data.plural_name.downcase
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)
Expand Down
2 changes: 1 addition & 1 deletion admin/app/components/solidus_admin/ui/table/component.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +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}?'
action_confirmation: 'Are you sure you want to %{action} ${count} ${resource}?'
refine_search: 'Refine Search'
batch_actions: Batch actions
cancel: Cancel
6 changes: 3 additions & 3 deletions admin/spec/features/products_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
visit "/admin/products"
select_row("Just a product")

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

Expand All @@ -48,7 +48,7 @@
visit "/admin/products"
find('main tbody tr:nth-child(2)').find('input').check

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

Expand All @@ -66,7 +66,7 @@

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

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

Expand Down

0 comments on commit 2e70faf

Please sign in to comment.