feat: add retry mechanism for publishing posts in user center (#6406)

#### What type of PR is this?

/area ui
/kind improvement
/milestone 2.18.x

#### What this PR does / why we need it:

为个人中心发布文章的操作添加重试机制,方式后端因为乐观锁出现异常错误。

#### Which issue(s) this PR fixes:

Fixes #6349 


#### Does this PR introduce a user-facing change?

```release-note
为个人中心发布文章的操作添加重试机制,方式后端因为乐观锁出现异常错误。
```
pull/6411/head
Ryan Wang 2024-07-29 18:13:54 +08:00 committed by GitHub
parent 429b832ba8
commit f7ee732c62
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 6 deletions

View File

@ -29,7 +29,7 @@ import { useMutation } from "@tanstack/vue-query";
import { usePostUpdateMutate } from "@uc/modules/contents/posts/composables/use-post-update-mutate";
import { useLocalStorage } from "@vueuse/core";
import { useRouteQuery } from "@vueuse/router";
import type { AxiosRequestConfig } from "axios";
import { AxiosError, type AxiosRequestConfig } from "axios";
import type { ComputedRef } from "vue";
import { computed, nextTick, onMounted, provide, ref, toRef, watch } from "vue";
import { useI18n } from "vue-i18n";
@ -386,10 +386,16 @@ const { mutateAsync: handlePublish, isLoading: isPublishing } = useMutation({
mutationFn: async () => {
await handleSave({ mute: true });
return await ucApiClient.content.post.publishMyPost({
name: formState.value.metadata.name,
});
return await ucApiClient.content.post.publishMyPost(
{
name: formState.value.metadata.name,
},
{
mute: true,
}
);
},
retry: 3,
onSuccess() {
Toast.success(t("core.common.toast.publish_success"), {
duration: 2000,
@ -397,8 +403,13 @@ const { mutateAsync: handlePublish, isLoading: isPublishing } = useMutation({
handleClearCache(formState.value.metadata.name);
router.push({ name: "Posts" });
},
onError() {
Toast.error(t("core.common.toast.publish_failed_and_retry"));
onError(error: Error) {
if (error instanceof AxiosError) {
const { detail, title } = error.response?.data || {};
Toast.error(detail || title);
} else {
Toast.error(t("core.common.toast.publish_failed_and_retry"));
}
},
});