From 10accef83f3a4655dab181e260080b060304e1f2 Mon Sep 17 00:00:00 2001 From: Akuoko Daniel Jnr Date: Wed, 21 Apr 2021 19:15:39 +0000 Subject: [PATCH] fix: update viewer func --- components/core/Application.js | 39 +++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/components/core/Application.js b/components/core/Application.js index a1a9ffaaa..b6e6410cc 100644 --- a/components/core/Application.js +++ b/components/core/Application.js @@ -221,29 +221,38 @@ export default class ApplicationPage extends React.Component { viewer: { ...this.state.viewer, ...newViewerState, type: "VIEWER" }, data, }); + return; } } // NOTE(daniel): update optimistic files with upload data - let oldViewerState = this.state.viewer; - let oldLibrary = oldViewerState.library; + if (newViewerState.library?.length) { + let oldViewerState = this.state.viewer; + let oldLibrary = oldViewerState.library; + + let update = newViewerState.library[0].children.map((child) => { + let optimisticFileIndex = oldLibrary[0].children.findIndex( + (item) => item.id === child.id && item.decorator.startsWith("OPTMISTIC") + ); + if (optimisticFileIndex > -1) { + return { ...oldLibrary[0].children[optimisticFileIndex], ...child }; + } - let update = newViewerState.library[0].children.map((child) => { - let optimisticFileIndex = oldLibrary[0].children.findIndex( - (item) => item.id === child.id && item.decorator.startsWith("OPTMISTIC") - ); - if (optimisticFileIndex > -1) { - return { ...oldLibrary[0].children[optimisticFileIndex], ...child }; - } + return child; + }); - return child; - }); + oldViewerState.library[0].children = update; + + this.setState({ + viewer: { ...oldViewerState, ...newViewerState, type: "VIEWER" }, + }); - oldViewerState.library[0].children = update; + return; + } this.setState({ - viewer: { ...oldViewerState, ...newViewerState, type: "VIEWER" }, + viewer: { ...this.state.viewer, ...newViewerState, type: "VIEWER" }, }); }; @@ -356,7 +365,7 @@ export default class ApplicationPage extends React.Component { return null; } - files = await this._handleOptimisticUpload({ files }); + files = await this._handleOptimisticUpload({ files, slate }); const resolvedFiles = []; for (let i = 0; i < files.length; i++) { @@ -464,7 +473,7 @@ export default class ApplicationPage extends React.Component { this.setState({ optimisticFiles }); let update = [...optimisticFiles, ...this.state.viewer?.library[0].children]; - let library = this.props.viewer.library; + let library = this.props.viewer?.library; library[0].children = update; this._handleUpdateViewer({ library });