diff --git a/app/components/Sidebar/components/CollectionLink.js b/app/components/Sidebar/components/CollectionLink.js index 7381a6e85d..d3ad11bc58 100644 --- a/app/components/Sidebar/components/CollectionLink.js +++ b/app/components/Sidebar/components/CollectionLink.js @@ -56,7 +56,7 @@ class CollectionLink extends React.Component { } documents.move( - document, + documents.get(document.id), collection.id, undefined, result.destination.index diff --git a/app/components/Sidebar/components/DocumentLink.js b/app/components/Sidebar/components/DocumentLink.js index 3149e73c45..2f4fbadf33 100644 --- a/app/components/Sidebar/components/DocumentLink.js +++ b/app/components/Sidebar/components/DocumentLink.js @@ -88,7 +88,12 @@ class DocumentLink extends React.Component { return; } - documents.move(document, collection.id, node.id, result.destination.index); + documents.move( + documents.get(document.id), + collection.id, + node.id, + result.destination.index + ); }; render() { diff --git a/app/models/Collection.js b/app/models/Collection.js index c69d7f1211..e0c154a0a3 100644 --- a/app/models/Collection.js +++ b/app/models/Collection.js @@ -100,6 +100,58 @@ export default class Collection extends BaseModel { return []; } + @action + addDocumentToStructure( + document: NavigationNode, + parentDocumentId: ?string, + index: ?number + ) { + if (!parentDocumentId) { + this.documents.splice( + index !== undefined ? index : this.documents.length, + 0, + document + ); + } else { + const recursivelyAddDocument = nodes => { + for (let i = 0; i < nodes.length; i++) { + if (nodes[i].id === parentDocumentId) { + nodes[i].children.splice( + index !== undefined ? index : nodes[i].children.length, + 0, + document + ); + return true; + } + + const isAdded = recursivelyAddDocument(nodes[i].children); + if (isAdded) return true; + } + }; + + recursivelyAddDocument(this.documents); + } + } + + @action + removeDocumentInStructure(documentId: string) { + const recursivelyRemoveDocument = nodes => { + const index = nodes.findIndex(item => item.id === documentId); + if (index !== -1) { + nodes.splice(index, 1); + return true; + } + + for (let i = 0; i < nodes.length; i++) { + const isFound = recursivelyRemoveDocument(nodes[i].children); + if (isFound) return true; + } + return false; + }; + + return recursivelyRemoveDocument(this.documents); + } + toJS = () => { return pick(this, ['id', 'name', 'color', 'description', 'private']); }; diff --git a/app/stores/DocumentsStore.js b/app/stores/DocumentsStore.js index 6b9cf69755..2b0c05bebd 100644 --- a/app/stores/DocumentsStore.js +++ b/app/stores/DocumentsStore.js @@ -18,7 +18,12 @@ import BaseStore from 'stores/BaseStore'; import RootStore from 'stores/RootStore'; import Document from 'models/Document'; import Revision from 'models/Revision'; -import type { FetchOptions, PaginationParams, SearchResult } from 'types'; +import type { + FetchOptions, + PaginationParams, + SearchResult, + NavigationNode, +} from 'types'; export default class DocumentsStore extends BaseStore { @observable recentlyViewedIds: string[] = []; @@ -361,6 +366,25 @@ export default class DocumentsStore extends BaseStore { parentDocumentId: ?string, 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); + + // Recreate navigation node object + const navigationNode: NavigationNode = { + id: document.id, + title: document.title, + url: document.url, + children: childDocuments, + }; + + const collection = this.rootStore.collections.get(collectionId); + // Move document to new location on the client side + collection.addDocumentToStructure(navigationNode, parentDocumentId, index); + + // Send data to server const res = await client.post('/documents.move', { id: document.id, collectionId, @@ -369,6 +393,7 @@ export default class DocumentsStore extends BaseStore { }); invariant(res && res.data, 'Data not available'); + // Apply data from the server res.data.documents.forEach(this.add); res.data.collections.forEach(this.rootStore.collections.add); };