From 917c5c4923ee52d478caa31efddd1b7b06a96968 Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Tue, 12 Sep 2017 00:10:02 -0700 Subject: [PATCH 1/5] Return updated collection on document create --- server/api/documents.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/api/documents.js b/server/api/documents.js index 178e590aa..3eb5efb83 100644 --- a/server/api/documents.js +++ b/server/api/documents.js @@ -211,6 +211,8 @@ router.post('documents.create', auth(), async ctx => { await ownerCollection.addDocumentToStructure(document, index); } + document.collection = ownerCollection; + ctx.body = { data: await presentDocument(ctx, document), }; From ce5b6e6ac86bb654a8c9ebf4504ea717e0d947a5 Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Tue, 12 Sep 2017 00:11:53 -0700 Subject: [PATCH 2/5] Return updated collection on document move --- server/api/documents.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/api/documents.js b/server/api/documents.js index 3eb5efb83..a179f9830 100644 --- a/server/api/documents.js +++ b/server/api/documents.js @@ -282,6 +282,8 @@ router.post('documents.move', auth(), async ctx => { await collection.addDocumentToStructure(document, index); } + document.collection = collection; + ctx.body = { data: await presentDocument(ctx, document), }; From e40d9cebdaf95d9b2d9c3093efeb108d91db4bf6 Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Tue, 12 Sep 2017 19:54:22 -0700 Subject: [PATCH 3/5] Trigger collection update when document gets updated (renamed) --- frontend/models/Collection.js | 10 +++++++++- frontend/models/Document.js | 5 +++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/frontend/models/Collection.js b/frontend/models/Collection.js index 13d8e5e8b..80d0ba393 100644 --- a/frontend/models/Collection.js +++ b/frontend/models/Collection.js @@ -93,7 +93,7 @@ class Collection extends BaseModel { } }; - updateData(data: Object = {}) { + @action updateData(data: Object = {}) { this.data = data; extendObservable(this, data); } @@ -107,6 +107,14 @@ 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(); + } + ); } } diff --git a/frontend/models/Document.js b/frontend/models/Document.js index ea39c2f62..eade37798 100644 --- a/frontend/models/Document.js +++ b/frontend/models/Document.js @@ -160,6 +160,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 { From 5c43e1221837d3b11c88a15b01d5a7cd7ea6f700 Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Tue, 12 Sep 2017 19:55:01 -0700 Subject: [PATCH 4/5] Moved document matching to use urlId to prevent issues with renamed documents --- frontend/scenes/Document/Document.js | 4 ++++ frontend/stores/DocumentsStore.js | 5 ++++- server/presenters/document.js | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/frontend/scenes/Document/Document.js b/frontend/scenes/Document/Document.js index 4bc09784a..b7f80090a 100644 --- a/frontend/scenes/Document/Document.js +++ b/frontend/scenes/Document/Document.js @@ -90,6 +90,10 @@ type Props = { if (document) { this.props.ui.setActiveDocument(document); document.view(); + + // Update url to match the current one + const urlParts = this.props.match.url.split('/'); + this.props.history.replace([document.url, urlParts.slice(3)].join('/')); } else { // Render 404 with search this.setState({ notFound: true }); diff --git a/frontend/stores/DocumentsStore.js b/frontend/stores/DocumentsStore.js index 7ccedcad5..cfbb70f99 100644 --- a/frontend/stores/DocumentsStore.js +++ b/frontend/stores/DocumentsStore.js @@ -138,8 +138,11 @@ class DocumentsStore extends BaseStore { return this.data.get(id); }; + /** + * Match documents by the url ID as the title slug can change + */ getByUrl = (url: string): ?Document => { - return _.find(this.data.values(), { url }); + return _.find(this.data.values(), doc => url.endsWith(doc.urlId)); }; constructor(options: Options) { diff --git a/server/presenters/document.js b/server/presenters/document.js index 0ca0ea186..5927a83d9 100644 --- a/server/presenters/document.js +++ b/server/presenters/document.js @@ -17,6 +17,7 @@ async function present(ctx: Object, document: Document, options: ?Options) { const data = { id: document.id, url: document.getUrl(), + urlId: document.urlId, private: document.private, title: document.title, text: document.text, From 76afaacc6eddc944268399caca0b814836b065bf Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Tue, 12 Sep 2017 20:14:14 -0700 Subject: [PATCH 5/5] Moved url updating to a helper function --- frontend/scenes/Document/Document.js | 7 ++++--- frontend/utils/routeHelpers.js | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/frontend/scenes/Document/Document.js b/frontend/scenes/Document/Document.js index b7f80090a..b64c0023f 100644 --- a/frontend/scenes/Document/Document.js +++ b/frontend/scenes/Document/Document.js @@ -6,7 +6,7 @@ import { observer, inject } from 'mobx-react'; import { withRouter, Prompt } from 'react-router'; import Flex from 'components/Flex'; import { color, layout } from 'styles/constants'; -import { collectionUrl } from 'utils/routeHelpers'; +import { collectionUrl, updateDocumentUrl } from 'utils/routeHelpers'; import Document from 'models/Document'; import UiStore from 'stores/UiStore'; @@ -92,8 +92,9 @@ type Props = { document.view(); // Update url to match the current one - const urlParts = this.props.match.url.split('/'); - this.props.history.replace([document.url, urlParts.slice(3)].join('/')); + this.props.history.replace( + updateDocumentUrl(this.props.match.url, document.url) + ); } else { // Render 404 with search this.setState({ notFound: true }); diff --git a/frontend/utils/routeHelpers.js b/frontend/utils/routeHelpers.js index ce11b20f3..2a273bd51 100644 --- a/frontend/utils/routeHelpers.js +++ b/frontend/utils/routeHelpers.js @@ -38,3 +38,13 @@ export function searchUrl(query?: string): string { export function notFoundUrl(): string { return '/404'; } + +/** + * Replace full url's document part with the new one in case + * the document slug has been updated + */ +export function updateDocumentUrl(oldUrl: string, newUrl: string): string { + // Update url to match the current one + const urlParts = oldUrl.split('/'); + return [newUrl, urlParts.slice(3)].join('/'); +}