diff --git a/app/components/Sidebar/components/SidebarLink.js b/app/components/Sidebar/components/SidebarLink.js index 42958eb53..5e9219bc9 100644 --- a/app/components/Sidebar/components/SidebarLink.js +++ b/app/components/Sidebar/components/SidebarLink.js @@ -2,7 +2,7 @@ import * as React from 'react'; import { observable, action } from 'mobx'; import { observer } from 'mobx-react'; -import { NavLink } from 'react-router-dom'; +import { withRouter, NavLink } from 'react-router-dom'; import { CollapsedIcon } from 'outline-icons'; import { color, fontWeight } from 'shared/styles/constants'; import styled from 'styled-components'; @@ -59,6 +59,7 @@ type Props = { active?: boolean, }; +@withRouter @observer class SidebarLink extends React.Component { @observable expanded: boolean = false; diff --git a/app/models/Collection.js b/app/models/Collection.js index 6184c8e0b..409909d00 100644 --- a/app/models/Collection.js +++ b/app/models/Collection.js @@ -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; + 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) { 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 }) => { diff --git a/app/models/Document.js b/app/models/Document.js index d751423b2..975b64587 100644 --- a/app/models/Document.js +++ b/app/models/Document.js @@ -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'); diff --git a/app/scenes/Document/Document.js b/app/scenes/Document/Document.js index fef5f64b8..3d952d64a 100644 --- a/app/scenes/Document/Document.js +++ b/app/scenes/Document/Document.js @@ -157,7 +157,7 @@ class DocumentScene extends React.Component { handleOpenMoveModal = () => (this.moveModalOpen = true); onSave = async ( - options: { redirect?: boolean, publish?: boolean, autosave?: boolean } = {} + options: { done?: boolean, publish?: boolean, autosave?: boolean } = {} ) => { let document = this.document; if (!document || !document.allowSave) return; @@ -169,7 +169,7 @@ class DocumentScene extends React.Component { this.isSaving = false; this.isPublishing = false; - if (options.redirect) { + if (options.done) { this.props.history.push(document.url); this.props.ui.setActiveDocument(document); } else if (this.props.newDocument) { @@ -179,7 +179,7 @@ class DocumentScene extends React.Component { }; autosave = debounce(async () => { - this.onSave({ redirect: false, autosave: true }); + this.onSave({ done: false, autosave: true }); }, AUTOSAVE_INTERVAL); onImageUploadStart = () => { diff --git a/app/scenes/Document/components/Actions.js b/app/scenes/Document/components/Actions.js index b72fa7488..96768a9af 100644 --- a/app/scenes/Document/components/Actions.js +++ b/app/scenes/Document/components/Actions.js @@ -20,8 +20,9 @@ type Props = { savingIsDisabled: boolean, onDiscard: () => *, onSave: ({ - redirect?: boolean, + done?: boolean, publish?: boolean, + autosave?: boolean, }) => *, history: Object, }; @@ -36,11 +37,11 @@ class DocumentActions extends React.Component { }; handleSave = () => { - this.props.onSave({ redirect: true }); + this.props.onSave({ done: true }); }; handlePublish = () => { - this.props.onSave({ redirect: true, publish: true }); + this.props.onSave({ done: true, publish: true }); }; render() { diff --git a/app/utils/importFile.js b/app/utils/importFile.js index ccb1340cb..17d2d8046 100644 --- a/app/utils/importFile.js +++ b/app/utils/importFile.js @@ -29,7 +29,7 @@ const importFile = async ({ if (documentId) data.parentDocument = documentId; let document = new Document(data); - document = await document.save(true); + document = await document.save({ publish: true }); documents.add(document); resolve(document); }; diff --git a/server/pages/Api.js b/server/pages/Api.js index 11fc571f0..dee26dd06 100644 --- a/server/pages/Api.js +++ b/server/pages/Api.js @@ -425,7 +425,25 @@ export default function Pricing() { id="publish" description={ - Pass true to publish a draft + Pass true to publish a draft. + + } + /> + + Pass true to signify an autosave. This skips + creating a revision. + + } + /> + + Pass true to signify the end of an editing + session. This will trigger documents.update hooks. } />