From d06ec5ce0cb44230eb025f5ffd2d059e91487622 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 26 Feb 2020 21:10:10 -0800 Subject: [PATCH] fix: Nested document menu item appears where it shouldnt (#1193) --- app/menus/DocumentMenu.js | 2 +- app/scenes/Document/components/Document.js | 16 ++++++++++----- app/scenes/Document/components/Header.js | 2 +- server/policies/document.js | 24 ++++++++++++++++++---- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/app/menus/DocumentMenu.js b/app/menus/DocumentMenu.js index edfd38b13..6b9bc5ccb 100644 --- a/app/menus/DocumentMenu.js +++ b/app/menus/DocumentMenu.js @@ -185,7 +185,7 @@ class DocumentMenu extends React.Component { )} - {can.update && ( + {can.createChildDocument && ( { - getEditorText: () => string; + getEditorText: () => string = () => this.props.document.text; @observable editorComponent = EditorImport; @observable isUploading: boolean = false; @@ -80,6 +80,10 @@ class DocumentScene extends React.Component { this.loadEditor(); } + componentDidMount() { + this.updateIsDirty(); + } + @keydown('m') goToMove(ev) { ev.preventDefault(); @@ -179,14 +183,16 @@ class DocumentScene extends React.Component { this.onSave({ done: false, autosave: true }); }, AUTOSAVE_DELAY); - updateIsDirty = debounce(() => { + updateIsDirty = () => { const { document } = this.props; const editorText = this.getEditorText().trim(); // a single hash is a doc with just an empty title - this.isEmpty = editorText === '#'; + this.isEmpty = !editorText || editorText === '#'; this.isDirty = !!document && editorText !== document.text.trim(); - }, IS_DIRTY_DELAY); + }; + + updateIsDirtyDebounced = debounce(this.updateIsDirty, IS_DIRTY_DELAY); onImageUploadStart = () => { this.isUploading = true; @@ -198,7 +204,7 @@ class DocumentScene extends React.Component { onChange = getEditorText => { this.getEditorText = getEditorText; - this.updateIsDirty(); + this.updateIsDirtyDebounced(); this.autosave(); }; diff --git a/app/scenes/Document/components/Header.js b/app/scenes/Document/components/Header.js index e4a85d4f6..78b5f9004 100644 --- a/app/scenes/Document/components/Header.js +++ b/app/scenes/Document/components/Header.js @@ -230,7 +230,7 @@ class Header extends React.Component { )} {canEdit && - !isDraft && ( + can.createChildDocument && ( { }); allow(User, 'update', Document, (user, document) => { + if (document.archivedAt) return false; + if (document.deletedAt) return false; + invariant( document.collection, 'collection is missing, did you forget to include in the query scope?' ); if (cannot(user, 'update', document.collection)) return false; + + return user.teamId === document.teamId; +}); + +allow(User, 'createChildDocument', Document, (user, document) => { if (document.archivedAt) return false; - if (document.deletedAt) return false; + if (document.archivedAt) return false; + if (!document.publishedAt) return false; + + invariant( + document.collection, + 'collection is missing, did you forget to include in the query scope?' + ); + if (cannot(user, 'update', document.collection)) return false; return user.teamId === document.teamId; }); allow(User, ['move', 'pin', 'unpin'], Document, (user, document) => { + if (document.archivedAt) return false; + if (document.deletedAt) return false; + if (!document.publishedAt) return false; + invariant( document.collection, 'collection is missing, did you forget to include in the query scope?' ); if (cannot(user, 'update', document.collection)) return false; - if (document.archivedAt) return false; - if (document.deletedAt) return false; - if (!document.publishedAt) return false; return user.teamId === document.teamId; });