Update DeedTypeInformations

This commit is contained in:
Sosthene 2025-09-11 14:14:24 +02:00
parent 129e3ea973
commit dfe48d1de9

View File

@ -21,6 +21,7 @@ import classes from "./classes.module.scss";
import DeedTypeService from "src/common/Api/LeCoffreApi/sdk/DeedTypeService"; import DeedTypeService from "src/common/Api/LeCoffreApi/sdk/DeedTypeService";
import DocumentTypeService from "src/common/Api/LeCoffreApi/sdk/DocumentTypeService"; import DocumentTypeService from "src/common/Api/LeCoffreApi/sdk/DocumentTypeService";
import LoaderService from "src/common/Api/LeCoffreApi/sdk/Loader/LoaderService"; import LoaderService from "src/common/Api/LeCoffreApi/sdk/Loader/LoaderService";
import MessageBus from "src/sdk/MessageBus";
type IProps = {}; type IProps = {};
export default function DeedTypesInformations(props: IProps) { export default function DeedTypesInformations(props: IProps) {
@ -52,7 +53,9 @@ export default function DeedTypesInformations(props: IProps) {
const deleteDeedType = useCallback(async () => { const deleteDeedType = useCallback(async () => {
LoaderService.getInstance().show(); 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) { if (process) {
// New data // New data
const newData: any = { const newData: any = {
@ -80,9 +83,12 @@ export default function DeedTypesInformations(props: IProps) {
if (!deedTypeUid) return; if (!deedTypeUid) return;
setSelectedDocuments([]); 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) { if (process) {
const deedType: any = process.processData; console.log('[DeedTypesInformations] process', process);
const deedType: any = process;
setDeedTypeSelected(deedType); setDeedTypeSelected(deedType);
if (!deedType.document_types) return; 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)); .sort((a: any, b: any) => a.label.localeCompare(b.label));
setSelectedDocuments(documentsOptions); setSelectedDocuments(documentsOptions);
} else {
console.warn('[DeedTypesInformations] process not found:', processId);
} }
}); });
} }
async function getDocuments() { function getDocuments() {
setAvailableDocuments([]); setAvailableDocuments([]);
DocumentTypeService.getDocumentTypes().then((processes: any[]) => { DocumentTypeService.getDocumentTypes((processes: Record<string, any>) => {
if (processes.length) { const documents = Object.values(processes);
const documents: any[] = processes.map((process: any) => process.processData); if (documents.length) {
setAvailableDocuments(documents); setAvailableDocuments(documents);
} }
}); });
@ -120,13 +128,16 @@ export default function DeedTypesInformations(props: IProps) {
[openSaveModal], [openSaveModal],
); );
const saveDocumentTypes = useCallback(() => { const saveDocumentTypes = useCallback(async () => {
LoaderService.getInstance().show(); 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
if (process) { const processId = (deedTypeUid as string) + ':0';
const deedType: any = process.processData; 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; let document_types: any[] = deedType['document_types'];
if (!document_types) { if (!document_types) {
document_types = []; document_types = [];
} }
@ -138,14 +149,15 @@ export default function DeedTypesInformations(props: IProps) {
document_types: document_types document_types: document_types
}; };
// Merge process data with new data & update process console.log('[DeedTypesInformations] newData', newData);
process.processData.document_types = newData.document_types;
await DeedTypeService.updateDeedType(process, newData); await DeedTypeService.updateDeedType(processId, newData);
LoaderService.getInstance().hide(); LoaderService.getInstance().hide();
closeSaveModal(); closeSaveModal();
} else {
console.warn('[DeedTypesInformations] process not found:', processId);
} }
});
}, [closeSaveModal, deedTypeUid, selectedDocuments]); }, [closeSaveModal, deedTypeUid, selectedDocuments]);
const onDocumentChangeHandler = useCallback((options: IOption[] | null) => { const onDocumentChangeHandler = useCallback((options: IOption[] | null) => {