Refactor comment data update using the patch api (#6112)

#### What type of PR is this?

/area ui
/kind improvement
/milestone 2.17.x

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

使用新的 patch 接口重构评论相关的逻辑。

#### Special notes for your reviewer:

需要测试:

1. 审核评论和回复功能。
2. 批量审核评论和回复功能。

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

```release-note
None
```
pull/6086/head^2
Ryan Wang 2024-06-24 00:16:33 +08:00 committed by GitHub
parent ae7dcbb317
commit f45ffdeca1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 65 additions and 48 deletions

View File

@ -200,18 +200,22 @@ const handleApproveInBatch = async () => {
});
const promises = commentsToUpdate?.map((comment) => {
return apiClient.extension.comment.updateContentHaloRunV1alpha1Comment(
return apiClient.extension.comment.patchContentHaloRunV1alpha1Comment(
{
name: comment.comment.metadata.name,
comment: {
...comment.comment,
spec: {
...comment.comment.spec,
approved: true,
jsonPatchInner: [
{
op: "add",
path: "/spec/approved",
value: true,
},
{
op: "add",
path: "/spec/approvedTime",
// TODO: see https://github.com/halo-dev/halo/pull/2746
approvedTime: new Date().toISOString(),
},
value: new Date().toISOString(),
},
],
}
);
});

View File

@ -31,7 +31,6 @@ import type {
CommentSubjectRefResult,
} from "@halo-dev/console-shared";
import { useMutation, useQuery, useQueryClient } from "@tanstack/vue-query";
import { cloneDeep } from "lodash-es";
import { computed, onMounted, provide, ref, type Ref } from "vue";
import { useI18n } from "vue-i18n";
import ReplyCreationModal from "./ReplyCreationModal.vue";
@ -91,17 +90,21 @@ const handleApproveReplyInBatch = async () => {
return !reply.reply.spec.approved;
});
const promises = repliesToUpdate?.map((reply) => {
return apiClient.extension.reply.updateContentHaloRunV1alpha1Reply({
return apiClient.extension.reply.patchContentHaloRunV1alpha1Reply({
name: reply.reply.metadata.name,
reply: {
...reply.reply,
spec: {
...reply.reply.spec,
approved: true,
jsonPatchInner: [
{
op: "add",
path: "/spec/approved",
value: true,
},
{
op: "add",
path: "/spec/approvedTime",
// TODO: see https://github.com/halo-dev/halo/pull/2746
approvedTime: new Date().toISOString(),
},
value: new Date().toISOString(),
},
],
});
});
await Promise.all(promises || []);
@ -118,13 +121,21 @@ const handleApproveReplyInBatch = async () => {
const handleApprove = async () => {
try {
const commentToUpdate = cloneDeep(props.comment.comment);
commentToUpdate.spec.approved = true;
await apiClient.extension.comment.patchContentHaloRunV1alpha1Comment({
name: props.comment.comment.metadata.name,
jsonPatchInner: [
{
op: "add",
path: "/spec/approved",
value: true,
},
{
op: "add",
path: "/spec/approvedTime",
// TODO: see https://github.com/halo-dev/halo/pull/2746
commentToUpdate.spec.approvedTime = new Date().toISOString();
await apiClient.extension.comment.updateContentHaloRunV1alpha1Comment({
name: commentToUpdate.metadata.name,
comment: commentToUpdate,
value: new Date().toISOString(),
},
],
});
Toast.success(t("core.common.toast.operation_success"));
@ -165,21 +176,16 @@ const {
const { mutateAsync: updateCommentLastReadTimeMutate } = useMutation({
mutationKey: ["update-comment-last-read-time"],
mutationFn: async () => {
const { data: latestComment } =
await apiClient.extension.comment.getContentHaloRunV1alpha1Comment({
name: props.comment.comment.metadata.name,
});
if (!latestComment.status?.unreadReplyCount) {
return latestComment;
}
latestComment.spec.lastReadTime = new Date().toISOString();
return apiClient.extension.comment.updateContentHaloRunV1alpha1Comment(
return apiClient.extension.comment.patchContentHaloRunV1alpha1Comment(
{
name: latestComment.metadata.name,
comment: latestComment,
name: props.comment.comment.metadata.name,
jsonPatchInner: [
{
op: "add",
path: "/spec/lastReadTime",
value: new Date().toISOString(),
},
],
},
{
mute: true,

View File

@ -14,7 +14,6 @@ import type { ListedComment, ListedReply } from "@halo-dev/api-client";
import { formatDatetime } from "@/utils/date";
import { apiClient } from "@/utils/api-client";
import { computed, inject, ref, type Ref } from "vue";
import { cloneDeep } from "lodash-es";
import { useI18n } from "vue-i18n";
import { useQueryClient } from "@tanstack/vue-query";
import ReplyCreationModal from "./ReplyCreationModal.vue";
@ -71,13 +70,21 @@ const handleDelete = async () => {
const handleApprove = async () => {
try {
const replyToUpdate = cloneDeep(props.reply.reply);
replyToUpdate.spec.approved = true;
await apiClient.extension.reply.patchContentHaloRunV1alpha1Reply({
name: props.reply.reply.metadata.name,
jsonPatchInner: [
{
op: "add",
path: "/spec/approved",
value: true,
},
{
op: "add",
path: "/spec/approvedTime",
// TODO: see https://github.com/halo-dev/halo/pull/2746
replyToUpdate.spec.approvedTime = new Date().toISOString();
await apiClient.extension.reply.updateContentHaloRunV1alpha1Reply({
name: replyToUpdate.metadata.name,
reply: replyToUpdate,
value: new Date().toISOString(),
},
],
});
Toast.success(t("core.common.toast.operation_success"));