From fe0de1a7137a2d5f7506fabb5b27e0d8c6344eb9 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Sat, 12 Nov 2022 00:10:12 +0800 Subject: [PATCH] refactor: post publishing (halo-dev/console#690) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind improvement /milestone 2.0 #### What this PR does / why we need it: 重构文章发布/取消发布/放入回收站的逻辑。适配 https://github.com/halo-dev/halo/pull/2675 #### Special notes for your reviewer: 测试方式: 1. Halo 需要切换到 https://github.com/halo-dev/halo/pull/2675 的分支。 2. Console 需要 `pnpm install`。 3. 测试文章发布/取消发布/放入回收站。 #### Does this PR introduce a user-facing change? ```release-note None ``` --- package.json | 2 +- pnpm-lock.yaml | 8 +-- .../attachments/composables/use-attachment.ts | 1 + src/modules/contents/comments/CommentList.vue | 1 + .../contents/pages/DeletedSinglePageList.vue | 1 + src/modules/contents/pages/SinglePageList.vue | 1 + .../contents/posts/DeletedPostList.vue | 1 + src/modules/contents/posts/PostEditor.vue | 23 +++------ src/modules/contents/posts/PostList.vue | 9 ++-- .../posts/components/PostSettingModal.vue | 49 +++++++++---------- src/modules/system/plugins/PluginList.vue | 1 + src/modules/system/users/UserList.vue | 1 + 12 files changed, 45 insertions(+), 53 deletions(-) diff --git a/package.json b/package.json index 5d558322c..25b13b9dc 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "@formkit/inputs": "^1.0.0-beta.11", "@formkit/themes": "^1.0.0-beta.11", "@formkit/vue": "^1.0.0-beta.11", - "@halo-dev/api-client": "^0.0.43", + "@halo-dev/api-client": "^0.0.46", "@halo-dev/components": "workspace:*", "@halo-dev/console-shared": "workspace:*", "@halo-dev/richtext-editor": "^0.0.0-alpha.11", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c6307b585..c76eb854f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,7 +11,7 @@ importers: '@formkit/inputs': ^1.0.0-beta.11 '@formkit/themes': ^1.0.0-beta.11 '@formkit/vue': ^1.0.0-beta.11 - '@halo-dev/api-client': ^0.0.43 + '@halo-dev/api-client': ^0.0.46 '@halo-dev/components': workspace:* '@halo-dev/console-shared': workspace:* '@halo-dev/richtext-editor': ^0.0.0-alpha.11 @@ -104,7 +104,7 @@ importers: '@formkit/inputs': 1.0.0-beta.11 '@formkit/themes': 1.0.0-beta.11_tailwindcss@3.2.1 '@formkit/vue': 1.0.0-beta.11_vjnbgdptsk6bkj7ab5a6mk2cwm - '@halo-dev/api-client': 0.0.43 + '@halo-dev/api-client': 0.0.46 '@halo-dev/components': link:packages/components '@halo-dev/console-shared': link:packages/shared '@halo-dev/richtext-editor': 0.0.0-alpha.11_vue@3.2.41 @@ -1937,8 +1937,8 @@ packages: - windicss dev: false - /@halo-dev/api-client/0.0.43: - resolution: {integrity: sha512-bCh5P7AYCYA/nVbAB/t62acrtOaDs8UItFe4JmK1qfeb5vOmFT2FT9jeJFj4ABqa4t4xJxy9hnoxNm6H+iB3IQ==} + /@halo-dev/api-client/0.0.46: + resolution: {integrity: sha512-K2tuCv3OmYzaz9h6X5nVZbghDSd09AawORNAcoi3KuE/Uzi0n5OldO72AuJmVFxtbZqGIHt//CdBSE+cCFq08Q==} dev: false /@halo-dev/richtext-editor/0.0.0-alpha.11_vue@3.2.41: diff --git a/src/modules/contents/attachments/composables/use-attachment.ts b/src/modules/contents/attachments/composables/use-attachment.ts index 7eebbf2c1..4da365b6e 100644 --- a/src/modules/contents/attachments/composables/use-attachment.ts +++ b/src/modules/contents/attachments/composables/use-attachment.ts @@ -58,6 +58,7 @@ export function useAttachmentControl(filterOptions?: { last: false, hasNext: false, hasPrevious: false, + totalPages: 0, }); const loading = ref(false); diff --git a/src/modules/contents/comments/CommentList.vue b/src/modules/contents/comments/CommentList.vue index aa1af273f..56e7e51b1 100644 --- a/src/modules/contents/comments/CommentList.vue +++ b/src/modules/contents/comments/CommentList.vue @@ -32,6 +32,7 @@ const comments = ref({ last: false, hasNext: false, hasPrevious: false, + totalPages: 0, }); const loading = ref(false); const checkAll = ref(false); diff --git a/src/modules/contents/pages/DeletedSinglePageList.vue b/src/modules/contents/pages/DeletedSinglePageList.vue index a802f30eb..aaa433485 100644 --- a/src/modules/contents/pages/DeletedSinglePageList.vue +++ b/src/modules/contents/pages/DeletedSinglePageList.vue @@ -34,6 +34,7 @@ const singlePages = ref({ last: false, hasNext: false, hasPrevious: false, + totalPages: 0, }); const loading = ref(false); const selectedPageNames = ref([]); diff --git a/src/modules/contents/pages/SinglePageList.vue b/src/modules/contents/pages/SinglePageList.vue index b3e198e2e..1f753518a 100644 --- a/src/modules/contents/pages/SinglePageList.vue +++ b/src/modules/contents/pages/SinglePageList.vue @@ -46,6 +46,7 @@ const singlePages = ref({ last: false, hasNext: false, hasPrevious: false, + totalPages: 0, }); const loading = ref(false); const settingModal = ref(false); diff --git a/src/modules/contents/posts/DeletedPostList.vue b/src/modules/contents/posts/DeletedPostList.vue index aea76693b..477a4aa82 100644 --- a/src/modules/contents/posts/DeletedPostList.vue +++ b/src/modules/contents/posts/DeletedPostList.vue @@ -35,6 +35,7 @@ const posts = ref({ last: false, hasNext: false, hasPrevious: false, + totalPages: 0, }); const loading = ref(false); const checkedAll = ref(false); diff --git a/src/modules/contents/posts/PostEditor.vue b/src/modules/contents/posts/PostEditor.vue index 1c8af8e40..703e50571 100644 --- a/src/modules/contents/posts/PostEditor.vue +++ b/src/modules/contents/posts/PostEditor.vue @@ -141,27 +141,18 @@ const handlePublish = async () => { }, }); - // Get latest post - const { data: latestPost } = - await apiClient.extension.post.getcontentHaloRunV1alpha1Post({ - name: postName, - }); - - formState.value.post = latestPost; - formState.value.post.spec.publish = true; - formState.value.post.spec.headSnapshot = latestContent.snapshotName; - formState.value.post.spec.releaseSnapshot = - formState.value.post.spec.headSnapshot; - - await apiClient.extension.post.updatecontentHaloRunV1alpha1Post({ + await apiClient.post.publishPost({ name: postName, - post: formState.value.post, + headSnapshot: latestContent.snapshotName, }); } else { - formState.value.post.spec.publish = true; - await apiClient.post.draftPost({ + const { data } = await apiClient.post.draftPost({ postRequest: formState.value, }); + + await apiClient.post.publishPost({ + name: data.metadata.name, + }); } router.push({ name: "Posts" }); diff --git a/src/modules/contents/posts/PostList.vue b/src/modules/contents/posts/PostList.vue index 0932f0e26..a45b90581 100644 --- a/src/modules/contents/posts/PostList.vue +++ b/src/modules/contents/posts/PostList.vue @@ -39,7 +39,6 @@ import { usePostCategory } from "@/modules/contents/posts/categories/composables import { usePostTag } from "@/modules/contents/posts/tags/composables/use-post-tag"; import { usePermission } from "@/utils/permission"; import { onBeforeRouteLeave } from "vue-router"; -import cloneDeep from "lodash.clonedeep"; import { postLabels } from "@/constants/labels"; const { currentUserHasPermission } = usePermission(); @@ -53,6 +52,7 @@ const posts = ref({ last: false, hasNext: false, hasPrevious: false, + totalPages: 0, }); const loading = ref(false); const settingModal = ref(false); @@ -239,11 +239,8 @@ const handleDelete = async (post: Post) => { description: "此操作会将文章放入回收站,后续可以从回收站恢复", confirmType: "danger", onConfirm: async () => { - const postToUpdate = cloneDeep(post); - postToUpdate.spec.deleted = true; - await apiClient.extension.post.updatecontentHaloRunV1alpha1Post({ - name: postToUpdate.metadata.name, - post: postToUpdate, + await apiClient.post.recyclePost({ + name: post.metadata.name, }); await handleFetchPosts(); }, diff --git a/src/modules/contents/posts/components/PostSettingModal.vue b/src/modules/contents/posts/components/PostSettingModal.vue index 05c9828c9..44f66777a 100644 --- a/src/modules/contents/posts/components/PostSettingModal.vue +++ b/src/modules/contents/posts/components/PostSettingModal.vue @@ -105,46 +105,43 @@ const handleSave = async () => { } }; -const handleSwitchPublish = async (publish: boolean) => { +const handlePublish = async () => { if (props.onlyEmit) { emit("published", formState.value); return; } try { - if (publish) { - publishing.value = true; - } else { - publishCanceling.value = true; - } + publishing.value = true; - if (publish) { - formState.value.spec.releaseSnapshot = formState.value.spec.headSnapshot; - } - - const { data } = - await apiClient.extension.post.updatecontentHaloRunV1alpha1Post({ - name: formState.value.metadata.name, - post: { - ...formState.value, - spec: { - ...formState.value.spec, - publish: publish, - }, - }, - }); + const { data } = await apiClient.post.publishPost({ + name: formState.value.metadata.name, + }); formState.value = data; - if (publish) { - emit("published", data); - } + emit("published", data); handleVisibleChange(false); } catch (e) { console.error("Failed to publish post", e); } finally { publishing.value = false; + } +}; + +const handleUnpublish = async () => { + try { + publishCanceling.value = true; + + await apiClient.post.unpublishPost({ + name: formState.value.metadata.name, + }); + + handleVisibleChange(false); + } catch (e) { + console.error("Failed to publish post", e); + } finally { publishCanceling.value = false; } }; @@ -294,7 +291,7 @@ const { templates } = useThemeCustomTemplates("post"); v-if="formState.metadata.labels?.[postLabels.PUBLISHED] !== 'true'" :loading="publishing" type="secondary" - @click="handleSwitchPublish(true)" + @click="handlePublish()" > 发布 @@ -302,7 +299,7 @@ const { templates } = useThemeCustomTemplates("post"); v-else :loading="publishCanceling" type="danger" - @click="handleSwitchPublish(false)" + @click="handleUnpublish()" > 取消发布 diff --git a/src/modules/system/plugins/PluginList.vue b/src/modules/system/plugins/PluginList.vue index 3686498f1..3eccea7e5 100644 --- a/src/modules/system/plugins/PluginList.vue +++ b/src/modules/system/plugins/PluginList.vue @@ -31,6 +31,7 @@ const plugins = ref({ last: false, hasNext: false, hasPrevious: false, + totalPages: 0, }); const loading = ref(false); const pluginInstall = ref(false); diff --git a/src/modules/system/users/UserList.vue b/src/modules/system/users/UserList.vue index 62394471b..f3def5fdd 100644 --- a/src/modules/system/users/UserList.vue +++ b/src/modules/system/users/UserList.vue @@ -44,6 +44,7 @@ const users = ref({ last: false, hasNext: false, hasPrevious: false, + totalPages: 0, }); const selectedUserNames = ref([]); const selectedUser = ref();