diff --git a/app/actions/definitions/documents.tsx b/app/actions/definitions/documents.tsx index 17d88345b..7c2f4fee7 100644 --- a/app/actions/definitions/documents.tsx +++ b/app/actions/definitions/documents.tsx @@ -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); + } }, }); diff --git a/app/stores/DocumentsStore.ts b/app/stores/DocumentsStore.ts index 290363d56..01555b5bf 100644 --- a/app/stores/DocumentsStore.ts +++ b/app/stores/DocumentsStore.ts @@ -651,42 +651,6 @@ export default class DocumentsStore extends Store { 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 { } }; + @action + update = async ( + params: Partial, + options?: Record + ): Promise => { + 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 }); }); }; diff --git a/server/routes/api/documents/documents.test.ts b/server/routes/api/documents/documents.test.ts index 4bf9f8c0c..f014f7a0a 100644 --- a/server/routes/api/documents/documents.test.ts +++ b/server/routes/api/documents/documents.test.ts @@ -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", () => {