From 5d2be0a199e4bc792ebae8a396edc653efbf90ca Mon Sep 17 00:00:00 2001 From: Ante Primorac Date: Sun, 24 May 2020 23:12:33 +0200 Subject: [PATCH] fix flow issues introduced in documents reordering feature --- .../Sidebar/components/CollectionLink.js | 8 +--- .../Sidebar/components/DocumentLink.js | 8 +--- app/models/Collection.js | 8 ++-- app/stores/DocumentsStore.js | 40 ++++++++++++------- 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/app/components/Sidebar/components/CollectionLink.js b/app/components/Sidebar/components/CollectionLink.js index d3ad11bc58..7df936cec5 100644 --- a/app/components/Sidebar/components/CollectionLink.js +++ b/app/components/Sidebar/components/CollectionLink.js @@ -3,12 +3,8 @@ import * as React from 'react'; import { observer, Observer } from 'mobx-react'; import { observable } from 'mobx'; import { CollectionIcon, PrivateCollectionIcon } from 'outline-icons'; -import { - DragDropContext, - Droppable, - Draggable, - DropResult, -} from 'react-beautiful-dnd'; +import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd'; +import type { DropResult } from 'react-beautiful-dnd'; import Collection from 'models/Collection'; import Document from 'models/Document'; import CollectionMenu from 'menus/CollectionMenu'; diff --git a/app/components/Sidebar/components/DocumentLink.js b/app/components/Sidebar/components/DocumentLink.js index 2f4fbadf33..dd5478f68d 100644 --- a/app/components/Sidebar/components/DocumentLink.js +++ b/app/components/Sidebar/components/DocumentLink.js @@ -3,12 +3,8 @@ import * as React from 'react'; import { observer, Observer } from 'mobx-react'; import { observable } from 'mobx'; import styled from 'styled-components'; -import { - DragDropContext, - Droppable, - Draggable, - DropResult, -} from 'react-beautiful-dnd'; +import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd'; +import type { DropResult } from 'react-beautiful-dnd'; import Document from 'models/Document'; import DocumentMenu from 'menus/DocumentMenu'; import SidebarLink from './SidebarLink'; diff --git a/app/models/Collection.js b/app/models/Collection.js index e0c154a0a3..ca2196eac2 100644 --- a/app/models/Collection.js +++ b/app/models/Collection.js @@ -108,16 +108,18 @@ export default class Collection extends BaseModel { ) { if (!parentDocumentId) { this.documents.splice( - index !== undefined ? index : this.documents.length, + index !== undefined && index !== null ? index : this.documents.length, 0, document ); } else { - const recursivelyAddDocument = nodes => { + const recursivelyAddDocument = (nodes: NavigationNode[]) => { for (let i = 0; i < nodes.length; i++) { if (nodes[i].id === parentDocumentId) { nodes[i].children.splice( - index !== undefined ? index : nodes[i].children.length, + index !== undefined && index !== null + ? index + : nodes[i].children.length, 0, document ); diff --git a/app/stores/DocumentsStore.js b/app/stores/DocumentsStore.js index 2b0c05bebd..428dd835d4 100644 --- a/app/stores/DocumentsStore.js +++ b/app/stores/DocumentsStore.js @@ -367,22 +367,34 @@ export default class DocumentsStore extends BaseStore { index: ?number ) => { const oldCollection = this.rootStore.collections.get(document.collectionId); - // Retrive all children documents - const childDocuments = oldCollection.getDocumentChildren(document.id); - // Remove document from old collection - oldCollection.removeDocumentInStructure(document.id); + let newCollection = oldCollection; - // Recreate navigation node object - const navigationNode: NavigationNode = { - id: document.id, - title: document.title, - url: document.url, - children: childDocuments, - }; + if (document.collectionId !== collectionId) { + newCollection = this.rootStore.collections.get(collectionId); + } - const collection = this.rootStore.collections.get(collectionId); - // Move document to new location on the client side - collection.addDocumentToStructure(navigationNode, parentDocumentId, index); + // Update UI + if (oldCollection && newCollection) { + // Retrive all children documents + const childDocuments = oldCollection.getDocumentChildren(document.id); + // Remove document from old collection + oldCollection.removeDocumentInStructure(document.id); + + // Recreate navigation node object + const navigationNode: NavigationNode = { + id: document.id, + title: document.title, + url: document.url, + children: childDocuments, + }; + + // Move document to new location + newCollection.addDocumentToStructure( + navigationNode, + parentDocumentId, + index + ); + } // Send data to server const res = await client.post('/documents.move', {