diff --git a/app/controllers/instructeurs/dossiers_controller.rb b/app/controllers/instructeurs/dossiers_controller.rb index a148e129a..1f9bc3dd9 100644 --- a/app/controllers/instructeurs/dossiers_controller.rb +++ b/app/controllers/instructeurs/dossiers_controller.rb @@ -244,8 +244,8 @@ module Instructeurs def champs_private_params params.require(:dossier).permit(champs_private_attributes: [ - :id, :primary_value, :secondary_value, :piece_justificative_file, :value, value: [], - champs_attributes: [:id, :_destroy, :value, :primary_value, :secondary_value, :piece_justificative_file, value: []] + :id, :primary_value, :secondary_value, :piece_justificative_file, :value_other, :external_id, :numero_allocataire, :code_postal, :departement, :code_departement, :value, value: [], + champs_attributes: [:id, :_destroy, :value, :primary_value, :secondary_value, :piece_justificative_file, :value_other, :external_id, :numero_allocataire, :code_postal, :departement, :code_departement, value: []] ]) end diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 6ba476c78..0f0c33f1d 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -337,8 +337,8 @@ module Users def champs_params params.permit(dossier: { champs_attributes: [ - :id, :value, :value_other, :external_id, :primary_value, :secondary_value, :numero_allocataire, :code_postal, :piece_justificative_file, value: [], - champs_attributes: [:id, :_destroy, :value, :value_other, :external_id, :primary_value, :secondary_value, :numero_allocataire, :code_postal, :piece_justificative_file, value: []] + :id, :value, :value_other, :external_id, :primary_value, :secondary_value, :numero_allocataire, :code_postal, :piece_justificative_file, :departement, :code_departement, value: [], + champs_attributes: [:id, :_destroy, :value, :value_other, :external_id, :primary_value, :secondary_value, :numero_allocataire, :code_postal, :piece_justificative_file, :departement, :code_departement, value: []] ] }) end diff --git a/app/javascript/components/ComboCommunesSearch.jsx b/app/javascript/components/ComboCommunesSearch.jsx index b02c5070e..ae2b005ba 100644 --- a/app/javascript/components/ComboCommunesSearch.jsx +++ b/app/javascript/components/ComboCommunesSearch.jsx @@ -49,12 +49,31 @@ const [placeholderDepartement, placeholderCommune] = ]; function ComboCommunesSearch(params) { - const [departementCode, setDepartementCode] = useState(); + const hiddenDepartementFieldId = `${params.hiddenFieldId}:departement`; + const hiddenDepartementField = useMemo( + () => + document.querySelector(`input[data-attr="${hiddenDepartementFieldId}"]`), + [params.hiddenFieldId] + ); + const hiddenCodeDepartementField = useMemo( + () => + document.querySelector( + `input[data-attr="${params.hiddenFieldId}:code_departement"]` + ), + [params.hiddenFieldId] + ); const inputId = useMemo( () => document.querySelector(`input[data-uuid="${params.hiddenFieldId}"]`)?.id, [params.hiddenFieldId] ); + const [departementCode, setDepartementCode] = useState( + () => hiddenCodeDepartementField?.value + ); + const departementValue = useMemo( + () => hiddenDepartementField?.value, + [hiddenDepartementField] + ); const departementDescribedBy = `${inputId}_departement_notice`; const communeDescribedBy = `${inputId}_commune_notice`; @@ -68,12 +87,17 @@ function ComboCommunesSearch(params) {

{ + required={params.mandatory} + onChange={(value, result) => { setDepartementCode(result?.code); + if (hiddenDepartementField && hiddenCodeDepartementField) { + hiddenDepartementField.setAttribute('value', value); + hiddenCodeDepartementField.setAttribute('value', result?.code); + } }} /> diff --git a/app/javascript/components/ComboSearch.jsx b/app/javascript/components/ComboSearch.jsx index 563f494d5..8677f2b5d 100644 --- a/app/javascript/components/ComboSearch.jsx +++ b/app/javascript/components/ComboSearch.jsx @@ -51,7 +51,7 @@ function ComboSearch({ ), [hiddenFieldId] ); - const initialValue = hiddenValueField && hiddenValueField.value; + const initialValue = hiddenValueField ? hiddenValueField.value : props.value; const [searchTerm, setSearchTerm] = useState(''); const [debouncedSearchTerm] = useDebounce(searchTerm, 300); const [value, setValue] = useState(initialValue); @@ -177,6 +177,7 @@ function ComboSearch({ } ComboSearch.propTypes = { + value: PropTypes.string, hiddenFieldId: PropTypes.string, scope: PropTypes.string, minimumInputLength: PropTypes.number, diff --git a/app/models/champs/commune_champ.rb b/app/models/champs/commune_champ.rb index 4477da8e3..5f3ccdc05 100644 --- a/app/models/champs/commune_champ.rb +++ b/app/models/champs/commune_champ.rb @@ -20,6 +20,8 @@ # type_de_champ_id :integer # class Champs::CommuneChamp < Champs::TextChamp + store_accessor :value_json, :departement, :code_departement + def for_export [value, external_id] end diff --git a/app/views/shared/dossiers/editable_champs/_communes.html.haml b/app/views/shared/dossiers/editable_champs/_communes.html.haml index 372f3b211..0e43f37b6 100644 --- a/app/views/shared/dossiers/editable_champs/_communes.html.haml +++ b/app/views/shared/dossiers/editable_champs/_communes.html.haml @@ -1,4 +1,6 @@ - hidden_field_id = SecureRandom.uuid = form.hidden_field :value, { data: { uuid: hidden_field_id } } = form.hidden_field :external_id, { data: { reference: true } } += form.hidden_field :departement, { data: { attr: "#{hidden_field_id}:departement" } } += form.hidden_field :code_departement, { data: { attr: "#{hidden_field_id}:code_departement" } } = react_component("ComboCommunesSearch", mandatory: champ.mandatory?, hiddenFieldId: hidden_field_id)