Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2366 - Les valeurs des champs département et type de l'agence ne disparaissent plus lorsque l'on clique su modifier la convention #2459

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions front/src/app/components/forms/commons/AgencySelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -187,6 +192,7 @@ export const AgencySelector = ({
disabled={agencyKindOptions.length === 0 || shouldLockToPeAgencies}
nativeSelectProps={{
...agencyKindField,
...register(agencyKindFieldName),
clement-duport marked this conversation as resolved.
Show resolved Hide resolved
onChange: (event) =>
setAllowedAgencyKinds(
event.currentTarget.value === "all"
Expand Down
40 changes: 21 additions & 19 deletions front/src/app/components/forms/convention/ConventionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -163,9 +164,10 @@ export const ConventionForm = ({
useWaitForReduxFormUiReadyBeforeInitialisation(initialValues);
const defaultValues =
mode === "create" ? initialValues : fetchedConvention || initialValues;
const methods = useForm<ConventionReadDto>({

const methods = useForm<Required<ConventionPresentation>>({
defaultValues,
resolver: zodResolver(conventionSchema),
resolver: zodResolver(conventionPresentationSchema),
mode: "onTouched",
});

Expand Down Expand Up @@ -203,14 +205,22 @@ export const ConventionForm = ({
conventionValues.internshipKind ?? "immersion",
);

const onSubmit: SubmitHandler<ConventionReadDto> = (convention) => {
const onSubmit: SubmitHandler<Required<ConventionPresentation>> = (
convention,
) => {
const conventionToSave: ConventionReadDto = {
...convention,
...conventionSchema.parse(convention),
agencyKind: convention.agencyKind,
agencyDepartment: convention.agencyDepartment,
workConditions: undefinedIfEmptyString(convention.workConditions),
establishmentNumberEmployeesRange:
establishmentNumberEmployeesRange === ""
? undefined
: establishmentNumberEmployeesRange,
agencySiret: "",
agencyName: "",
agencyCounsellorEmails: [],
agencyValidatorEmails: [],
};
dispatch(
conventionSlice.actions.showSummaryChangeRequested({
Expand Down Expand Up @@ -301,7 +311,7 @@ export const ConventionForm = ({
): Promise<Record<number, StepSeverity>> => {
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 = () => {
Expand All @@ -312,7 +322,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";
Expand Down Expand Up @@ -384,6 +394,10 @@ export const ConventionForm = ({
data-matomo-name={domElementIds.conventionImmersionRoute.form({
mode,
})}
onSubmit={handleSubmit(onSubmit, (errors) => {
validateSteps("doNotClear");
console.error(conventionValues, errors);
})}
>
<>
<>
Expand Down Expand Up @@ -545,16 +559,10 @@ export const ConventionForm = ({
disabled={shouldSubmitButtonBeDisabled}
iconId="fr-icon-checkbox-circle-line"
iconPosition="left"
type="button"
type="submit"
clement-duport marked this conversation as resolved.
Show resolved Hide resolved
nativeButtonProps={{
id: domElementIds.conventionImmersionRoute.submitFormButton,
}}
onClick={(e) =>
handleSubmit(onSubmit, (errors) => {
validateSteps("doNotClear");
console.error(conventionValues, errors);
})(e)
}
>
Vérifier la demande
</Button>
Expand Down Expand Up @@ -583,12 +591,6 @@ export const ConventionForm = ({
<ShareConventionLink />
<Button
type="submit"
onClick={(e) =>
handleSubmit(onSubmit, (errors) => {
validateSteps("doNotClear");
console.error(conventionValues, errors);
})(e)
}
id={
domElementIds.conventionImmersionRoute
.submitFormButtonMobile
Expand Down
34 changes: 25 additions & 9 deletions front/src/app/components/forms/convention/conventionHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {
ConventionDto,
DepartmentCode,
ConventionReadDto,
EstablishmentTutor,
InternshipKind,
OmitFromExistingKeys,
Signatories,
agencyKindSchema,
conventionSchema,
refersToAgencyIdSchema,
zStringMinLength1,
} from "shared";
import { z } from "zod";

export const undefinedIfEmptyString = (text?: string): string | undefined =>
text || undefined;
Expand All @@ -20,10 +24,6 @@ type WithEstablishmentTutor = {
establishmentTutor: EstablishmentTutor;
};

type WithAgencyDepartment = {
agencyDepartment: DepartmentCode;
};

type WithIntershipKind = {
internshipKind: InternshipKind;
};
Expand All @@ -33,11 +33,27 @@ type WithFromPeConnectedUser = {
};

export type ConventionPresentation = OmitFromExistingKeys<
Partial<ConventionDto>,
"statusJustification"
Partial<ConventionReadDto>,
| "agencyName"
| "agencyCounsellorEmails"
| "agencyValidatorEmails"
| "agencySiret"
> &
WithSignatures &
WithEstablishmentTutor &
WithIntershipKind &
WithAgencyDepartment &
WithFromPeConnectedUser;

export const conventionPresentationSchema: z.Schema<ConventionPresentation> =
conventionSchema.and(
z.object({
agencyDepartment: z.string(),
agencyRefersTo: z
.object({
id: refersToAgencyIdSchema,
name: zStringMinLength1,
kind: agencyKindSchema,
})
.optional(),
}),
);
3 changes: 3 additions & 0 deletions front/src/app/routes/routeParams/convention.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { addDays, startOfToday } from "date-fns";
import {
AgencyKind,
AppellationAndRomeDto,
AppellationCode,
BeneficiaryCurrentEmployer,
Expand Down Expand Up @@ -287,6 +288,7 @@ export const conventionValuesFromUrl = {
isRqth: param.query.optional.boolean,
birthdate: param.query.optional.string,
agencyDepartment: param.query.optional.string,
agencyKind: param.query.optional.string,

brEmail: param.query.optional.string,
brFirstName: param.query.optional.string,
Expand Down Expand Up @@ -409,6 +411,7 @@ const conventionPresentationFromParams = (
// Agency
agencyId: params.agencyId ?? undefined,
agencyDepartment: params.agencyDepartment ?? "",
agencyKind: params.agencyKind as AgencyKind | undefined,

//Actors
establishmentTutor: {
Expand Down
Loading