diff --git a/app/components/editable_champ/communes_component.rb b/app/components/editable_champ/communes_component.rb index 80fbb70e9..3728cc30a 100644 --- a/app/components/editable_champ/communes_component.rb +++ b/app/components/editable_champ/communes_component.rb @@ -4,4 +4,15 @@ class EditableChamp::CommunesComponent < EditableChamp::EditableChampBaseCompone def dsfr_input_classname 'fr-select' end + + def react_props + react_input_opts(id: @champ.input_id, + class: 'fr-mt-1w', + name: @form.field_name(:code), + selected_key: @champ.selected, + items: @champ.selected_items, + loader: data_sources_data_source_commune_path(with_combined_code: true), + limit: 20, + minimum_input_length: 2) + end end diff --git a/app/components/editable_champ/communes_component/communes_component.html.haml b/app/components/editable_champ/communes_component/communes_component.html.haml index 709c87d93..5c2f652de 100644 --- a/app/components/editable_champ/communes_component/communes_component.html.haml +++ b/app/components/editable_champ/communes_component/communes_component.html.haml @@ -1,2 +1,2 @@ -= render Dsfr::ComboboxComponent.new form: @form, url: data_sources_data_source_commune_path, selected: [@champ.to_s, @champ.selected], limit: 20, input_html_options: { name: :external_id, id: @champ.input_id, class: 'fr-select', describedby: @champ.describedby_id } do - = @form.hidden_field :code_postal, data: { value_slot: 'data:string' } +%react-fragment + = render ReactComponent.new "ComboBox/RemoteComboBox", **react_props diff --git a/app/controllers/data_sources/commune_controller.rb b/app/controllers/data_sources/commune_controller.rb index 825d3f7c4..49884399e 100644 --- a/app/controllers/data_sources/commune_controller.rb +++ b/app/controllers/data_sources/commune_controller.rb @@ -61,11 +61,18 @@ class DataSources::CommuneController < ApplicationController else [item] end.map do |item| - { - label: "#{item[:name]} (#{item[:postal_code]})", - value: item[:code], - data: item[:postal_code] - } + if params[:with_combined_code].present? + { + label: "#{item[:name]} (#{item[:postal_code]})", + value: "#{item[:code]}-#{item[:postal_code]}" + } + else + { + label: "#{item[:name]} (#{item[:postal_code]})", + value: item[:code], + data: item[:postal_code] + } + end end end end diff --git a/app/controllers/instructeurs/dossiers_controller.rb b/app/controllers/instructeurs/dossiers_controller.rb index 189477bff..884309712 100644 --- a/app/controllers/instructeurs/dossiers_controller.rb +++ b/app/controllers/instructeurs/dossiers_controller.rb @@ -401,6 +401,7 @@ module Instructeurs :value, :value_other, :external_id, + :code, :primary_value, :secondary_value, :numero_allocataire, diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 3e322e618..f22896053 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -494,6 +494,7 @@ module Users :value, :value_other, :external_id, + :code, :primary_value, :secondary_value, :numero_allocataire, diff --git a/app/models/champs/commune_champ.rb b/app/models/champs/commune_champ.rb index be62ce968..37d8ad70e 100644 --- a/app/models/champs/commune_champ.rb +++ b/app/models/champs/commune_champ.rb @@ -28,10 +28,6 @@ class Champs::CommuneChamp < Champs::TextChamp code_postal.present? end - def code_postal=(value) - super(value&.gsub(/[[:space:]]/, '')) - end - alias postal_code code_postal def name @@ -43,7 +39,36 @@ class Champs::CommuneChamp < Champs::TextChamp end def selected - code + code? ? "#{code}-#{code_postal}" : nil + end + + def selected_items + if code? + [{ label: to_s, value: selected }] + else + [] + end + end + + def code=(code) + if code.blank? + self.code_departement = nil + self.code_postal = nil + self.external_id = nil + self.value = nil + elsif code.match?(/-/) + codes = code.split('-') + self.external_id = codes.first + self.code_postal = codes.second + else + self.external_id = code + end + end + + private + + def safe_to_s + value.present? ? value.to_s : '' end def communes @@ -54,12 +79,6 @@ class Champs::CommuneChamp < Champs::TextChamp end end - private - - def safe_to_s - value.present? ? value.to_s : '' - end - def on_codes_change return if !code? diff --git a/spec/models/champs/commune_champ_spec.rb b/spec/models/champs/commune_champ_spec.rb index 76051c0c3..87d726dd6 100644 --- a/spec/models/champs/commune_champ_spec.rb +++ b/spec/models/champs/commune_champ_spec.rb @@ -5,7 +5,7 @@ describe Champs::CommuneChamp do let(:champ) { create(:champ_communes, code_postal:, external_id: code_insee) } describe 'value' do - it 'with code_postal' do + it 'find commune' do expect(champ.to_s).to eq('Châteldon (63290)') expect(champ.name).to eq('Châteldon') expect(champ.external_id).to eq(code_insee) @@ -15,15 +15,22 @@ describe Champs::CommuneChamp do expect(champ.for_export(:value)).to eq 'Châteldon (63290)' expect(champ.for_export(:code)).to eq '63102' expect(champ.for_export(:departement)).to eq '63 – Puy-de-Dôme' - expect(champ.communes.size).to eq(8) end - end - describe 'code_postal with spaces' do - let(:code_postal) { ' 63 2 90  ' } + context 'with code' do + let(:champ) { create(:champ_communes, code: '63102-63290') } - it 'with code_postal' do - expect(champ.communes.size).to eq(8) + it 'find commune' do + expect(champ.to_s).to eq('Châteldon (63290)') + expect(champ.name).to eq('Châteldon') + expect(champ.external_id).to eq(code_insee) + expect(champ.code).to eq(code_insee) + expect(champ.code_departement).to eq(code_departement) + expect(champ.code_postal).to eq(code_postal) + expect(champ.for_export(:value)).to eq 'Châteldon (63290)' + expect(champ.for_export(:code)).to eq '63102' + expect(champ.for_export(:departement)).to eq '63 – Puy-de-Dôme' + end end end end