diff --git a/app/models/BaseModel.ts b/app/models/BaseModel.ts index c70b87af5..8d0d9b5ec 100644 --- a/app/models/BaseModel.ts +++ b/app/models/BaseModel.ts @@ -20,7 +20,10 @@ export default abstract class BaseModel { this.store = store; } - save = async (params?: Record) => { + save = async ( + params?: Record, + options?: Record + ) => { this.isSaving = true; try { @@ -29,7 +32,7 @@ export default abstract class BaseModel { params = this.toAPI(); } - const model = await this.store.save({ ...params, id: this.id }); + const model = await this.store.save({ ...params, id: this.id }, options); // if saving is successful set the new values on the model itself set(this, { ...params, ...model }); diff --git a/app/models/Document.ts b/app/models/Document.ts index 6b3b9370e..f33608088 100644 --- a/app/models/Document.ts +++ b/app/models/Document.ts @@ -1,11 +1,11 @@ import { addDays, differenceInDays } from "date-fns"; import { floor } from "lodash"; -import { action, autorun, computed, observable } from "mobx"; +import { action, autorun, computed, observable, set } from "mobx"; import parseTitle from "@shared/utils/parseTitle"; import unescape from "@shared/utils/unescape"; import DocumentsStore from "~/stores/DocumentsStore"; import User from "~/models/User"; -import { NavigationNode } from "~/types"; +import type { NavigationNode } from "~/types"; import Storage from "~/utils/Storage"; import ParanoidModel from "./ParanoidModel"; import View from "./View"; @@ -63,7 +63,6 @@ export default class Document extends ParanoidModel { @observable title: string; - @Field @observable template: boolean; @@ -309,80 +308,33 @@ export default class Document extends ParanoidModel { return this.store.templatize(this.id); }; - @action - update = async ( - options: SaveOptions & { - title?: string; - lastRevision?: number; - } - ) => { - if (this.isSaving) { - return this; - } - this.isSaving = true; - - try { - if (options.lastRevision) { - return await this.store.update( - { - id: this.id, - title: options.title || this.title, - fullWidth: this.fullWidth, - }, - { - lastRevision: options.lastRevision, - publish: options?.publish, - done: options?.done, - } - ); - } - - throw new Error("Attempting to update without a lastRevision"); - } finally { - this.isSaving = false; - } - }; - @action save = async (options?: SaveOptions | undefined) => { - if (this.isSaving) { - return this; + const params = this.toAPI(); + const collaborativeEditing = this.store.rootStore.auth.team + ?.collaborativeEditing; + + if (collaborativeEditing) { + delete params.text; } - const isCreating = !this.id; + this.isSaving = true; try { - if (isCreating) { - return await this.store.create( - { - parentDocumentId: this.parentDocumentId, - collectionId: this.collectionId, - title: this.title, - text: this.text, - }, - { - publish: options?.publish, - done: options?.done, - autosave: options?.autosave, - } - ); - } - - return await this.store.update( - { - id: this.id, - title: this.title, - text: this.text, - fullWidth: this.fullWidth, - templateId: this.templateId, - }, + const model = await this.store.save( + { ...params, id: this.id }, { lastRevision: options?.lastRevision || this.revision, - publish: options?.publish, - done: options?.done, - autosave: options?.autosave, + ...options, } ); + + // if saving is successful set the new values on the model itself + set(this, { ...params, ...model }); + + this.persistedAttributes = this.toAPI(); + + return model; } finally { this.isSaving = false; } diff --git a/app/scenes/Document/components/Document.tsx b/app/scenes/Document/components/Document.tsx index c48a473ed..33f7adbe0 100644 --- a/app/scenes/Document/components/Document.tsx +++ b/app/scenes/Document/components/Document.tsx @@ -285,7 +285,7 @@ class DocumentScene extends React.Component { autosave?: boolean; } = {} ) => { - const { document, auth } = this.props; + const { document } = this.props; // prevent saves when we are already saving if (document.isSaving) { return; @@ -311,22 +311,10 @@ class DocumentScene extends React.Component { this.isPublishing = !!options.publish; try { - let savedDocument = document; - - if (auth.team?.collaborativeEditing) { - // update does not send "text" field to the API, this is a workaround - // while the multiplayer editor is toggleable. Once it's finalized - // this can be cleaned up to single code path - savedDocument = await document.update({ - ...options, - lastRevision: this.lastRevision, - }); - } else { - savedDocument = await document.save({ - ...options, - lastRevision: this.lastRevision, - }); - } + const savedDocument = await document.save({ + ...options, + lastRevision: this.lastRevision, + }); this.isEditorDirty = false; this.lastRevision = savedDocument.revision; diff --git a/app/stores/BaseStore.ts b/app/stores/BaseStore.ts index fdfd4c6fb..5989fa775 100644 --- a/app/stores/BaseStore.ts +++ b/app/stores/BaseStore.ts @@ -106,11 +106,14 @@ export default abstract class BaseStore { this.data.delete(id); } - save(params: Partial): Promise { + save( + params: Partial, + options?: Record + ): Promise { if (params.id) { - return this.update(params); + return this.update(params, options); } - return this.create(params); + return this.create(params, options); } get(id: string): T | undefined {