Update docs

Remove extra API request after document.update
Cleanup
This commit is contained in:
Tom Moor
2018-05-07 22:08:47 -07:00
parent 67f2b3cce4
commit ba0a7b7f4a
7 changed files with 56 additions and 20 deletions

View File

@@ -3,6 +3,7 @@ import { extendObservable, action, computed, runInAction } from 'mobx';
import invariant from 'invariant';
import BaseModel from 'models/BaseModel';
import Document from 'models/Document';
import { client } from 'utils/ApiClient';
import stores from 'stores';
import ErrorsStore from 'stores/ErrorsStore';
@@ -20,7 +21,7 @@ class Collection extends BaseModel {
name: string;
color: string;
type: 'atlas' | 'journal';
documents: Array<NavigationNode>;
documents: NavigationNode[];
updatedAt: string;
url: string;
@@ -51,6 +52,21 @@ class Collection extends BaseModel {
return results;
}
@action
updateDocument(document: Document) {
const travelDocuments = (documentList, path) =>
documentList.forEach(d => {
if (d.id === document.id) {
d.title = document.title;
d.url = document.url;
} else {
travelDocuments(d.children);
}
});
travelDocuments(this.documents);
}
/* Actions */
@action
@@ -123,7 +139,7 @@ class Collection extends BaseModel {
extendObservable(this, data);
}
constructor(collection: Object = {}) {
constructor(collection: $Shape<Collection>) {
super();
this.updateData(collection);
@@ -133,11 +149,11 @@ class Collection extends BaseModel {
if (data.collectionId === this.id) this.fetch();
});
this.on(
'collections.update',
(data: { id: string, collection: Collection }) => {
// FIXME: calling this.updateData won't update the
// UI. Some mobx issue
if (data.id === this.id) this.fetch();
'documents.update',
(data: { collectionId: string, document: Document }) => {
if (data.collectionId === this.id) {
this.updateDocument(data.document);
}
}
);
this.on('documents.move', (data: { collectionId: string }) => {

View File

@@ -11,7 +11,7 @@ import type { User } from 'types';
import BaseModel from './BaseModel';
import Collection from './Collection';
type SaveOptions = { publish: boolean, done: boolean, autosave: boolean };
type SaveOptions = { publish?: boolean, done?: boolean, autosave?: boolean };
class Document extends BaseModel {
isSaving: boolean = false;
@@ -204,9 +204,9 @@ class Document extends BaseModel {
this.hasPendingChanges = false;
});
this.emit('collections.update', {
id: this.collection.id,
collection: this.collection,
this.emit('documents.update', {
document: this,
collectionId: this.collection.id,
});
} catch (e) {
this.errors.add('Document failed saving');