Update DeedTypeCreate

This commit is contained in:
Sosthene 2025-09-11 14:13:09 +02:00
parent 4540ac726a
commit b4d8562519

View File

@ -7,7 +7,6 @@ import Typography, { ETypo } from "@Front/Components/DesignSystem/Typography";
import DefaultDeedTypesDashboard from "@Front/Components/LayoutTemplates/DefaultDeedTypeDashboard";
import { ToasterService } from "@Front/Components/DesignSystem/Toaster";
import Module from "@Front/Config/Module";
import { DeedType, Office } from "le-coffre-resources/dist/Admin";
import { useRouter } from "next/router";
import { useCallback, useState } from "react";
@ -19,6 +18,7 @@ import UserStore from "@Front/Stores/UserStore";
import LoaderService from "src/common/Api/LeCoffreApi/sdk/Loader/LoaderService";
import DeedTypeService from "src/common/Api/LeCoffreApi/sdk/DeedTypeService";
import { DEFAULT_VALIDATOR_ID } from "@Front/Config/AppConstants";
import Auth from "@Front/Api/Auth/IdNot";
type IProps = {};
export default function DeedTypesCreate(props: IProps) {
@ -31,27 +31,44 @@ export default function DeedTypesCreate(props: IProps) {
async (e: React.FormEvent<HTMLFormElement> | null, values: { [key: string]: string }) => {
try {
const user: any = UserStore.instance.getUser();
const officeId: string = user.office.uid;
const deedType = DeedType.hydrate<DeedType>({
name: values["name"],
description: values["description"],
office: Office.hydrate<Office>({
uid: officeId,
}),
});
try {
await validateOrReject(deedType, { groups: ["createDeedType"], forbidUnknownValues: true });
} catch (validationErrors: Array<ValidationError> | any) {
setValidationError(validationErrors as ValidationError[]);
if (!user) {
console.error("DeedTypesCreate: User not found - user is null or undefined");
return;
}
const res = await Auth.getInstance().getOfficeProcessByIdNot();
if (!res.success) {
console.error("DeedTypesCreate: Office not found - office is null or undefined");
return;
}
const officeId: string | undefined = res.data.processId;
const officeIdNot: string | undefined = res.data.processData['idNot'];
if (!officeId || !officeIdNot) {
console.error("DeedTypesCreate: officeId or officeIdNot is undefined - office.processData.idNot is missing");
return;
}
// TODO: We should update the type definition to be able to use validation again
// const deedType = DeedType.hydrate<DeedType>({
// name: values["name"],
// description: values["description"],
// office: Office.hydrate<Office>({
// idNot: officeId,
// }),
// });
// try {
// await validateOrReject(deedType, { groups: ["createDeedType"], forbidUnknownValues: true });
// } catch (validationErrors: Array<ValidationError> | any) {
// console.log("validationErrors", validationErrors);
// setValidationError(Array.isArray(validationErrors) ? validationErrors : []);
// return;
// }
const deedTypeData: any = {
name: values["name"],
description: values["description"],
office: {
uid: officeId,
idNot: officeIdNot,
}
};
@ -61,15 +78,18 @@ export default function DeedTypesCreate(props: IProps) {
title: "Succès !",
description: "Type d'acte créé avec succès"
});
const deedTypeUid = processCreated.processId;
// Remove ':0' suffix from processId for URL navigation
const urlId = deedTypeUid.replace(/:0$/, '');
router.push(
Module.getInstance()
.get()
.modules.pages.DeedTypes.pages.DeedTypesInformations.props.path.replace("[uid]", processCreated.processData.uid),
.modules.pages.DeedTypes.pages.DeedTypesInformations.props.path.replace("[uid]", urlId),
);
LoaderService.getInstance().hide();
});
} catch (validationErrors: Array<ValidationError> | any) {
setValidationError(validationErrors as ValidationError[]);
setValidationError(Array.isArray(validationErrors) ? validationErrors : []);
return;
}
},
@ -106,12 +126,12 @@ export default function DeedTypesCreate(props: IProps) {
<TextField
name="name"
placeholder="Nom de l'acte"
validationError={validationError.find((error) => error.property === "name")}
validationError={Array.isArray(validationError) ? validationError.find((error) => error.property === "name") : undefined}
/>
<TextAreaField
name="description"
placeholder="Description"
validationError={validationError.find((error) => error.property === "description")}
validationError={Array.isArray(validationError) ? validationError.find((error) => error.property === "description") : undefined}
/>
<div className={classes["buttons-container"]}>
<Button variant={EButtonVariant.PRIMARY} styletype={EButtonstyletype.OUTLINED} onClick={onCancel}>