Merge pull request #6651 from betagouv/main

2021-11-17-01
This commit is contained in:
krichtof 2021-11-17 15:10:59 +01:00 committed by GitHub
commit 2167c02454
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 8 deletions

View file

@ -244,8 +244,8 @@ module Instructeurs
def champs_private_params def champs_private_params
params.require(:dossier).permit(champs_private_attributes: [ params.require(:dossier).permit(champs_private_attributes: [
:id, :primary_value, :secondary_value, :piece_justificative_file, :value, 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: []] 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 end

View file

@ -337,8 +337,8 @@ module Users
def champs_params def champs_params
params.permit(dossier: { params.permit(dossier: {
champs_attributes: [ champs_attributes: [
:id, :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, 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 end

View file

@ -49,12 +49,31 @@ const [placeholderDepartement, placeholderCommune] =
]; ];
function ComboCommunesSearch(params) { 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( const inputId = useMemo(
() => () =>
document.querySelector(`input[data-uuid="${params.hiddenFieldId}"]`)?.id, document.querySelector(`input[data-uuid="${params.hiddenFieldId}"]`)?.id,
[params.hiddenFieldId] [params.hiddenFieldId]
); );
const [departementCode, setDepartementCode] = useState(
() => hiddenCodeDepartementField?.value
);
const departementValue = useMemo(
() => hiddenDepartementField?.value,
[hiddenDepartementField]
);
const departementDescribedBy = `${inputId}_departement_notice`; const departementDescribedBy = `${inputId}_departement_notice`;
const communeDescribedBy = `${inputId}_commune_notice`; const communeDescribedBy = `${inputId}_commune_notice`;
@ -68,12 +87,17 @@ function ComboCommunesSearch(params) {
</p> </p>
</div> </div>
<ComboDepartementsSearch <ComboDepartementsSearch
value={departementValue}
inputId={!departementCode ? inputId : null} inputId={!departementCode ? inputId : null}
aria-describedby={departementDescribedBy} aria-describedby={departementDescribedBy}
placeholder={placeholderDepartement} placeholder={placeholderDepartement}
mandatory={params.mandatory} required={params.mandatory}
onChange={(_, result) => { onChange={(value, result) => {
setDepartementCode(result?.code); setDepartementCode(result?.code);
if (hiddenDepartementField && hiddenCodeDepartementField) {
hiddenDepartementField.setAttribute('value', value);
hiddenCodeDepartementField.setAttribute('value', result?.code);
}
}} }}
/> />
</div> </div>

View file

@ -51,7 +51,7 @@ function ComboSearch({
), ),
[hiddenFieldId] [hiddenFieldId]
); );
const initialValue = hiddenValueField && hiddenValueField.value; const initialValue = hiddenValueField ? hiddenValueField.value : props.value;
const [searchTerm, setSearchTerm] = useState(''); const [searchTerm, setSearchTerm] = useState('');
const [debouncedSearchTerm] = useDebounce(searchTerm, 300); const [debouncedSearchTerm] = useDebounce(searchTerm, 300);
const [value, setValue] = useState(initialValue); const [value, setValue] = useState(initialValue);
@ -177,6 +177,7 @@ function ComboSearch({
} }
ComboSearch.propTypes = { ComboSearch.propTypes = {
value: PropTypes.string,
hiddenFieldId: PropTypes.string, hiddenFieldId: PropTypes.string,
scope: PropTypes.string, scope: PropTypes.string,
minimumInputLength: PropTypes.number, minimumInputLength: PropTypes.number,

View file

@ -20,6 +20,8 @@
# type_de_champ_id :integer # type_de_champ_id :integer
# #
class Champs::CommuneChamp < Champs::TextChamp class Champs::CommuneChamp < Champs::TextChamp
store_accessor :value_json, :departement, :code_departement
def for_export def for_export
[value, external_id] [value, external_id]
end end

View file

@ -1,4 +1,6 @@
- hidden_field_id = SecureRandom.uuid - hidden_field_id = SecureRandom.uuid
= form.hidden_field :value, { data: { uuid: hidden_field_id } } = form.hidden_field :value, { data: { uuid: hidden_field_id } }
= form.hidden_field :external_id, { data: { reference: true } } = 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) = react_component("ComboCommunesSearch", mandatory: champ.mandatory?, hiddenFieldId: hidden_field_id)