refactor(pays): champ pays to use simple select

This commit is contained in:
Paul Chavard 2022-12-20 21:28:42 +01:00
parent 4d4c378724
commit f7c1dff22a
5 changed files with 120 additions and 37 deletions

View file

@ -1,3 +1,18 @@
class EditableChamp::PaysComponent < EditableChamp::EditableChampBaseComponent class EditableChamp::PaysComponent < EditableChamp::EditableChampBaseComponent
include ApplicationHelper include ApplicationHelper
private
def options
options = APIGeoService.countries.map { [_1[:name], _1[:code]] }
# For legacy fields, selected value is non standard country name. Add it to the list.
if (@champ.selected&.size || 0) > 2
options.unshift([@champ.selected, @champ.selected])
end
options
end
def select_options
{ selected: @champ.selected }.merge(@champ.mandatory? ? { prompt: '' } : { include_blank: '' })
end
end end

View file

@ -1,7 +1 @@
= @form.hidden_field :value = @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"
= @form.hidden_field :external_id
= react_component("ComboPaysSearch",
required: @champ.required?,
id: @champ.input_id,
className: "width-33-desktop width-100-mobile",
describedby: @champ.describedby_id)

View file

@ -1,20 +0,0 @@
import React from 'react';
import { QueryClientProvider } from 'react-query';
import ComboSearch, { ComboSearchProps } from './ComboSearch';
import { queryClient } from './shared/queryClient';
export default function ComboPaysSearch(
props: ComboSearchProps<{ code: string; value: string; label: string }>
) {
return (
<QueryClientProvider client={queryClient}>
<ComboSearch
{...props}
scope="pays"
minimumInputLength={0}
transformResult={({ code, value, label }) => [code, value, label]}
/>
</QueryClientProvider>
);
}

View file

@ -21,19 +21,42 @@
# type_de_champ_id :integer # type_de_champ_id :integer
# #
class Champs::PaysChamp < Champs::TextChamp class Champs::PaysChamp < Champs::TextChamp
def localized_value def to_s
formatted_value
end
def for_tag
formatted_value
end
def selected
code || value
end
def value=(code)
if code&.size == 2
self.external_id = code
super(APIGeoService.country_name(code, locale: 'FR'))
elsif code.blank?
self.external_id = nil
super(nil)
elsif code != value
self.external_id = APIGeoService.country_code(code)
super(code)
end
end
def code
external_id || APIGeoService.country_code(value)
end
private
def formatted_value
if external_id if external_id
CountriesService.get(I18n.locale)[external_id].to_s APIGeoService.country_name(external_id)
else else
value.present? ? value.to_s : '' value.present? ? value.to_s : ''
end end
end end
def to_s
localized_value
end
def for_tag
localized_value
end
end end

View file

@ -0,0 +1,71 @@
describe Champs::PaysChamp, type: :model do
let(:champ) { described_class.new }
describe 'value' do
it 'with code' do
champ.value = 'GB'
expect(champ.external_id).to eq('GB')
expect(champ.value).to eq('Royaume-Uni')
expect(champ.selected).to eq('GB')
expect(champ.to_s).to eq('Royaume-Uni')
I18n.with_locale(:en) do
expect(champ.to_s).to eq('United Kingdom')
end
I18n.with_locale(:fr) do
expect(champ.to_s).to eq('Royaume-Uni')
end
end
it 'with name' do
champ.value = 'Royaume-Uni'
expect(champ.external_id).to eq('GB')
expect(champ.value).to eq('Royaume-Uni')
expect(champ.selected).to eq('GB')
expect(champ.to_s).to eq('Royaume-Uni')
end
it 'with nil' do
champ.write_attribute(:value, 'Royaume-Uni')
champ.write_attribute(:external_id, 'GB')
champ.value = nil
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 blank' do
champ.write_attribute(:value, 'Royaume-Uni')
champ.write_attribute(:external_id, 'GB')
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.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, 'Royaume-Uni')
expect(champ.external_id).to be_nil
expect(champ.value).to eq('Royaume-Uni')
expect(champ.selected).to eq('GB')
expect(champ.to_s).to eq('Royaume-Uni')
end
it 'with initial bad name' do
champ.write_attribute(:value, 'ROYAUME-UNIS')
expect(champ.external_id).to be_nil
expect(champ.value).to eq('ROYAUME-UNIS')
expect(champ.selected).to eq('ROYAUME-UNIS')
expect(champ.to_s).to eq('ROYAUME-UNIS')
end
end
end