diff --git a/src/front/Components/Layouts/DeedTypes/DeedTypesInformations/index.tsx b/src/front/Components/Layouts/DeedTypes/DeedTypesInformations/index.tsx index c05d984b..7a472e84 100644 --- a/src/front/Components/Layouts/DeedTypes/DeedTypesInformations/index.tsx +++ b/src/front/Components/Layouts/DeedTypes/DeedTypesInformations/index.tsx @@ -21,6 +21,7 @@ import classes from "./classes.module.scss"; import DeedTypeService from "src/common/Api/LeCoffreApi/sdk/DeedTypeService"; import DocumentTypeService from "src/common/Api/LeCoffreApi/sdk/DocumentTypeService"; import LoaderService from "src/common/Api/LeCoffreApi/sdk/Loader/LoaderService"; +import MessageBus from "src/sdk/MessageBus"; type IProps = {}; export default function DeedTypesInformations(props: IProps) { @@ -52,7 +53,9 @@ export default function DeedTypesInformations(props: IProps) { const deleteDeedType = useCallback(async () => { LoaderService.getInstance().show(); - DeedTypeService.getDeedTypeByUid(deedTypeUid as string).then(async (process: any) => { + // deedTypeUid comes from URL without ':0' suffix, add it back for API calls + const processId = (deedTypeUid as string) + ':0'; + MessageBus.getInstance().getProcessData(processId).then(async (process: any) => { if (process) { // New data const newData: any = { @@ -80,9 +83,12 @@ export default function DeedTypesInformations(props: IProps) { if (!deedTypeUid) return; setSelectedDocuments([]); - DeedTypeService.getDeedTypeByUid(deedTypeUid as string).then((process: any) => { + // deedTypeUid comes from URL without ':0' suffix, add it back for API calls + const processId = (deedTypeUid as string) + ':0'; + MessageBus.getInstance().getProcessData(processId).then((process: any) => { if (process) { - const deedType: any = process.processData; + console.log('[DeedTypesInformations] process', process); + const deedType: any = process; setDeedTypeSelected(deedType); if (!deedType.document_types) return; @@ -95,15 +101,17 @@ export default function DeedTypesInformations(props: IProps) { }) .sort((a: any, b: any) => a.label.localeCompare(b.label)); setSelectedDocuments(documentsOptions); + } else { + console.warn('[DeedTypesInformations] process not found:', processId); } }); } - async function getDocuments() { + function getDocuments() { setAvailableDocuments([]); - DocumentTypeService.getDocumentTypes().then((processes: any[]) => { - if (processes.length) { - const documents: any[] = processes.map((process: any) => process.processData); + DocumentTypeService.getDocumentTypes((processes: Record) => { + const documents = Object.values(processes); + if (documents.length) { setAvailableDocuments(documents); } }); @@ -120,32 +128,36 @@ export default function DeedTypesInformations(props: IProps) { [openSaveModal], ); - const saveDocumentTypes = useCallback(() => { + const saveDocumentTypes = useCallback(async () => { LoaderService.getInstance().show(); - DeedTypeService.getDeedTypeByUid(deedTypeUid as string).then(async (process: any) => { - if (process) { - const deedType: any = process.processData; + // deedTypeUid comes from URL without ':0' suffix, add it back for API calls + const processId = (deedTypeUid as string) + ':0'; + console.log('[DeedTypesInformations] processId', processId); + const deedType = (await MessageBus.getInstance().getProcessData(processId))[processId]; + if (deedType) { + console.log('[DeedTypesInformations] deedType', deedType); - let document_types: any[] = deedType.document_types; - if (!document_types) { - document_types = []; - } - selectedDocuments.map((selectedDocument: any) => selectedDocument.id as string) - .forEach((uid: any) => document_types.push(availableDocuments.find((document: any) => document.uid === uid))); - - // New data - const newData: any = { - document_types: document_types - }; - - // Merge process data with new data & update process - process.processData.document_types = newData.document_types; - await DeedTypeService.updateDeedType(process, newData); - - LoaderService.getInstance().hide(); - closeSaveModal(); + let document_types: any[] = deedType['document_types']; + if (!document_types) { + document_types = []; } - }); + selectedDocuments.map((selectedDocument: any) => selectedDocument.id as string) + .forEach((uid: any) => document_types.push(availableDocuments.find((document: any) => document.uid === uid))); + + // New data + const newData: any = { + document_types: document_types + }; + + console.log('[DeedTypesInformations] newData', newData); + + await DeedTypeService.updateDeedType(processId, newData); + + LoaderService.getInstance().hide(); + closeSaveModal(); + } else { + console.warn('[DeedTypesInformations] process not found:', processId); + } }, [closeSaveModal, deedTypeUid, selectedDocuments]); const onDocumentChangeHandler = useCallback((options: IOption[] | null) => {