Skip to content

Commit

Permalink
fix displayed beneficiary representative
Browse files Browse the repository at this point in the history
  • Loading branch information
celineung committed Dec 10, 2024
1 parent 362a796 commit 5c99a5b
Showing 1 changed file with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { RadioButtons } from "@codegouvfr/react-dsfr/RadioButtons";
import { Select } from "@codegouvfr/react-dsfr/SelectNext";
import { differenceInYears } from "date-fns";
import { keys } from "ramda";
import React, { useCallback, useEffect, useRef, useState } from "react";
import React, { useCallback, useEffect, useState } from "react";
import { useFormContext } from "react-hook-form";
import { useDispatch } from "react-redux";
import {
Expand Down Expand Up @@ -68,15 +68,21 @@ export const BeneficiaryFormSection = ({
formConventionFieldsLabels(values.internshipKind),
);
const formContents = getFormFields();
const initialBeneficiaryRepresentative = useRef(
values.signatories.beneficiaryRepresentative,
).current;

useEffect(() => {
if (hasBeneficiaryRepresentativeData(initialBeneficiaryRepresentative)) {
if (
hasBeneficiaryRepresentativeData(
values.signatories.beneficiaryRepresentative,
) &&
!isMinorOrProtected
) {
dispatch(conventionSlice.actions.isMinorChanged(true));
}
}, [dispatch, initialBeneficiaryRepresentative]);
}, [
dispatch,
values.signatories.beneficiaryRepresentative,
isMinorOrProtected,
]);

useEffect(() => {
if (userFieldsAreFilled) {
Expand Down Expand Up @@ -118,12 +124,15 @@ export const BeneficiaryFormSection = ({
new Date(value),
);
const newIsMinor = age < 18;
newIsMinor
? setValue(
"signatories.beneficiaryRepresentative.role",
"beneficiary-representative",
)
: setValue("signatories.beneficiaryRepresentative", undefined);
if (newIsMinor) {
setValue(
"signatories.beneficiaryRepresentative.role",
"beneficiary-representative",
);
}
if (!newIsMinor && !isMinorOrProtected) {
setValue("signatories.beneficiaryRepresentative", undefined);
}
setIsMinorAccordingToAge(newIsMinor);
dispatch(conventionSlice.actions.isMinorChanged(newIsMinor));
},
Expand Down

0 comments on commit 5c99a5b

Please sign in to comment.