fix: Remove collections/document when removed elsewhere

This commit is contained in:
Tom Moor 2019-06-26 22:10:24 -07:00
parent 3d7eb11a49
commit ce675a7fe2
3 changed files with 31 additions and 9 deletions

View File

@ -41,7 +41,11 @@ class SocketProvider extends React.Component<Props> {
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<Props> {
});
}
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 => {

View File

@ -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<Collection> {
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 = () => {

View File

@ -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<Document> {
@ -56,6 +56,10 @@ export default class DocumentsStore extends BaseStore<Document> {
);
}
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<Document> {
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);