Merge master

This commit is contained in:
Tom Moor
2017-09-13 19:18:49 -07:00
27 changed files with 581 additions and 144 deletions

View File

@ -99,7 +99,7 @@ class Collection extends BaseModel {
return false;
};
updateData(data: Object = {}) {
@action updateData(data: Object = {}) {
this.data = data;
extendObservable(this, data);
}
@ -113,6 +113,17 @@ class Collection extends BaseModel {
this.on('documents.delete', (data: { collectionId: string }) => {
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();
}
);
this.on('documents.move', (data: { collectionId: string }) => {
if (data.collectionId === this.id) this.fetch();
});
}
}

View File

@ -47,11 +47,17 @@ class Document extends BaseModel {
return !!this.lastViewedAt && this.lastViewedAt < this.updatedAt;
}
@computed get pathToDocument(): Array<string> {
@computed get pathToDocument(): Array<{ id: string, title: string }> {
let path;
const traveler = (nodes, previousPath) => {
nodes.forEach(childNode => {
const newPath = [...previousPath, childNode.id];
const newPath = [
...previousPath,
{
id: childNode.id,
title: childNode.title,
},
];
if (childNode.id === this.id) {
path = newPath;
return;
@ -170,6 +176,11 @@ class Document extends BaseModel {
this.updateData(res.data);
this.hasPendingChanges = false;
});
this.emit('collections.update', {
id: this.collection.id,
collection: this.collection,
});
} catch (e) {
this.errors.add('Document failed saving');
} finally {
@ -179,6 +190,24 @@ class Document extends BaseModel {
return this;
};
@action move = async (parentDocumentId: ?string) => {
try {
const res = await client.post('/documents.move', {
id: this.id,
parentDocument: parentDocumentId,
});
invariant(res && res.data, 'Data not available');
this.updateData(res.data);
this.emit('documents.move', {
id: this.id,
collectionId: this.collection.id,
});
} catch (e) {
this.errors.add('Error while moving the document');
}
return;
};
@action delete = async () => {
try {
await client.post('/documents.delete', { id: this.id });