refactor(departements): champ departements to use simple select
This commit is contained in:
parent
f7c1dff22a
commit
e0b0a42bc3
5 changed files with 115 additions and 19 deletions
|
@ -1,3 +1,13 @@
|
|||
class EditableChamp::DepartementsComponent < EditableChamp::EditableChampBaseComponent
|
||||
include ApplicationHelper
|
||||
|
||||
private
|
||||
|
||||
def options
|
||||
APIGeoService.departements.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] }
|
||||
end
|
||||
|
||||
def select_options
|
||||
{ selected: @champ.selected }.merge(@champ.mandatory? ? { prompt: '' } : { include_blank: '' })
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,7 +1 @@
|
|||
= @form.hidden_field :value
|
||||
= @form.hidden_field :external_id
|
||||
= react_component("ComboDepartementsSearch",
|
||||
required: @champ.required?,
|
||||
id: @champ.input_id,
|
||||
className: "width-33-desktop width-100-mobile",
|
||||
describedby: @champ.describedby_id)
|
||||
= @form.select :value, options, select_options, required: @champ.mandatory?, id: @champ.input_id, aria: { describedby: @champ.describedby_id }, class: "width-33-desktop width-100-mobile"
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
import React from 'react';
|
||||
import { QueryClientProvider } from 'react-query';
|
||||
import { matchSorter } from 'match-sorter';
|
||||
|
||||
import ComboSearch, { ComboSearchProps } from './ComboSearch';
|
||||
import { queryClient } from './shared/queryClient';
|
||||
|
||||
type DepartementResult = { code: string; nom: string };
|
||||
|
||||
|
@ -42,13 +40,3 @@ export function ComboDepartementsSearch({
|
|||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export default function ComboDepartementsSearchDefault(
|
||||
params: ComboDepartementsSearchProps
|
||||
) {
|
||||
return (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<ComboDepartementsSearch {...params} />
|
||||
</QueryClientProvider>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -21,4 +21,44 @@
|
|||
# type_de_champ_id :integer
|
||||
#
|
||||
class Champs::DepartementChamp < Champs::TextChamp
|
||||
def to_s
|
||||
formatted_value
|
||||
end
|
||||
|
||||
def for_tag
|
||||
formatted_value
|
||||
end
|
||||
|
||||
def selected
|
||||
code
|
||||
end
|
||||
|
||||
def code
|
||||
external_id || APIGeoService.departement_code(name)
|
||||
end
|
||||
|
||||
def name
|
||||
maybe_code_and_name = value&.match(/(\d+) - (.+)/)
|
||||
if maybe_code_and_name
|
||||
maybe_code_and_name[2]
|
||||
else
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
def value=(code)
|
||||
if code&.size == 2
|
||||
self.external_id = code
|
||||
super(APIGeoService.departement_name(code))
|
||||
elsif code.blank?
|
||||
self.external_id = nil
|
||||
super(nil)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def formatted_value
|
||||
blank? ? "" : "#{code} – #{name}"
|
||||
end
|
||||
end
|
||||
|
|
64
spec/models/champs/departement_champ_spec.rb
Normal file
64
spec/models/champs/departement_champ_spec.rb
Normal file
|
@ -0,0 +1,64 @@
|
|||
describe Champs::DepartementChamp, type: :model do
|
||||
let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) }
|
||||
|
||||
before do
|
||||
allow(Rails).to receive(:cache).and_return(memory_store)
|
||||
Rails.cache.clear
|
||||
end
|
||||
|
||||
let(:champ) { described_class.new }
|
||||
|
||||
describe 'value', vcr: { cassette_name: 'api_geo_departements' } do
|
||||
it 'with code' do
|
||||
champ.value = '01'
|
||||
expect(champ.external_id).to eq('01')
|
||||
expect(champ.code).to eq('01')
|
||||
expect(champ.name).to eq('Ain')
|
||||
expect(champ.value).to eq('Ain')
|
||||
expect(champ.selected).to eq('01')
|
||||
expect(champ.to_s).to eq('01 – Ain')
|
||||
end
|
||||
|
||||
it 'with nil' do
|
||||
champ.write_attribute(:value, 'Ain')
|
||||
champ.write_attribute(:external_id, '01')
|
||||
champ.value = nil
|
||||
expect(champ.external_id).to be_nil
|
||||
expect(champ.code).to be_nil
|
||||
expect(champ.name).to be_nil
|
||||
expect(champ.value).to be_nil
|
||||
expect(champ.selected).to be_nil
|
||||
expect(champ.to_s).to eq('')
|
||||
end
|
||||
|
||||
it 'with blank' do
|
||||
champ.write_attribute(:value, 'Ain')
|
||||
champ.write_attribute(:external_id, '01')
|
||||
champ.value = ''
|
||||
expect(champ.external_id).to be_nil
|
||||
expect(champ.value).to be_nil
|
||||
expect(champ.selected).to be_nil
|
||||
expect(champ.to_s).to eq('')
|
||||
end
|
||||
|
||||
it 'with initial nil' do
|
||||
champ.write_attribute(:value, nil)
|
||||
expect(champ.external_id).to be_nil
|
||||
expect(champ.code).to be_nil
|
||||
expect(champ.name).to be_nil
|
||||
expect(champ.value).to be_nil
|
||||
expect(champ.selected).to be_nil
|
||||
expect(champ.to_s).to eq('')
|
||||
end
|
||||
|
||||
it 'with initial name' do
|
||||
champ.write_attribute(:value, '01 - Ain')
|
||||
expect(champ.external_id).to be_nil
|
||||
expect(champ.code).to eq('01')
|
||||
expect(champ.name).to eq('Ain')
|
||||
expect(champ.value).to eq('01 - Ain')
|
||||
expect(champ.selected).to eq('01')
|
||||
expect(champ.to_s).to eq('01 – Ain')
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue