refactor: post publishing (#690)

#### 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
```
pull/692/head
Ryan Wang 2022-11-12 00:10:12 +08:00 committed by GitHub
parent fe89862552
commit 9ac9e1312d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 45 additions and 53 deletions

View File

@ -31,7 +31,7 @@
"@formkit/inputs": "^1.0.0-beta.11", "@formkit/inputs": "^1.0.0-beta.11",
"@formkit/themes": "^1.0.0-beta.11", "@formkit/themes": "^1.0.0-beta.11",
"@formkit/vue": "^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/components": "workspace:*",
"@halo-dev/console-shared": "workspace:*", "@halo-dev/console-shared": "workspace:*",
"@halo-dev/richtext-editor": "^0.0.0-alpha.11", "@halo-dev/richtext-editor": "^0.0.0-alpha.11",

View File

@ -11,7 +11,7 @@ importers:
'@formkit/inputs': ^1.0.0-beta.11 '@formkit/inputs': ^1.0.0-beta.11
'@formkit/themes': ^1.0.0-beta.11 '@formkit/themes': ^1.0.0-beta.11
'@formkit/vue': ^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/components': workspace:*
'@halo-dev/console-shared': workspace:* '@halo-dev/console-shared': workspace:*
'@halo-dev/richtext-editor': ^0.0.0-alpha.11 '@halo-dev/richtext-editor': ^0.0.0-alpha.11
@ -104,7 +104,7 @@ importers:
'@formkit/inputs': 1.0.0-beta.11 '@formkit/inputs': 1.0.0-beta.11
'@formkit/themes': 1.0.0-beta.11_tailwindcss@3.2.1 '@formkit/themes': 1.0.0-beta.11_tailwindcss@3.2.1
'@formkit/vue': 1.0.0-beta.11_vjnbgdptsk6bkj7ab5a6mk2cwm '@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/components': link:packages/components
'@halo-dev/console-shared': link:packages/shared '@halo-dev/console-shared': link:packages/shared
'@halo-dev/richtext-editor': 0.0.0-alpha.11_vue@3.2.41 '@halo-dev/richtext-editor': 0.0.0-alpha.11_vue@3.2.41
@ -1937,8 +1937,8 @@ packages:
- windicss - windicss
dev: false dev: false
/@halo-dev/api-client/0.0.43: /@halo-dev/api-client/0.0.46:
resolution: {integrity: sha512-bCh5P7AYCYA/nVbAB/t62acrtOaDs8UItFe4JmK1qfeb5vOmFT2FT9jeJFj4ABqa4t4xJxy9hnoxNm6H+iB3IQ==} resolution: {integrity: sha512-K2tuCv3OmYzaz9h6X5nVZbghDSd09AawORNAcoi3KuE/Uzi0n5OldO72AuJmVFxtbZqGIHt//CdBSE+cCFq08Q==}
dev: false dev: false
/@halo-dev/richtext-editor/0.0.0-alpha.11_vue@3.2.41: /@halo-dev/richtext-editor/0.0.0-alpha.11_vue@3.2.41:

View File

@ -58,6 +58,7 @@ export function useAttachmentControl(filterOptions?: {
last: false, last: false,
hasNext: false, hasNext: false,
hasPrevious: false, hasPrevious: false,
totalPages: 0,
}); });
const loading = ref<boolean>(false); const loading = ref<boolean>(false);

View File

@ -32,6 +32,7 @@ const comments = ref<ListedCommentList>({
last: false, last: false,
hasNext: false, hasNext: false,
hasPrevious: false, hasPrevious: false,
totalPages: 0,
}); });
const loading = ref(false); const loading = ref(false);
const checkAll = ref(false); const checkAll = ref(false);

View File

@ -34,6 +34,7 @@ const singlePages = ref<ListedSinglePageList>({
last: false, last: false,
hasNext: false, hasNext: false,
hasPrevious: false, hasPrevious: false,
totalPages: 0,
}); });
const loading = ref(false); const loading = ref(false);
const selectedPageNames = ref<string[]>([]); const selectedPageNames = ref<string[]>([]);

View File

@ -46,6 +46,7 @@ const singlePages = ref<ListedSinglePageList>({
last: false, last: false,
hasNext: false, hasNext: false,
hasPrevious: false, hasPrevious: false,
totalPages: 0,
}); });
const loading = ref(false); const loading = ref(false);
const settingModal = ref(false); const settingModal = ref(false);

View File

@ -35,6 +35,7 @@ const posts = ref<ListedPostList>({
last: false, last: false,
hasNext: false, hasNext: false,
hasPrevious: false, hasPrevious: false,
totalPages: 0,
}); });
const loading = ref(false); const loading = ref(false);
const checkedAll = ref(false); const checkedAll = ref(false);

View File

@ -141,27 +141,18 @@ const handlePublish = async () => {
}, },
}); });
// Get latest post await apiClient.post.publishPost({
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({
name: postName, name: postName,
post: formState.value.post, headSnapshot: latestContent.snapshotName,
}); });
} else { } else {
formState.value.post.spec.publish = true; const { data } = await apiClient.post.draftPost({
await apiClient.post.draftPost({
postRequest: formState.value, postRequest: formState.value,
}); });
await apiClient.post.publishPost({
name: data.metadata.name,
});
} }
router.push({ name: "Posts" }); router.push({ name: "Posts" });

View File

@ -39,7 +39,6 @@ import { usePostCategory } from "@/modules/contents/posts/categories/composables
import { usePostTag } from "@/modules/contents/posts/tags/composables/use-post-tag"; import { usePostTag } from "@/modules/contents/posts/tags/composables/use-post-tag";
import { usePermission } from "@/utils/permission"; import { usePermission } from "@/utils/permission";
import { onBeforeRouteLeave } from "vue-router"; import { onBeforeRouteLeave } from "vue-router";
import cloneDeep from "lodash.clonedeep";
import { postLabels } from "@/constants/labels"; import { postLabels } from "@/constants/labels";
const { currentUserHasPermission } = usePermission(); const { currentUserHasPermission } = usePermission();
@ -53,6 +52,7 @@ const posts = ref<ListedPostList>({
last: false, last: false,
hasNext: false, hasNext: false,
hasPrevious: false, hasPrevious: false,
totalPages: 0,
}); });
const loading = ref(false); const loading = ref(false);
const settingModal = ref(false); const settingModal = ref(false);
@ -239,11 +239,8 @@ const handleDelete = async (post: Post) => {
description: "此操作会将文章放入回收站,后续可以从回收站恢复", description: "此操作会将文章放入回收站,后续可以从回收站恢复",
confirmType: "danger", confirmType: "danger",
onConfirm: async () => { onConfirm: async () => {
const postToUpdate = cloneDeep(post); await apiClient.post.recyclePost({
postToUpdate.spec.deleted = true; name: post.metadata.name,
await apiClient.extension.post.updatecontentHaloRunV1alpha1Post({
name: postToUpdate.metadata.name,
post: postToUpdate,
}); });
await handleFetchPosts(); await handleFetchPosts();
}, },

View File

@ -105,46 +105,43 @@ const handleSave = async () => {
} }
}; };
const handleSwitchPublish = async (publish: boolean) => { const handlePublish = async () => {
if (props.onlyEmit) { if (props.onlyEmit) {
emit("published", formState.value); emit("published", formState.value);
return; return;
} }
try { try {
if (publish) { publishing.value = true;
publishing.value = true;
} else {
publishCanceling.value = true;
}
if (publish) { const { data } = await apiClient.post.publishPost({
formState.value.spec.releaseSnapshot = formState.value.spec.headSnapshot; name: formState.value.metadata.name,
} });
const { data } =
await apiClient.extension.post.updatecontentHaloRunV1alpha1Post({
name: formState.value.metadata.name,
post: {
...formState.value,
spec: {
...formState.value.spec,
publish: publish,
},
},
});
formState.value = data; formState.value = data;
if (publish) { emit("published", data);
emit("published", data);
}
handleVisibleChange(false); handleVisibleChange(false);
} catch (e) { } catch (e) {
console.error("Failed to publish post", e); console.error("Failed to publish post", e);
} finally { } finally {
publishing.value = false; 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; publishCanceling.value = false;
} }
}; };
@ -294,7 +291,7 @@ const { templates } = useThemeCustomTemplates("post");
v-if="formState.metadata.labels?.[postLabels.PUBLISHED] !== 'true'" v-if="formState.metadata.labels?.[postLabels.PUBLISHED] !== 'true'"
:loading="publishing" :loading="publishing"
type="secondary" type="secondary"
@click="handleSwitchPublish(true)" @click="handlePublish()"
> >
发布 发布
</VButton> </VButton>
@ -302,7 +299,7 @@ const { templates } = useThemeCustomTemplates("post");
v-else v-else
:loading="publishCanceling" :loading="publishCanceling"
type="danger" type="danger"
@click="handleSwitchPublish(false)" @click="handleUnpublish()"
> >
取消发布 取消发布
</VButton> </VButton>

View File

@ -31,6 +31,7 @@ const plugins = ref<PluginList>({
last: false, last: false,
hasNext: false, hasNext: false,
hasPrevious: false, hasPrevious: false,
totalPages: 0,
}); });
const loading = ref(false); const loading = ref(false);
const pluginInstall = ref(false); const pluginInstall = ref(false);

View File

@ -44,6 +44,7 @@ const users = ref<UserList>({
last: false, last: false,
hasNext: false, hasNext: false,
hasPrevious: false, hasPrevious: false,
totalPages: 0,
}); });
const selectedUserNames = ref<string[]>([]); const selectedUserNames = ref<string[]>([]);
const selectedUser = ref<User>(); const selectedUser = ref<User>();