From 5ab423108acc4aa38fb179e2fa14773d4bce0a36 Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Sun, 28 Jan 2018 17:04:53 -0800 Subject: [PATCH] Fixed collection deletion --- app/stores/DocumentsStore.js | 37 ++++++++++++++++++++++++++++-------- server/models/Collection.js | 8 ++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/app/stores/DocumentsStore.js b/app/stores/DocumentsStore.js index 82a78f02..bfda9c6e 100644 --- a/app/stores/DocumentsStore.js +++ b/app/stores/DocumentsStore.js @@ -29,6 +29,7 @@ type Options = { class DocumentsStore extends BaseStore { @observable recentlyViewedIds: Array = []; + @observable recentlyEditedIds: Array = []; @observable data: Map = new ObservableMap([]); @observable isLoaded: boolean = false; @observable isFetching: boolean = false; @@ -41,17 +42,22 @@ class DocumentsStore extends BaseStore { @computed get recentlyViewed(): Array { - return _.take( - _.filter(this.data.values(), ({ id }) => - this.recentlyViewedIds.includes(id) - ), - 5 - ); + const docs = []; + this.recentlyViewedIds.forEach(id => { + const doc = this.getById(id); + if (doc) docs.push(doc); + }); + return docs; } @computed get recentlyEdited(): Document[] { - return _.take(_.orderBy(this.data.values(), 'updatedAt', 'desc'), 5); + const docs = []; + this.recentlyEditedIds.forEach(id => { + const doc = this.getById(id); + if (doc) docs.push(doc); + }); + return docs; } recentlyEditedIn(documentIds: string[]): Document[] { @@ -105,7 +111,12 @@ class DocumentsStore extends BaseStore { @action fetchRecentlyModified = async (options: ?PaginationParams): Promise<*> => { - return await this.fetchAll('list', options); + const data = await this.fetchAll('list', options); + + runInAction('DocumentsStore#fetchRecentlyModified', () => { + this.recentlyEditedIds = _.map(data, 'id'); + }); + return data; }; @action @@ -207,6 +218,16 @@ class DocumentsStore extends BaseStore { this.add(new Document(data)); }); + // Re-fetch dashboard content so that we don't show deleted documents + this.on('collections.delete', () => { + this.fetchRecentlyModified(); + this.fetchRecentlyViewed(); + }); + this.on('documents.delete', () => { + this.fetchRecentlyModified(); + this.fetchRecentlyViewed(); + }); + autorunAsync('DocumentsStore.persists', () => { if (this.data.size) { this.cache.setItem( diff --git a/server/models/Collection.js b/server/models/Collection.js index fa3d18d9..2b32e91e 100644 --- a/server/models/Collection.js +++ b/server/models/Collection.js @@ -95,6 +95,14 @@ Collection.associate = models => { }); }; +Collection.addHook('afterDestroy', async model => { + await Document.destroy({ + where: { + atlasId: model.id, + }, + }); +}); + // Hooks Collection.addHook('afterCreate', model =>