diff --git a/frontend/scenes/Document/components/DocumentMove/DocumentMove.js b/frontend/scenes/Document/components/DocumentMove/DocumentMove.js index 1f7ebfbf..a6c39a65 100644 --- a/frontend/scenes/Document/components/DocumentMove/DocumentMove.js +++ b/frontend/scenes/Document/components/DocumentMove/DocumentMove.js @@ -90,6 +90,26 @@ type Props = { render() { const { document, documents } = this.props; + let resultSet; + + resultSet = this.resultIds.filter(docId => { + const resultDoc = documents.getById(docId); + + if (document && resultDoc) { + return ( + // Exclude the document if it's on the path to a potential new path + !resultDoc.pathToDocument.map(doc => doc.id).includes(document.id) && + // Exclude if the same path, e.g the last one before the current + _.last(resultDoc.pathToDocument).id !== document.parentDocumentId + ); + } + return true; + }); + + // Prepend root if document does have a parent document + resultSet = document.parentDocumentId + ? _.concat(null, resultSet) + : this.resultIds; return ( @@ -115,18 +135,13 @@ type Props = { mode={ArrowKeyNavigation.mode.VERTICAL} defaultActiveChildIndex={0} > - this.setFirstDocumentRef(ref)} - onSuccess={this.handleClose} - /> - {this.resultIds.map((documentId, index) => ( + {resultSet.map((documentId, index) => ( index === 0 && this.setFirstDocumentRef(ref)} onSuccess={this.handleClose} /> ))} diff --git a/frontend/scenes/Document/components/DocumentMove/components/PathToDocument.js b/frontend/scenes/Document/components/DocumentMove/components/PathToDocument.js index df2f1697..12cfdaa5 100644 --- a/frontend/scenes/Document/components/DocumentMove/components/PathToDocument.js +++ b/frontend/scenes/Document/components/DocumentMove/components/PathToDocument.js @@ -74,16 +74,17 @@ type Props = { }; render() { - const { document, onSuccess, ref } = this.props; + const { document, documentId, onSuccess, ref } = this.props; // $FlowIssue we'll always have a document - const { collection } = document || this.resultDocument; + const { collection } = documentId ? this.resultDocument : document; const Component = onSuccess ? ResultWrapperLink : ResultWrapper; + // Exclude document when it's part of the path and not the preview return ( {collection.name} diff --git a/frontend/stores/CollectionsStore.js b/frontend/stores/CollectionsStore.js index d90c0985..d1db0f31 100644 --- a/frontend/stores/CollectionsStore.js +++ b/frontend/stores/CollectionsStore.js @@ -5,7 +5,6 @@ import { action, runInAction, ObservableArray, - autorunAsync, } from 'mobx'; import ApiClient, { client } from 'utils/ApiClient'; import _ from 'lodash'; @@ -17,8 +16,6 @@ import ErrorsStore from 'stores/ErrorsStore'; import CacheStore from 'stores/CacheStore'; import UiStore from 'stores/UiStore'; -const COLLECTION_CACHE_KEY = 'COLLECTION_CACHE_KEY'; - type Options = { teamId: string, cache: CacheStore, @@ -41,6 +38,30 @@ class CollectionsStore { : undefined; } + /** + * List of paths to each of the documents, where paths are composed of id and title/name pairs + */ + @computed get pathsToDocuments(): ?[[{ id: string, title: string }]] { + let results = []; + const travelDocuments = (documentList, path) => + documentList.forEach(document => { + const { id, title } = document; + const node = { id, title }; + results.push(_.concat(path, node)); + travelDocuments(document.children, _.concat(path, [node])); + }); + + if (this.isLoaded) { + this.data.forEach(collection => { + const { id, name } = collection; + const node = { id, title: name }; + travelDocuments(collection.documents, [node]); + }); + } + + return results; + } + /* Actions */ @action fetchAll = async (): Promise<*> => { @@ -99,21 +120,6 @@ class CollectionsStore { this.teamId = options.teamId; this.cache = options.cache; this.ui = options.ui; - // - // this.cache.getItem(COLLECTION_CACHE_KEY).then(data => { - // if (data) { - // this.data.replace(data.map(collection => new Collection(collection))); - // this.isLoaded = true; - // } - // }); - - autorunAsync('CollectionsStore.persists', () => { - if (this.data.length > 0) - this.cache.setItem( - COLLECTION_CACHE_KEY, - this.data.map(collection => collection.data) - ); - }); } }