From 458d9b5d9911685e7ca3e56dcb6c659bef23efc1 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 4 Jul 2018 13:00:53 -0700 Subject: [PATCH] Fixes: Issue saving after draft --- app/models/Document.js | 42 ++++++++++++++++++++---------------- app/stores/DocumentsStore.js | 4 ++-- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/app/models/Document.js b/app/models/Document.js index 65b5c953f..9b240c0bd 100644 --- a/app/models/Document.js +++ b/app/models/Document.js @@ -175,19 +175,12 @@ class Document extends BaseModel { if (this.isSaving) return this; const wasDraft = !this.publishedAt; + const isCreating = !this.id; this.isSaving = true; try { let res; - if (this.id) { - res = await client.post('/documents.update', { - id: this.id, - title: this.title, - text: this.text, - lastRevision: this.revision, - ...options, - }); - } else { + if (isCreating) { const data = { parentDocument: undefined, collection: this.collection.id, @@ -199,25 +192,36 @@ class Document extends BaseModel { data.parentDocument = this.parentDocument; } res = await client.post('/documents.create', data); - if (res && res.data) this.emit('documents.create', res.data); + } else { + res = await client.post('/documents.update', { + id: this.id, + title: this.title, + text: this.text, + lastRevision: this.revision, + ...options, + }); } runInAction('Document#save', () => { invariant(res && res.data, 'Data should be available'); this.updateData(res.data); this.hasPendingChanges = false; - }); - this.emit('documents.update', { - document: this, - collectionId: this.collection.id, - }); + if (isCreating) { + this.emit('documents.create', this); + } - if (wasDraft && this.publishedAt) { - this.emit('documents.publish', { - id: this.id, + this.emit('documents.update', { + document: this, collectionId: this.collection.id, }); - } + + if (wasDraft && this.publishedAt) { + this.emit('documents.publish', { + id: this.id, + collectionId: this.collection.id, + }); + } + }); } catch (e) { this.ui.showToast('Document failed to save'); } finally { diff --git a/app/stores/DocumentsStore.js b/app/stores/DocumentsStore.js index ca91c62c5..0a33a1c3d 100644 --- a/app/stores/DocumentsStore.js +++ b/app/stores/DocumentsStore.js @@ -217,7 +217,7 @@ class DocumentsStore extends BaseStore { if (res && res.data) { const duped = res.data; - this.emit('documents.create', duped); + this.emit('documents.create', new Document(duped)); this.emit('documents.publish', { id: duped.id, collectionId: duped.collection.id, @@ -255,7 +255,7 @@ class DocumentsStore extends BaseStore { this.remove(data.id); }); this.on('documents.create', (data: Document) => { - this.add(new Document(data)); + this.add(data); }); this.on('documents.duplicate', (data: Document) => { this.duplicate(data);