Disable buttons in table actions when pressed

By default, Rails disables submit inputs (<input type="submit">) when
they're pressed so we avoid a double-submission when users click the
button twice.

However, Rails does not disable submit buttons (<button type="submit">)
when they're pressed. This means there's a chance users might press the
button several times. Even if most our table actions are idempotent, it
might cause certain issues. For instance, pressing the "Delete" button
twice means the second request might raise an
`ActiveRecord::RecordNotFound` exception.

Disabling the button also gives feedback to users, letting them know
they've correctly clicked the button.
This commit is contained in:
Javi Martín
2021-08-20 03:21:54 +02:00
parent 5311daadfe
commit aaa5f6c285
4 changed files with 43 additions and 7 deletions

View File

@@ -10,6 +10,10 @@ class Admin::ActionComponent < ApplicationComponent
private
def button?
options[:method] && options[:method] != :get
end
def text
options[:text] || t("admin.actions.#{action}")
end
@@ -22,12 +26,15 @@ class Admin::ActionComponent < ApplicationComponent
{
class: html_class,
"aria-label": label,
data: { confirm: confirmation_text }
}.merge(options.except(:"aria-label", :confirm, :path, :text))
data: {
confirm: confirmation_text,
disable_with: (text if button?)
}
}.merge(options.except(:"aria-label", :class, :confirm, :path, :text))
end
def html_class
"#{action.to_s.gsub("_", "-")}-link"
"admin-action #{options[:class] || "#{action.to_s.gsub("_", "-")}-link"}".strip
end
def label