refactor(champ): update champ commune
This commit is contained in:
parent
4e8b29b21c
commit
cb01f15570
7 changed files with 71 additions and 25 deletions
|
@ -4,4 +4,15 @@ class EditableChamp::CommunesComponent < EditableChamp::EditableChampBaseCompone
|
||||||
def dsfr_input_classname
|
def dsfr_input_classname
|
||||||
'fr-select'
|
'fr-select'
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -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
|
%react-fragment
|
||||||
= @form.hidden_field :code_postal, data: { value_slot: 'data:string' }
|
= render ReactComponent.new "ComboBox/RemoteComboBox", **react_props
|
||||||
|
|
|
@ -61,6 +61,12 @@ class DataSources::CommuneController < ApplicationController
|
||||||
else
|
else
|
||||||
[item]
|
[item]
|
||||||
end.map do |item|
|
end.map do |item|
|
||||||
|
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]})",
|
label: "#{item[:name]} (#{item[:postal_code]})",
|
||||||
value: item[:code],
|
value: item[:code],
|
||||||
|
@ -69,6 +75,7 @@ class DataSources::CommuneController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def code_metropole?(result)
|
def code_metropole?(result)
|
||||||
result[:code].in?(['75056', '13055', '69123'])
|
result[:code].in?(['75056', '13055', '69123'])
|
||||||
|
|
|
@ -401,6 +401,7 @@ module Instructeurs
|
||||||
:value,
|
:value,
|
||||||
:value_other,
|
:value_other,
|
||||||
:external_id,
|
:external_id,
|
||||||
|
:code,
|
||||||
:primary_value,
|
:primary_value,
|
||||||
:secondary_value,
|
:secondary_value,
|
||||||
:numero_allocataire,
|
:numero_allocataire,
|
||||||
|
|
|
@ -494,6 +494,7 @@ module Users
|
||||||
:value,
|
:value,
|
||||||
:value_other,
|
:value_other,
|
||||||
:external_id,
|
:external_id,
|
||||||
|
:code,
|
||||||
:primary_value,
|
:primary_value,
|
||||||
:secondary_value,
|
:secondary_value,
|
||||||
:numero_allocataire,
|
:numero_allocataire,
|
||||||
|
|
|
@ -28,10 +28,6 @@ class Champs::CommuneChamp < Champs::TextChamp
|
||||||
code_postal.present?
|
code_postal.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def code_postal=(value)
|
|
||||||
super(value&.gsub(/[[:space:]]/, ''))
|
|
||||||
end
|
|
||||||
|
|
||||||
alias postal_code code_postal
|
alias postal_code code_postal
|
||||||
|
|
||||||
def name
|
def name
|
||||||
|
@ -43,7 +39,36 @@ class Champs::CommuneChamp < Champs::TextChamp
|
||||||
end
|
end
|
||||||
|
|
||||||
def selected
|
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
|
end
|
||||||
|
|
||||||
def communes
|
def communes
|
||||||
|
@ -54,12 +79,6 @@ class Champs::CommuneChamp < Champs::TextChamp
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def safe_to_s
|
|
||||||
value.present? ? value.to_s : ''
|
|
||||||
end
|
|
||||||
|
|
||||||
def on_codes_change
|
def on_codes_change
|
||||||
return if !code?
|
return if !code?
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ describe Champs::CommuneChamp do
|
||||||
let(:champ) { create(:champ_communes, code_postal:, external_id: code_insee) }
|
let(:champ) { create(:champ_communes, code_postal:, external_id: code_insee) }
|
||||||
|
|
||||||
describe 'value' do
|
describe 'value' do
|
||||||
it 'with code_postal' do
|
it 'find commune' do
|
||||||
expect(champ.to_s).to eq('Châteldon (63290)')
|
expect(champ.to_s).to eq('Châteldon (63290)')
|
||||||
expect(champ.name).to eq('Châteldon')
|
expect(champ.name).to eq('Châteldon')
|
||||||
expect(champ.external_id).to eq(code_insee)
|
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(:value)).to eq 'Châteldon (63290)'
|
||||||
expect(champ.for_export(:code)).to eq '63102'
|
expect(champ.for_export(:code)).to eq '63102'
|
||||||
expect(champ.for_export(:departement)).to eq '63 – Puy-de-Dôme'
|
expect(champ.for_export(:departement)).to eq '63 – Puy-de-Dôme'
|
||||||
expect(champ.communes.size).to eq(8)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'code_postal with spaces' do
|
context 'with code' do
|
||||||
let(:code_postal) { ' 63 2 90 ' }
|
let(:champ) { create(:champ_communes, code: '63102-63290') }
|
||||||
|
|
||||||
it 'with code_postal' do
|
it 'find commune' do
|
||||||
expect(champ.communes.size).to eq(8)
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue