diff --git a/app/components/editable_champ/pays_component.rb b/app/components/editable_champ/pays_component.rb index e6d4da952..9997af382 100644 --- a/app/components/editable_champ/pays_component.rb +++ b/app/components/editable_champ/pays_component.rb @@ -1,3 +1,18 @@ class EditableChamp::PaysComponent < EditableChamp::EditableChampBaseComponent 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 diff --git a/app/components/editable_champ/pays_component/pays_component.html.haml b/app/components/editable_champ/pays_component/pays_component.html.haml index d2625b6f3..c5a6f744d 100644 --- a/app/components/editable_champ/pays_component/pays_component.html.haml +++ b/app/components/editable_champ/pays_component/pays_component.html.haml @@ -1,7 +1 @@ -= @form.hidden_field :value -= @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) += @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" diff --git a/app/javascript/components/ComboPaysSearch.tsx b/app/javascript/components/ComboPaysSearch.tsx deleted file mode 100644 index c69aaaa4b..000000000 --- a/app/javascript/components/ComboPaysSearch.tsx +++ /dev/null @@ -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 ( - - [code, value, label]} - /> - - ); -} diff --git a/app/models/champs/pays_champ.rb b/app/models/champs/pays_champ.rb index fbf57d4cb..80d580cb7 100644 --- a/app/models/champs/pays_champ.rb +++ b/app/models/champs/pays_champ.rb @@ -21,19 +21,42 @@ # type_de_champ_id :integer # 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 - CountriesService.get(I18n.locale)[external_id].to_s + APIGeoService.country_name(external_id) else value.present? ? value.to_s : '' end end - - def to_s - localized_value - end - - def for_tag - localized_value - end end diff --git a/spec/models/champs/pays_champ_spec.rb b/spec/models/champs/pays_champ_spec.rb new file mode 100644 index 000000000..2110261e0 --- /dev/null +++ b/spec/models/champs/pays_champ_spec.rb @@ -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