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.
51 lines
1.1 KiB
Ruby
51 lines
1.1 KiB
Ruby
class Admin::BudgetInvestments::ToggleSelectionComponent < ApplicationComponent
|
|
attr_reader :investment
|
|
use_helpers :can?
|
|
delegate :selected?, to: :investment
|
|
|
|
def initialize(investment)
|
|
@investment = investment
|
|
end
|
|
|
|
private
|
|
|
|
def selected_text
|
|
t("admin.budget_investments.index.selected")
|
|
end
|
|
|
|
def action
|
|
if selected?
|
|
:deselect
|
|
else
|
|
:select
|
|
end
|
|
end
|
|
|
|
def path
|
|
url_for({
|
|
controller: "admin/budget_investments",
|
|
action: action,
|
|
budget_id: investment.budget,
|
|
id: investment,
|
|
filter: params[:filter],
|
|
sort_by: params[:sort_by],
|
|
min_total_supports: params[:min_total_supports],
|
|
max_total_supports: params[:max_total_supports],
|
|
advanced_filters: params[:advanced_filters],
|
|
page: params[:page]
|
|
})
|
|
end
|
|
|
|
def options
|
|
{
|
|
"aria-label": label,
|
|
form_class: "toggle-selection",
|
|
path: path
|
|
}
|
|
end
|
|
|
|
def label
|
|
t("admin.actions.label", action: t("admin.actions.select"), name: investment.title)
|
|
end
|
|
end
|