refactor(champ): update champ commune

This commit is contained in:
Paul Chavard 2024-05-06 21:44:41 +02:00
parent 4e8b29b21c
commit cb01f15570
No known key found for this signature in database
7 changed files with 71 additions and 25 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -401,6 +401,7 @@ module Instructeurs
:value,
:value_other,
:external_id,
:code,
:primary_value,
:secondary_value,
:numero_allocataire,

View file

@ -494,6 +494,7 @@ module Users
:value,
:value_other,
:external_id,
:code,
:primary_value,
:secondary_value,
:numero_allocataire,

View file

@ -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?

View file

@ -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