diff --git a/app/components/editable_champ/regions_component.rb b/app/components/editable_champ/regions_component.rb index 5aea4ff3b..b488f1128 100644 --- a/app/components/editable_champ/regions_component.rb +++ b/app/components/editable_champ/regions_component.rb @@ -1,3 +1,13 @@ class EditableChamp::RegionsComponent < EditableChamp::EditableChampBaseComponent include ApplicationHelper + + private + + def options + APIGeoService.regions.map { [_1[:name], _1[:code]] } + end + + def select_options + { selected: @champ.selected }.merge(@champ.mandatory? ? { prompt: '' } : { include_blank: '' }) + end end diff --git a/app/components/editable_champ/regions_component/regions_component.html.haml b/app/components/editable_champ/regions_component/regions_component.html.haml index e9961421a..c5a6f744d 100644 --- a/app/components/editable_champ/regions_component/regions_component.html.haml +++ b/app/components/editable_champ/regions_component/regions_component.html.haml @@ -1,7 +1 @@ -= @form.hidden_field :value -= @form.hidden_field :external_id -= react_component("ComboRegionsSearch", - 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/ComboRegionsSearch.tsx b/app/javascript/components/ComboRegionsSearch.tsx deleted file mode 100644 index a1afcc95c..000000000 --- a/app/javascript/components/ComboRegionsSearch.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 ComboRegionsSearch( - props: ComboSearchProps<{ code: string; nom: string }> -) { - return ( - - [code, nom]} - /> - - ); -} diff --git a/app/models/champs/region_champ.rb b/app/models/champs/region_champ.rb index 1076cd701..ec9450af8 100644 --- a/app/models/champs/region_champ.rb +++ b/app/models/champs/region_champ.rb @@ -21,4 +21,25 @@ # type_de_champ_id :integer # class Champs::RegionChamp < Champs::TextChamp + def selected + code + end + + def name + value + end + + def code + external_id || APIGeoService.region_code(value) + end + + def value=(code) + if code&.size == 2 + self.external_id = code + super(APIGeoService.region_name(code)) + elsif code.blank? + self.external_id = nil + super(nil) + end + end end diff --git a/app/views/shared/champs/regions/_show.html.haml b/app/views/shared/champs/regions/_show.html.haml index 693f20ff4..0a3867677 100644 --- a/app/views/shared/champs/regions/_show.html.haml +++ b/app/views/shared/champs/regions/_show.html.haml @@ -1,4 +1,4 @@ - if champ.external_id.present? - = format_text_value("#{champ.external_id} - #{champ}") + = format_text_value("#{champ.external_id} – #{champ}") - else = format_text_value(champ.to_s) diff --git a/spec/models/champs/region_champ_spec.rb b/spec/models/champs/region_champ_spec.rb new file mode 100644 index 000000000..fea2fd94d --- /dev/null +++ b/spec/models/champs/region_champ_spec.rb @@ -0,0 +1,56 @@ +describe Champs::RegionChamp, 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_regions' } do + it 'with code' do + champ.value = '01' + expect(champ.external_id).to eq('01') + expect(champ.value).to eq('Guadeloupe') + expect(champ.selected).to eq('01') + expect(champ.to_s).to eq('Guadeloupe') + end + + it 'with nil' do + champ.write_attribute(:value, 'Guadeloupe') + champ.write_attribute(:external_id, '01') + 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, 'Guadeloupe') + 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.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, 'Guadeloupe') + expect(champ.external_id).to be_nil + expect(champ.value).to eq('Guadeloupe') + expect(champ.selected).to eq('01') + expect(champ.to_s).to eq('Guadeloupe') + end + end +end