refactor(champ): update champ chorus

This commit is contained in:
Paul Chavard 2024-05-07 11:28:14 +02:00
parent f0f88ef3f0
commit dc6af4fb85
No known key found for this signature in database
2 changed files with 32 additions and 10 deletions

View file

@ -14,6 +14,20 @@ class Procedure::ChorusFormComponent < ApplicationComponent
}
end
def selected_key(attribute_name)
items(attribute_name).first&.dig(:value)
end
def items(attribute_name)
label = format_displayed_value(attribute_name)
data = format_hidden_value(attribute_name)
if label.present?
[{ label:, value: label, data: }]
else
[]
end
end
def format_displayed_value(attribute_name)
case attribute_name
when :centre_de_cout
@ -30,13 +44,23 @@ class Procedure::ChorusFormComponent < ApplicationComponent
def format_hidden_value(attribute_name)
case attribute_name
when :centre_de_cout
@chorus_configuration.centre_de_cout.to_json
@chorus_configuration.centre_de_cout
when :domaine_fonctionnel
@chorus_configuration.domaine_fonctionnel.to_json
@chorus_configuration.domaine_fonctionnel
when :referentiel_de_programmation
@chorus_configuration.referentiel_de_programmation.to_json
@chorus_configuration.referentiel_de_programmation
else
raise 'unknown attribute_name'
end
end
def react_props(name, chorus_configuration_attribute, datasource_endpoint)
{
name:,
selected_key: selected_key(chorus_configuration_attribute),
items: items(chorus_configuration_attribute),
loader: datasource_endpoint,
id: chorus_configuration_attribute
}
end
end

View file

@ -1,12 +1,10 @@
= form_for([procedure, @chorus_configuration],url: admin_procedure_chorus_path(procedure), method: :put) do |f|
= form_for([procedure, @chorus_configuration], url: admin_procedure_chorus_path(procedure), method: :put) do |f|
- map_attribute_to_autocomplete_endpoint.map do |chorus_configuration_attribute, datasource_endpoint|
- label_id = "#{chorus_configuration_attribute}-label"
.fr-select-group
= f.label chorus_configuration_attribute, class: 'fr-label', id: label_id
= render Dsfr::ComboboxComponent.new form: f,
url: datasource_endpoint,
selected: format_displayed_value(chorus_configuration_attribute),
input_html_options: { id: chorus_configuration_attribute, class: 'fr-select', describedby: label_id, name: :chorus_configuration_attribute } do
= f.hidden_field chorus_configuration_attribute, data: { value_slot: 'data' }, value: format_hidden_value(chorus_configuration_attribute)
= f.label chorus_configuration_attribute, class: 'fr-label', id: label_id, for: chorus_configuration_attribute
%react-fragment
= render ReactComponent.new "ComboBox/RemoteComboBox", **react_props(f.field_name(:chorus_configuration_attribute), chorus_configuration_attribute, datasource_endpoint) do
= render ReactComponent.new "ComboBox/ComboBoxValueSlot", field: :data, name: f.field_name(chorus_configuration_attribute)
= f.submit "Enregister", class: 'fr-btn'