Update docs
Remove extra API request after document.update Cleanup
This commit is contained in:
parent
67f2b3cce4
commit
ba0a7b7f4a
|
@ -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<Props> {
|
||||
@observable expanded: boolean = false;
|
||||
|
|
|
@ -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<NavigationNode>;
|
||||
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<Collection>) {
|
||||
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 }) => {
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -157,7 +157,7 @@ class DocumentScene extends React.Component<Props> {
|
|||
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<Props> {
|
|||
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<Props> {
|
|||
};
|
||||
|
||||
autosave = debounce(async () => {
|
||||
this.onSave({ redirect: false, autosave: true });
|
||||
this.onSave({ done: false, autosave: true });
|
||||
}, AUTOSAVE_INTERVAL);
|
||||
|
||||
onImageUploadStart = () => {
|
||||
|
|
|
@ -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<Props> {
|
|||
};
|
||||
|
||||
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() {
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -425,7 +425,25 @@ export default function Pricing() {
|
|||
id="publish"
|
||||
description={
|
||||
<span>
|
||||
Pass <code>true</code> to publish a draft
|
||||
Pass <code>true</code> to publish a draft.
|
||||
</span>
|
||||
}
|
||||
/>
|
||||
<Argument
|
||||
id="autosave"
|
||||
description={
|
||||
<span>
|
||||
Pass <code>true</code> to signify an autosave. This skips
|
||||
creating a revision.
|
||||
</span>
|
||||
}
|
||||
/>
|
||||
<Argument
|
||||
id="done"
|
||||
description={
|
||||
<span>
|
||||
Pass <code>true</code> to signify the end of an editing
|
||||
session. This will trigger documents.update hooks.
|
||||
</span>
|
||||
}
|
||||
/>
|
||||
|
|
Reference in New Issue