diff --git a/app/components/SocketProvider.js b/app/components/SocketProvider.js index dc07fcef..d34a1877 100644 --- a/app/components/SocketProvider.js +++ b/app/components/SocketProvider.js @@ -41,7 +41,11 @@ class SocketProvider extends React.Component { this.socket.on('entities', event => { if (event.documents) { event.documents.forEach(doc => { - documents.add(doc); + if (doc.deletedAt) { + documents.remove(doc.id); + } else { + documents.add(doc); + } // TODO: Move this to the document scene once data loading // has been refactored to be friendlier there. @@ -61,7 +65,14 @@ class SocketProvider extends React.Component { }); } if (event.collections) { - event.collections.forEach(collections.add); + event.collections.forEach(collection => { + if (collection.deletedAt) { + collections.remove(collection.id); + documents.removeCollectionDocuments(collection.id); + } else { + collections.add(collection); + } + }); } }); this.socket.on('documents.star', event => { diff --git a/app/stores/CollectionsStore.js b/app/stores/CollectionsStore.js index 854eb664..fa518f1e 100644 --- a/app/stores/CollectionsStore.js +++ b/app/stores/CollectionsStore.js @@ -1,5 +1,5 @@ // @flow -import { computed, runInAction } from 'mobx'; +import { computed } from 'mobx'; import { concat, filter, last } from 'lodash'; import { client } from 'utils/ApiClient'; @@ -100,10 +100,9 @@ export default class CollectionsStore extends BaseStore { delete(collection: Collection) { super.delete(collection); - runInAction(() => { - this.rootStore.documents.fetchRecentlyUpdated(); - this.rootStore.documents.fetchRecentlyViewed(); - }); + this.rootStore.documents.removeCollectionDocuments(collection.id); + this.rootStore.documents.fetchRecentlyUpdated(); + this.rootStore.documents.fetchRecentlyViewed(); } export = () => { diff --git a/app/stores/DocumentsStore.js b/app/stores/DocumentsStore.js index 8c09c4ac..76c3fe29 100644 --- a/app/stores/DocumentsStore.js +++ b/app/stores/DocumentsStore.js @@ -16,8 +16,8 @@ import invariant from 'invariant'; import BaseStore from 'stores/BaseStore'; import RootStore from 'stores/RootStore'; -import Document from '../models/Document'; -import Revision from '../models/Revision'; +import Document from 'models/Document'; +import Revision from 'models/Revision'; import type { FetchOptions, PaginationParams, SearchResult } from 'types'; export default class DocumentsStore extends BaseStore { @@ -56,6 +56,10 @@ export default class DocumentsStore extends BaseStore { ); } + inCollection(collectionId: string): Document[] { + return filter(this.all, document => document.collectionId === collectionId); + } + pinnedInCollection(collectionId: string): Document[] { return filter( this.recentlyUpdatedInCollection(collectionId), @@ -352,6 +356,14 @@ export default class DocumentsStore extends BaseStore { return document; }; + @action + removeCollectionDocuments(collectionId: string) { + const documents = this.inCollection(collectionId); + const documentIds = documents.map(doc => doc.id); + this.recentlyViewedIds = without(this.recentlyViewedIds, ...documentIds); + documentIds.forEach(id => this.data.delete(id)); + } + @action async update(params: *) { const document = await super.update(params);