From 3c34c7fecc49f01465cf6cfa88d94738a791ee21 Mon Sep 17 00:00:00 2001 From: hexastack Date: Thu, 24 Oct 2024 08:20:00 +0100 Subject: [PATCH] fix: incomplete blocks removal during delete operation --- .../components/visual-editor/v2/Diagrams.tsx | 50 ++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/frontend/src/components/visual-editor/v2/Diagrams.tsx b/frontend/src/components/visual-editor/v2/Diagrams.tsx index 8e2a478..8412072 100644 --- a/frontend/src/components/visual-editor/v2/Diagrams.tsx +++ b/frontend/src/components/visual-editor/v2/Diagrams.tsx @@ -399,48 +399,52 @@ const Diagrams = () => { } else { // Block Delete Case const ids = id.includes(",") ? id.split(",") : [id]; - const deletePromises = ids.map((id) => { - const block = getBlockFromCache(id); - return deleteBlock(id, { + for (const blockId of ids) { + const block = getBlockFromCache(blockId); + + await deleteBlock(blockId, { onSuccess() { // Update all linked blocks to remove any reference to the deleted block - [ + const linkedBlockIds = [ ...(block?.nextBlocks || []), ...(block?.previousBlocks || []), ...(block?.attachedBlock ? [block.attachedBlock] : []), ...(block?.attachedToBlock ? [block.attachedToBlock] : []), - ] - .map((bid) => getBlockFromCache(bid)) - .filter((b) => !!b) - .forEach((b) => { + ]; + + for (const linkedBlockId of linkedBlockIds) { + const linkedBlock = getBlockFromCache(linkedBlockId); + + if (linkedBlock) { updateCachedBlock({ - id: b.id, + id: linkedBlock.id, payload: { - ...b, - nextBlocks: b.nextBlocks?.filter( - (nextBlockId) => nextBlockId !== id, + ...linkedBlock, + nextBlocks: linkedBlock.nextBlocks?.filter( + (nextBlockId) => nextBlockId !== blockId, ), - previousBlocks: b.previousBlocks?.filter( - (previousBlockId) => previousBlockId !== id, + previousBlocks: linkedBlock.previousBlocks?.filter( + (previousBlockId) => previousBlockId !== blockId, ), attachedBlock: - b.attachedBlock === id ? undefined : b.attachedBlock, - attachedToBlock: - b.attachedToBlock === id + linkedBlock.attachedBlock === blockId ? undefined - : b.attachedToBlock, + : linkedBlock.attachedBlock, + attachedToBlock: + linkedBlock.attachedToBlock === blockId + ? undefined + : linkedBlock.attachedToBlock, }, strategy: "overwrite", }); - }); + } + } - deleteCachedBlock(id); + deleteCachedBlock(blockId); }, }); - }); - - await Promise.all(deletePromises); + } } deleteCallbackRef.current?.();