chore: Simplify model save codepath, prevents text from being sent ever when collab editing enabled

This commit is contained in:
Tom Moor
2022-06-20 22:55:37 +02:00
parent 6975d76faf
commit 9cd3ec0868
4 changed files with 35 additions and 89 deletions

View File

@@ -20,7 +20,10 @@ export default abstract class BaseModel {
this.store = store;
}
save = async (params?: Record<string, any>) => {
save = async (
params?: Record<string, any>,
options?: Record<string, string | boolean | number | undefined>
) => {
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 });

View File

@@ -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;
}

View File

@@ -285,7 +285,7 @@ class DocumentScene extends React.Component<Props> {
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<Props> {
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({
const savedDocument = await document.save({
...options,
lastRevision: this.lastRevision,
});
} else {
savedDocument = await document.save({
...options,
lastRevision: this.lastRevision,
});
}
this.isEditorDirty = false;
this.lastRevision = savedDocument.revision;

View File

@@ -106,11 +106,14 @@ export default abstract class BaseStore<T extends BaseModel> {
this.data.delete(id);
}
save(params: Partial<T>): Promise<T> {
save(
params: Partial<T>,
options?: Record<string, string | boolean | number | undefined>
): Promise<T> {
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 {