API endpoint accepts autosave

This commit is contained in:
Tom Moor
2018-05-06 22:13:52 -07:00
parent f887a8383b
commit 67f2b3cce4
5 changed files with 57 additions and 25 deletions

View File

@@ -11,6 +11,8 @@ import type { User } from 'types';
import BaseModel from './BaseModel';
import Collection from './Collection';
type SaveOptions = { publish: boolean, done: boolean, autosave: boolean };
class Document extends BaseModel {
isSaving: boolean = false;
hasPendingChanges: boolean = false;
@@ -168,7 +170,7 @@ class Document extends BaseModel {
};
@action
save = async (publish: boolean = false, done: boolean = false) => {
save = async (options: SaveOptions) => {
if (this.isSaving) return this;
this.isSaving = true;
@@ -180,8 +182,7 @@ class Document extends BaseModel {
title: this.title,
text: this.text,
lastRevision: this.revision,
publish,
done,
...options,
});
} else {
const data = {
@@ -189,8 +190,7 @@ class Document extends BaseModel {
collection: this.collection.id,
title: this.title,
text: this.text,
publish,
done,
...options,
};
if (this.parentDocument) {
data.parentDocument = this.parentDocument;

View File

@@ -1,6 +1,7 @@
// @flow
import * as React from 'react';
import get from 'lodash/get';
import debounce from 'lodash/debounce';
import styled from 'styled-components';
import breakpoint from 'styled-components-breakpoint';
import { observable } from 'mobx';
@@ -32,6 +33,7 @@ import CenteredContent from 'components/CenteredContent';
import PageTitle from 'components/PageTitle';
import Search from 'scenes/Search';
const AUTOSAVE_INTERVAL = 3000;
const DISCARD_CHANGES = `
You have unsaved changes.
Are you sure you want to discard them?
@@ -154,20 +156,20 @@ class DocumentScene extends React.Component<Props> {
handleCloseMoveModal = () => (this.moveModalOpen = false);
handleOpenMoveModal = () => (this.moveModalOpen = true);
onSave = async (options: { redirect?: boolean, publish?: boolean } = {}) => {
const { redirect, publish } = options;
onSave = async (
options: { redirect?: boolean, publish?: boolean, autosave?: boolean } = {}
) => {
let document = this.document;
if (!document || !document.allowSave) return;
this.editCache = null;
this.isSaving = true;
this.isPublishing = !!publish;
document = await document.save(publish, redirect);
this.isPublishing = !!options.publish;
document = await document.save(options);
this.isSaving = false;
this.isPublishing = false;
if (redirect) {
if (options.redirect) {
this.props.history.push(document.url);
this.props.ui.setActiveDocument(document);
} else if (this.props.newDocument) {
@@ -176,6 +178,10 @@ class DocumentScene extends React.Component<Props> {
}
};
autosave = debounce(async () => {
this.onSave({ redirect: false, autosave: true });
}, AUTOSAVE_INTERVAL);
onImageUploadStart = () => {
this.isLoading = true;
};
@@ -189,6 +195,7 @@ class DocumentScene extends React.Component<Props> {
if (!document) return;
if (document.text.trim() === text.trim()) return;
document.updateData({ text }, true);
this.autosave();
};
onDiscard = () => {