From 485fc64bccd16bed9f1d6f042cd557b9408e17f5 Mon Sep 17 00:00:00 2001 From: Rebeka Date: Fri, 28 Jul 2023 19:34:03 +0200 Subject: [PATCH] fix: revert the move in the file tree on error GitOrigin-RevId: 580241cd771671ae8cfd93910f2e231e0e1b5148 --- .../contexts/file-tree-actionable.js | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/services/web/frontend/js/features/file-tree/contexts/file-tree-actionable.js b/services/web/frontend/js/features/file-tree/contexts/file-tree-actionable.js index 76552cdb6a..8e98bac22f 100644 --- a/services/web/frontend/js/features/file-tree/contexts/file-tree-actionable.js +++ b/services/web/frontend/js/features/file-tree/contexts/file-tree-actionable.js @@ -231,19 +231,30 @@ export function FileTreeActionableProvider({ children }) { return dispatch({ type: ACTION_TYPES.ERROR, error: validationError }) } + // keep track of old parent folder ids so we can revert entities if sync fails + const oldParentFolderIds = {} + let isMoveFailed = false + // dispatch moves immediately - founds.forEach(found => dispatchMove(found.entity._id, toFolderId)) + founds.forEach(found => { + oldParentFolderIds[found.entity._id] = found.parentFolderId + dispatchMove(found.entity._id, toFolderId) + }) // sync dispatched moves after - return mapSeries(founds, found => - syncMove(projectId, found.type, found.entity._id, toFolderId) - ) - .then(() => { - dispatch({ type: ACTION_TYPES.CLEAR }) - }) - .catch(error => { + return mapSeries(founds, async found => { + try { + await syncMove(projectId, found.type, found.entity._id, toFolderId) + } catch (error) { + isMoveFailed = true + dispatchMove(found.entity._id, oldParentFolderIds[found.entity._id]) dispatch({ type: ACTION_TYPES.ERROR, error }) - }) + } + }).then(() => { + if (!isMoveFailed) { + dispatch({ type: ACTION_TYPES.CLEAR }) + } + }) }, [dispatchMove, fileTreeData, projectId] )