Improve reliability of document operations with websocket disconnected
This commit is contained in:
@@ -246,13 +246,17 @@ export const unpublishDocument = createAction({
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await document.unpublish();
|
||||
|
||||
toast.message(
|
||||
toast.success(
|
||||
t("Unpublished {{ documentName }}", {
|
||||
documentName: document.noun,
|
||||
})
|
||||
);
|
||||
} catch (err) {
|
||||
toast.error(err.message);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -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 });
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
Reference in New Issue
Block a user