refactor: improve part of the post operation logic using the patch api (#6464)

#### What type of PR is this?

/area ui
/kind improvement
/milestone 2.19.x

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

使用 patch 接口重构文章的恢复、可见性修改等逻辑。

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

None

#### Special notes for your reviewer:

测试文章删除、恢复、可见性修改功能是否符合预期。

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

```release-note
使用 patch 接口重构文章的恢复、可见性修改等逻辑。
```
pull/6470/head
Ryan Wang 2024-08-14 14:59:57 +08:00 committed by GitHub
parent d78547f963
commit d011beb89b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 29 deletions

View File

@ -23,7 +23,6 @@ import {
VStatusDot,
} from "@halo-dev/components";
import { useQuery } from "@tanstack/vue-query";
import { cloneDeep } from "lodash-es";
import { ref, watch } from "vue";
import { useI18n } from "vue-i18n";
import PostTag from "./tags/components/PostTag.vue";
@ -132,11 +131,15 @@ const handleRecovery = async (post: Post) => {
confirmText: t("core.common.buttons.confirm"),
cancelText: t("core.common.buttons.cancel"),
onConfirm: async () => {
const postToUpdate = cloneDeep(post);
postToUpdate.spec.deleted = false;
await coreApiClient.content.post.updatePost({
name: postToUpdate.metadata.name,
post: postToUpdate,
await coreApiClient.content.post.patchPost({
name: post.metadata.name,
jsonPatchInner: [
{
op: "add",
path: "/spec/deleted",
value: false,
},
],
});
await refetch();
@ -157,23 +160,23 @@ const handleRecoveryInBatch = async () => {
onConfirm: async () => {
await Promise.all(
selectedPostNames.value.map((name) => {
const post = posts.value?.find(
const isPostExist = posts.value?.some(
(item) => item.post.metadata.name === name
)?.post;
);
if (!post) {
if (!isPostExist) {
return Promise.resolve();
}
return coreApiClient.content.post.updatePost({
name: post.metadata.name,
post: {
...post,
spec: {
...post.spec,
deleted: false,
return coreApiClient.content.post.patchPost({
name: name,
jsonPatchInner: [
{
op: "add",
path: "/spec/deleted",
value: false,
},
},
],
});
})
);

View File

@ -185,6 +185,7 @@ const { startFields, endFields } = useEntityFieldItemExtensionPoint<ListedPost>(
priority: 30,
position: "end",
component: markRaw(VisibleField),
permissions: ["system:posts:manage"],
props: {
post: props.post,
},

View File

@ -17,24 +17,21 @@ withDefaults(
const { mutate: changeVisibleMutation } = useMutation({
mutationFn: async (post: Post) => {
const { data } = await coreApiClient.content.post.getPost({
return await coreApiClient.content.post.patchPost({
name: post.metadata.name,
jsonPatchInner: [
{
op: "add",
path: "/spec/visible",
value: post.spec.visible === "PRIVATE" ? "PUBLIC" : "PRIVATE",
},
],
});
data.spec.visible = data.spec.visible === "PRIVATE" ? "PUBLIC" : "PRIVATE";
await coreApiClient.content.post.updatePost(
{
name: post.metadata.name,
post: data,
},
{
mute: true,
}
);
await queryClient.invalidateQueries({ queryKey: ["posts"] });
},
retry: 3,
onSuccess: () => {
Toast.success(t("core.common.toast.operation_success"));
queryClient.invalidateQueries({ queryKey: ["posts"] });
},
onError: () => {
Toast.error(t("core.common.toast.operation_failed"));