fix(champs): save departement info on champ commune

This commit is contained in:
Paul Chavard 2021-11-17 14:52:47 +03:00
parent 333a4029af
commit ae09b37e72
6 changed files with 37 additions and 8 deletions

View file

@ -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

View file

@ -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

View file

@ -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) {
</p>
</div>
<ComboDepartementsSearch
value={departementValue}
inputId={!departementCode ? inputId : null}
aria-describedby={departementDescribedBy}
placeholder={placeholderDepartement}
mandatory={params.mandatory}
onChange={(_, result) => {
required={params.mandatory}
onChange={(value, result) => {
setDepartementCode(result?.code);
if (hiddenDepartementField && hiddenCodeDepartementField) {
hiddenDepartementField.setAttribute('value', value);
hiddenCodeDepartementField.setAttribute('value', result?.code);
}
}}
/>
</div>

View file

@ -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,

View file

@ -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

View file

@ -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)