From 9a40ada10d665a707b8900c3b2a9f23762d8dcd3 Mon Sep 17 00:00:00 2001 From: Chen-I Lim Date: Fri, 6 Nov 2020 11:59:40 -0800 Subject: [PATCH] Handle delete boards correctly --- webapp/src/pages/boardPage.tsx | 2 +- webapp/src/viewModel/workspaceTree.ts | 33 +++++++++------------------ 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/webapp/src/pages/boardPage.tsx b/webapp/src/pages/boardPage.tsx index a74ae455a..c4801332b 100644 --- a/webapp/src/pages/boardPage.tsx +++ b/webapp/src/pages/boardPage.tsx @@ -192,7 +192,7 @@ export default class BoardPage extends React.Component { const newWorkspaceTree = workspaceTree.mutableCopy() newWorkspaceTree.incrementalUpdate(blocks) - const newBoardTree = boardTree.mutableCopy() + const newBoardTree = boardTree ? boardTree.mutableCopy() : new MutableBoardTree(this.state.boardId) newBoardTree.incrementalUpdate(blocks) newBoardTree.setActiveView(viewId) diff --git a/webapp/src/viewModel/workspaceTree.ts b/webapp/src/viewModel/workspaceTree.ts index 65b43c0c9..c348a8393 100644 --- a/webapp/src/viewModel/workspaceTree.ts +++ b/webapp/src/viewModel/workspaceTree.ts @@ -17,39 +17,28 @@ class MutableWorkspaceTree { boards: Board[] = [] views: BoardView[] = [] - private rawBoards: IBlock[] = [] - private rawViews: IBlock[] = [] + private rawBlocks: IBlock[] = [] async sync() { - this.rawBoards = await octoClient.getBlocksWithType('board') - this.rawViews = await octoClient.getBlocksWithType('view') - this.rebuild( - OctoUtils.hydrateBlocks(this.rawBoards), - OctoUtils.hydrateBlocks(this.rawViews), - ) + const rawBoards = await octoClient.getBlocksWithType('board') + const rawViews = await octoClient.getBlocksWithType('view') + this.rawBlocks = [...rawBoards, ...rawViews] + this.rebuild(OctoUtils.hydrateBlocks(this.rawBlocks)) } incrementalUpdate(updatedBlocks: IBlock[]) { - const updatedBoards = updatedBlocks.filter((o) => o.type === 'board') - const updatedViews = updatedBlocks.filter((o) => o.type === 'view') - - this.rawBoards = OctoUtils.mergeBlocks(this.rawBoards, updatedBoards) - this.rawViews = OctoUtils.mergeBlocks(this.rawViews, updatedViews) - this.rebuild( - OctoUtils.hydrateBlocks(this.rawBoards), - OctoUtils.hydrateBlocks(this.rawViews), - ) + this.rawBlocks = OctoUtils.mergeBlocks(this.rawBlocks, updatedBlocks) + this.rebuild(OctoUtils.hydrateBlocks(this.rawBlocks)) } - private rebuild(boards: IBlock[], views: IBlock[]) { - this.boards = boards.filter((block) => block.type === 'board') as Board[] - this.views = views.filter((block) => block.type === 'view') as BoardView[] + private rebuild(blocks: IBlock[]) { + this.boards = blocks.filter((block) => block.type === 'board') as Board[] + this.views = blocks.filter((block) => block.type === 'view') as BoardView[] } mutableCopy(): MutableWorkspaceTree { const workspaceTree = new MutableWorkspaceTree() - const rawBlocks = [...this.rawBoards, ...this.rawViews] - workspaceTree.incrementalUpdate(rawBlocks) + workspaceTree.incrementalUpdate(this.rawBlocks) return workspaceTree } }