Files
nairobi/app/components/admin/toggle_switch_component.rb
Javi Martín fc5103881d Use a switch to toggle visibility to valuators
Using a checkbox wasn't very intuitive because checkboxes are
checked/unchecked when clicked on even if there's an error in the
request. Usually, when checkboxes appear on a form, they don't send any
information to the server unless we click a button to send the form.

So we're using a switch instead of a checkbox, like we did to
enable/disable phases in commit 46d8bc4f0.

Note that, since we've got two switches that match the default
`dom_id(record) .toggle-switch` selector, we need to find a way to
differentiate them. We're adding the `form_class` option for that.

Also note that we're now using a separate action and removing the
JavaScript in the `update` action which assumed that AJAX requests to
this action were always related to updating the `visible_to_valuators`
attribute.
2024-10-28 13:41:55 +01:00

36 lines
718 B
Ruby

class Admin::ToggleSwitchComponent < ApplicationComponent
attr_reader :action, :record, :pressed, :options
alias_method :pressed?, :pressed
def initialize(action, record, pressed:, **options)
@action = action
@record = record
@pressed = pressed
@options = options
end
private
def text
if pressed?
t("shared.yes")
else
t("shared.no")
end
end
def default_options
{
text: text,
method: :patch,
remote: true,
"aria-pressed": pressed?,
form_class: "toggle-switch #{options[:form_class]}".strip
}
end
def html_options
default_options.merge(options.except(:form_class))
end
end