This is consistent with the way we use separate actions to hide and restore records, which is similar to enabling and disabling a record. We might do something similar with the `toggle_selection` actions in the future. For now, we're only doing it with budget phases because we're going to add a similar switch control to hide and restore tenants. We're also making these actions idempotent, so sending many requests to the same action will get the same result, which wasn't the case with the `toggle` action. Although it's a low probability case, the `toggle` action could result in disabling a phase when trying to enable it if someone else has enabled it between the time the page loaded and the time the admin clicked on the "enable" button.
38 lines
652 B
Ruby
38 lines
652 B
Ruby
class Admin::BudgetPhases::ToggleEnabledComponent < ApplicationComponent
|
|
attr_reader :phase
|
|
delegate :enabled?, to: :phase
|
|
|
|
def initialize(phase)
|
|
@phase = phase
|
|
end
|
|
|
|
private
|
|
|
|
def options
|
|
{
|
|
text: text,
|
|
method: :patch,
|
|
remote: true,
|
|
"aria-label": t("admin.budgets.edit.enable_phase", phase: phase.name),
|
|
"aria-pressed": enabled?,
|
|
form_class: "toggle-switch"
|
|
}
|
|
end
|
|
|
|
def action
|
|
if enabled?
|
|
:disable
|
|
else
|
|
:enable
|
|
end
|
|
end
|
|
|
|
def text
|
|
if enabled?
|
|
t("shared.yes")
|
|
else
|
|
t("shared.no")
|
|
end
|
|
end
|
|
end
|