From 4a07920c0cb6ac89182a86763b0705fa5c50382e Mon Sep 17 00:00:00 2001 From: clement-duport Date: Wed, 6 Nov 2024 10:57:21 +0100 Subject: [PATCH 1/4] keep the agency departments and kind field value when modifiying a convention --- .../forms/commons/AgencySelector.tsx | 6 + .../forms/convention/ConventionForm.tsx | 108 ++++++++++++------ .../forms/convention/conventionHelpers.ts | 35 ++++-- .../src/app/routes/routeParams/convention.ts | 3 + 4 files changed, 111 insertions(+), 41 deletions(-) diff --git a/front/src/app/components/forms/commons/AgencySelector.tsx b/front/src/app/components/forms/commons/AgencySelector.tsx index ea904027cd..c784b9e1f9 100644 --- a/front/src/app/components/forms/commons/AgencySelector.tsx +++ b/front/src/app/components/forms/commons/AgencySelector.tsx @@ -98,6 +98,11 @@ export const AgencySelector = ({ [agencyDepartmentField.name], ); + const agencyKindFieldName = useMemo( + () => agencyKindField.name as keyof SupportedFormsDto, + [agencyKindField.name], + ); + const agencyDepartment = useWatch({ name: agencyDepartmentFieldName, control, @@ -187,6 +192,7 @@ export const AgencySelector = ({ disabled={agencyKindOptions.length === 0 || shouldLockToPeAgencies} nativeSelectProps={{ ...agencyKindField, + ...register(agencyKindFieldName), onChange: (event) => setAllowedAgencyKinds( event.currentTarget.value === "all" diff --git a/front/src/app/components/forms/convention/ConventionForm.tsx b/front/src/app/components/forms/convention/ConventionForm.tsx index 78cb3bf311..a60e01a4c4 100644 --- a/front/src/app/components/forms/convention/ConventionForm.tsx +++ b/front/src/app/components/forms/convention/ConventionForm.tsx @@ -23,12 +23,12 @@ import { useDispatch, useSelector } from "react-redux"; import { AgencyKindFilter, Beneficiary, + ConventionInternshipKindSpecific, ConventionReadDto, DepartmentCode, FederatedIdentity, InternshipKind, addressDtoToString, - conventionSchema, domElementIds, hasBeneficiaryCurrentEmployer, isBeneficiaryMinor, @@ -62,6 +62,7 @@ import { import { ConventionFeedbackNotification } from "src/app/components/forms/convention/ConventionFeedbackNotification"; import { ConventionPresentation, + conventionPresentationSchema, undefinedIfEmptyString, } from "src/app/components/forms/convention/conventionHelpers"; @@ -163,9 +164,10 @@ export const ConventionForm = ({ useWaitForReduxFormUiReadyBeforeInitialisation(initialValues); const defaultValues = mode === "create" ? initialValues : fetchedConvention || initialValues; - const methods = useForm({ + + const methods = useForm>({ defaultValues, - resolver: zodResolver(conventionSchema), + resolver: zodResolver(conventionPresentationSchema), mode: "onTouched", }); @@ -203,23 +205,71 @@ export const ConventionForm = ({ conventionValues.internshipKind ?? "immersion", ); - const onSubmit: SubmitHandler = (convention) => { - const conventionToSave: ConventionReadDto = { - ...convention, - workConditions: undefinedIfEmptyString(convention.workConditions), - establishmentNumberEmployeesRange: - establishmentNumberEmployeesRange === "" - ? undefined - : establishmentNumberEmployeesRange, - }; - dispatch( - conventionSlice.actions.showSummaryChangeRequested({ - showSummary: true, - convention: conventionToSave, - }), + const isImmersionConvention = ( + convention: ConventionPresentation, + ): convention is Required & { + internshipKind: "immersion"; + } => convention.internshipKind === "immersion"; + + const isMiniStage = ( + convention: ConventionPresentation, + ): convention is Required & { + signatories: ConventionInternshipKindSpecific<"mini-stage-cci">["signatories"]; + internshipKind: "mini-stage-cci"; + } => { + return ( + convention.internshipKind === "mini-stage-cci" && + "levelOfEducation" in convention.signatories.beneficiary ); }; + const onSubmit: SubmitHandler> = ( + convention, + ) => { + if (isMiniStage(convention)) { + const conventionToSave: ConventionReadDto = { + ...convention, + internshipKind: convention.internshipKind, + workConditions: undefinedIfEmptyString(convention.workConditions), + establishmentNumberEmployeesRange: + establishmentNumberEmployeesRange === "" + ? undefined + : establishmentNumberEmployeesRange, + agencySiret: "", + agencyName: "", + agencyCounsellorEmails: [], + agencyValidatorEmails: [], + }; + + dispatch( + conventionSlice.actions.showSummaryChangeRequested({ + showSummary: true, + convention: conventionToSave, + }), + ); + } else if (isImmersionConvention(convention)) { + const conventionToSave: ConventionReadDto = { + ...convention, + workConditions: undefinedIfEmptyString(convention.workConditions), + establishmentNumberEmployeesRange: + establishmentNumberEmployeesRange === "" + ? undefined + : establishmentNumberEmployeesRange, + agencySiret: "", + agencyName: "", + agencyCounsellorEmails: [], + agencyValidatorEmails: [], + }; + + dispatch( + conventionSlice.actions.showSummaryChangeRequested({ + showSummary: true, + convention: conventionToSave, + }), + ); + } + }; + const accordionsRef = useRef>>([]); const [stepsStatus, setStepsStatus] = useState> => { const stepFields = formUiSections[step - 1]; const validatedFields = stepFields.map(async (field) => ({ - [field]: await trigger(field as keyof ConventionReadDto), + [field]: await trigger(field as keyof ConventionPresentation), })); const validatedFieldsValue = await Promise.all(validatedFields); const getStepStatus = () => { @@ -312,7 +362,7 @@ export const ConventionForm = ({ ).length; const stepIsTouched = stepFields.filter( (stepField) => - getFieldState(stepField as keyof ConventionReadDto).isTouched, + getFieldState(stepField as keyof ConventionPresentation).isTouched, ).length; if (validatedFieldsValue.every((field) => Object.values(field)[0])) { return "success"; @@ -384,6 +434,12 @@ export const ConventionForm = ({ data-matomo-name={domElementIds.conventionImmersionRoute.form({ mode, })} + onSubmit={(e) => + handleSubmit(onSubmit, (errors) => { + validateSteps("doNotClear"); + console.error(conventionValues, errors); + })(e) + } > <> <> @@ -545,16 +601,10 @@ export const ConventionForm = ({ disabled={shouldSubmitButtonBeDisabled} iconId="fr-icon-checkbox-circle-line" iconPosition="left" - type="button" + type="submit" nativeButtonProps={{ id: domElementIds.conventionImmersionRoute.submitFormButton, }} - onClick={(e) => - handleSubmit(onSubmit, (errors) => { - validateSteps("doNotClear"); - console.error(conventionValues, errors); - })(e) - } > VĂ©rifier la demande @@ -583,12 +633,6 @@ export const ConventionForm = ({