diff --git a/src/front/Components/Layouts/DeedTypes/DeedTypesEdit/index.tsx b/src/front/Components/Layouts/DeedTypes/DeedTypesEdit/index.tsx index 10d85b8f..c7c680b0 100644 --- a/src/front/Components/Layouts/DeedTypes/DeedTypesEdit/index.tsx +++ b/src/front/Components/Layouts/DeedTypes/DeedTypesEdit/index.tsx @@ -16,6 +16,7 @@ import classes from "./classes.module.scss"; import DeedTypeService from "src/common/Api/LeCoffreApi/sdk/DeedTypeService"; import LoaderService from "src/common/Api/LeCoffreApi/sdk/Loader/LoaderService"; +import MessageBus from "src/sdk/MessageBus"; export default function DeedTypesEdit() { const router = useRouter(); @@ -31,10 +32,11 @@ export default function DeedTypesEdit() { async function getDeedType() { if (!deedTypeUid) return; LoaderService.getInstance().show(); - DeedTypeService.getDeedTypeByUid(deedTypeUid as string).then((process: any) => { - if (process) { - const deedType: any = process.processData; - setDeedTypeSelected(deedType); + // deedTypeUid comes from URL without ':0' suffix, add it back for API calls + const processId = (deedTypeUid as string) + ':0'; + MessageBus.getInstance().getProcessData(processId).then((processData: any) => { + if (processData) { + setDeedTypeSelected(processData); } LoaderService.getInstance().hide(); }); @@ -47,51 +49,52 @@ export default function DeedTypesEdit() { setIsConfirmModalVisible(false); }, []); - const onSubmitHandler = useCallback( - async (e: React.FormEvent | null, values: { [key: string]: string | undefined }) => { - const deedType = DeedType.hydrate({ - uid: deedTypeUid as string, - name: values["name"], - description: values["description"], - }); - try { - await deedType.validateOrReject?.({ groups: ["updateDeedType"], forbidUnknownValues: true }); - } catch (validationErrors: Array | any) { - if (!Array.isArray(validationErrors)) return; - setValidationError(validationErrors as ValidationError[]); - return; - } - try { - LoaderService.getInstance().show(); - DeedTypeService.getDeedTypeByUid(deedTypeUid as string).then(async (process: any) => { - if (process) { - // New data - const newData: any = { - name: values["name"], - description: values["description"] - }; + const onSubmitHandler = async (e: React.FormEvent | null, values: { [key: string]: string | undefined }) => { + // const deedType = DeedType.hydrate({ + // name: values["name"], + // description: values["description"], + // }); + + // try { + // await deedType.validateOrReject?.({ groups: ["updateDeedType"], forbidUnknownValues: true }); + // } catch (validationErrors: Array | any) { + // if (!Array.isArray(validationErrors)) return; + // setValidationError(validationErrors as ValidationError[]); + // return; + // } + + try { + LoaderService.getInstance().show(); + // deedTypeUid comes from URL without ':0' suffix, add it back for API calls + const processId = (deedTypeUid as string) + ':0'; + + const process = await MessageBus.getInstance().getProcessData(processId); + if (process) { + console.log('process', process); + // New data + const newData: any = { + name: values["name"], + description: values["description"] + }; - // Merge process data with new data & update process - process.processData.name = newData.name; - process.processData.description = newData.description; - await DeedTypeService.updateDeedType(process, newData); + // Merge process data with new data & update process + process[processId].name = newData.name; + process[processId].description = newData.description; + await DeedTypeService.updateDeedType(processId, newData); - router.push( - Module.getInstance() - .get() - .modules.pages.DeedTypes.pages.DeedTypesInformations.props.path.replace("[uid]", deedTypeUid as string), - ); - LoaderService.getInstance().hide(); - } - }); - } catch (validationErrors) { - if (!Array.isArray(validationErrors)) return; - setValidationError(validationErrors as ValidationError[]); - return; + router.push( + Module.getInstance() + .get() + .modules.pages.DeedTypes.pages.DeedTypesInformations.props.path.replace("[uid]", deedTypeUid as string), + ); } - }, - [deedTypeUid, router], - ); + } catch (error) { + console.error('Error updating deed type:', error); + // Handle error appropriately + } finally { + LoaderService.getInstance().hide(); + } + }; const onFieldChange = useCallback((name: string, field: any) => { setHasChanged(true);