diff --git a/src/modules/contents/pages/components/SinglePageSettingModal.vue b/src/modules/contents/pages/components/SinglePageSettingModal.vue index 67ce50c8..cd670636 100644 --- a/src/modules/contents/pages/components/SinglePageSettingModal.vue +++ b/src/modules/contents/pages/components/SinglePageSettingModal.vue @@ -17,6 +17,7 @@ import { toDatetimeLocal, toISOString } from "@/utils/date"; import { submitForm } from "@formkit/core"; import AnnotationsForm from "@/components/form/AnnotationsForm.vue"; import useSlugify from "@/composables/use-slugify"; +import { useMutation } from "@tanstack/vue-query"; const initialFormState: SinglePage = { spec: { @@ -110,6 +111,40 @@ const handlePublishClick = () => { }); }; +// Fix me: +// Force update post settings, +// because currently there may be errors caused by changes in version due to asynchronous processing. +const { mutateAsync: singlePageUpdateMutate } = useMutation({ + mutationKey: ["singlePage-update"], + mutationFn: async (page: SinglePage) => { + const { data: latestSinglePage } = + await apiClient.extension.singlePage.getcontentHaloRunV1alpha1SinglePage({ + name: page.metadata.name, + }); + return apiClient.extension.singlePage.updatecontentHaloRunV1alpha1SinglePage( + { + name: page.metadata.name, + singlePage: { + ...latestSinglePage, + spec: page.spec, + metadata: { + ...latestSinglePage.metadata, + annotations: page.metadata.annotations, + }, + }, + }, + { + mute: true, + } + ); + }, + retry: 3, + onError: (error) => { + console.error("Failed to update post", error); + Toast.error(`服务器内部错误`); + }, +}); + const handleSave = async () => { annotationsFormRef.value?.handleSubmit(); await nextTick(); @@ -133,15 +168,8 @@ const handleSave = async () => { try { saving.value = true; - saving.value = true; - const { data } = isUpdateMode.value - ? await apiClient.extension.singlePage.updatecontentHaloRunV1alpha1SinglePage( - { - name: formState.value.metadata.name, - singlePage: formState.value, - } - ) + ? await singlePageUpdateMutate(formState.value) : await apiClient.extension.singlePage.createcontentHaloRunV1alpha1SinglePage( { singlePage: formState.value, diff --git a/src/modules/contents/posts/components/PostSettingModal.vue b/src/modules/contents/posts/components/PostSettingModal.vue index 8affc3ea..89d3fa2d 100644 --- a/src/modules/contents/posts/components/PostSettingModal.vue +++ b/src/modules/contents/posts/components/PostSettingModal.vue @@ -17,6 +17,7 @@ import { toDatetimeLocal, toISOString } from "@/utils/date"; import AnnotationsForm from "@/components/form/AnnotationsForm.vue"; import { submitForm } from "@formkit/core"; import useSlugify from "@/composables/use-slugify"; +import { useMutation } from "@tanstack/vue-query"; const initialFormState: Post = { spec: { @@ -114,6 +115,40 @@ const handlePublishClick = () => { }); }; +// Fix me: +// Force update post settings, +// because currently there may be errors caused by changes in version due to asynchronous processing. +const { mutateAsync: postUpdateMutate } = useMutation({ + mutationKey: ["post-update"], + mutationFn: async (post: Post) => { + const { data: latestPost } = + await apiClient.extension.post.getcontentHaloRunV1alpha1Post({ + name: post.metadata.name, + }); + return apiClient.extension.post.updatecontentHaloRunV1alpha1Post( + { + name: post.metadata.name, + post: { + ...latestPost, + spec: post.spec, + metadata: { + ...latestPost.metadata, + annotations: post.metadata.annotations, + }, + }, + }, + { + mute: true, + } + ); + }, + retry: 3, + onError: (error) => { + console.error("Failed to update post", error); + Toast.error(`服务器内部错误`); + }, +}); + const handleSave = async () => { annotationsFormRef.value?.handleSubmit(); await nextTick(); @@ -139,10 +174,7 @@ const handleSave = async () => { saving.value = true; const { data } = isUpdateMode.value - ? await apiClient.extension.post.updatecontentHaloRunV1alpha1Post({ - name: formState.value.metadata.name, - post: formState.value, - }) + ? await postUpdateMutate(formState.value) : await apiClient.extension.post.createcontentHaloRunV1alpha1Post({ post: formState.value, }); diff --git a/src/modules/contents/posts/components/__tests__/PostSettingModal.spec.ts b/src/modules/contents/posts/components/__tests__/PostSettingModal.spec.ts index 781930aa..1fba561b 100644 --- a/src/modules/contents/posts/components/__tests__/PostSettingModal.spec.ts +++ b/src/modules/contents/posts/components/__tests__/PostSettingModal.spec.ts @@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it } from "vitest"; import { mount } from "@vue/test-utils"; import PostSettingModal from "../PostSettingModal.vue"; import { createPinia, setActivePinia } from "pinia"; +import { VueQueryPlugin } from "@tanstack/vue-query"; describe("PostSettingModal", () => { beforeEach(() => { @@ -9,12 +10,19 @@ describe("PostSettingModal", () => { }); it("should render", () => { - const wrapper = mount({ - components: { - PostSettingModal, + const wrapper = mount( + { + components: { + PostSettingModal, + }, + template: ``, }, - template: ``, - }); + { + global: { + plugins: [VueQueryPlugin], + }, + } + ); expect(wrapper).toBeDefined(); }); });