From fb77d99177851e7dc2a71e0b8f516daa3da29e36 Mon Sep 17 00:00:00 2001 From: sabhas Date: Wed, 16 Mar 2022 18:52:00 +0000 Subject: [PATCH] fix(web-drive): upon delete remove entry of deleted file from directory tree in sidebar --- web/src/containers/Drive/index.tsx | 38 +++++++++++++++++++++++++++++- web/src/containers/Drive/main.tsx | 8 ++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/web/src/containers/Drive/index.tsx b/web/src/containers/Drive/index.tsx index 9ec1119..8382831 100644 --- a/web/src/containers/Drive/index.tsx +++ b/web/src/containers/Drive/index.tsx @@ -54,11 +54,47 @@ const Drive = () => { setSelectedFilePath(node.relativePath) } + const removeFileFromTree = (path: string) => { + if (directoryData) { + const newTree = JSON.parse(JSON.stringify(directoryData)) as TreeNode + findAndRemoveNode(newTree, newTree, path) + setDirectoryData(newTree) + } + } + + const findAndRemoveNode = ( + node: TreeNode, + parentNode: TreeNode, + path: string + ) => { + if (node.relativePath === path) { + removeNodeFromParent(parentNode, path) + return true + } + if (Array.isArray(node.children)) { + for (let i = 0; i < node.children.length; i++) { + if (findAndRemoveNode(node.children[i], node, path)) return + } + } + } + + const removeNodeFromParent = (parent: TreeNode, path: string) => { + const index = parent.children.findIndex( + (node) => node.relativePath === path + ) + if (index !== -1) { + parent.children.splice(index, 1) + } + } + return ( -
+
) } diff --git a/web/src/containers/Drive/main.tsx b/web/src/containers/Drive/main.tsx index fa083a4..0a92e2f 100644 --- a/web/src/containers/Drive/main.tsx +++ b/web/src/containers/Drive/main.tsx @@ -11,7 +11,12 @@ import Button from '@mui/material/Button' import Toolbar from '@mui/material/Toolbar' import CircularProgress from '@mui/material/CircularProgress' -const Main = (props: any) => { +type Props = { + selectedFilePath: string + removeFileFromTree: (path: string) => void +} + +const Main = (props: Props) => { const baseUrl = window.location.origin const [isLoading, setIsLoading] = useState(false) @@ -45,6 +50,7 @@ const Main = (props: any) => { .delete(`/SASjsApi/drive/file?_filePath=${filePath}`) .then((res) => { setFileContent('') + props.removeFileFromTree(filePath) window.history.pushState('', '', `${baseUrl}/#/SASjsDrive`) }) .catch((err) => {