Improve reliability of document operations with websocket disconnected

This commit is contained in:
Tom Moor
2023-10-22 23:21:33 -04:00
parent 1abe4964e8
commit 5fc68db5da
3 changed files with 24 additions and 80 deletions

View File

@@ -246,13 +246,17 @@ export const unpublishDocument = createAction({
return;
}
await document.unpublish();
try {
await document.unpublish();
toast.message(
t("Unpublished {{ documentName }}", {
documentName: document.noun,
})
);
toast.success(
t("Unpublished {{ documentName }}", {
documentName: document.noun,
})
);
} catch (err) {
toast.error(err.message);
}
},
});

View File

@@ -651,42 +651,6 @@ export default class DocumentsStore extends Store<Document> {
documentIds.forEach((id) => this.remove(id));
}
@action
async update(
params: {
id: string;
title?: string;
emoji?: string | null;
text?: string;
fullWidth?: boolean;
templateId?: string;
},
options?: {
publish?: boolean;
done?: boolean;
autosave?: boolean;
}
) {
this.isSaving = true;
try {
const res = await client.post(`/${this.apiEndpoint}.update`, {
...params,
...options,
apiVersion: 2,
});
invariant(res?.data, "Data should be available");
this.addPolicies(res.policies);
const document = this.add(res.data.document);
const collection = this.getCollectionForDocument(document);
collection?.updateData(res.data.collection);
return document;
} finally {
this.isSaving = false;
}
}
@action
async delete(
document: Document,
@@ -749,19 +713,29 @@ export default class DocumentsStore extends Store<Document> {
}
};
@action
update = async (
params: Partial<Document>,
options?: Record<string, string | boolean | number | undefined>
): Promise<Document> => {
const document = await super.update(params, options);
const collection = this.getCollectionForDocument(document);
void collection?.fetchDocuments({ force: true });
return document;
};
@action
unpublish = async (document: Document) => {
const res = await client.post("/documents.unpublish", {
id: document.id,
apiVersion: 2,
});
runInAction("Document#unpublish", () => {
invariant(res?.data, "Data should be available");
document.updateData(res.data.document);
const collection = this.getCollectionForDocument(document);
collection?.updateData(res.data.collection);
document.updateData(res.data);
this.addPolicies(res.policies);
const collection = this.getCollectionForDocument(document);
void collection?.fetchDocuments({ force: true });
});
};

View File

@@ -3360,40 +3360,6 @@ describe("#documents.update", () => {
expect(res.status).toBe(400);
expect(body.message).toBe("id: Required");
});
describe("apiVersion=2", () => {
it("should successfully publish a draft", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
});
const document = await buildDraftDocument({
title: "title",
text: "text",
teamId: team.id,
collectionId: null,
});
const res = await server.post("/api/documents.update", {
body: {
apiVersion: 2,
token: user.getJwtToken(),
id: document.id,
title: "Updated title",
text: "Updated text",
collectionId: collection.id,
publish: true,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.document.collectionId).toBe(collection.id);
expect(body.data.document.title).toBe("Updated title");
expect(body.data.document.text).toBe("Updated text");
});
});
});
describe("#documents.archive", () => {