2023-11-30 03:55:29 +00:00
|
|
|
<script lang="ts" setup>
|
2024-06-26 10:42:50 +00:00
|
|
|
import { contentAnnotations } from "@/constants/annotations";
|
|
|
|
import { randomUUID } from "@/utils/id";
|
2023-11-30 03:55:29 +00:00
|
|
|
import type { Content, Post } from "@halo-dev/api-client";
|
2024-06-26 10:42:50 +00:00
|
|
|
import { ucApiClient } from "@halo-dev/api-client";
|
|
|
|
import { Toast, VButton, VModal, VSpace } from "@halo-dev/components";
|
2023-11-30 03:55:29 +00:00
|
|
|
import { useMutation } from "@tanstack/vue-query";
|
2024-06-26 10:42:50 +00:00
|
|
|
import { ref } from "vue";
|
2023-11-30 03:55:29 +00:00
|
|
|
import { useI18n } from "vue-i18n";
|
2024-06-26 10:42:50 +00:00
|
|
|
import type { PostFormState } from "../types";
|
|
|
|
import PostSettingForm from "./PostSettingForm.vue";
|
2023-11-30 03:55:29 +00:00
|
|
|
|
|
|
|
const { t } = useI18n();
|
|
|
|
|
|
|
|
const props = withDefaults(
|
|
|
|
defineProps<{
|
|
|
|
title: string;
|
|
|
|
content: Content;
|
|
|
|
publish?: boolean;
|
2024-03-18 04:38:07 +00:00
|
|
|
post: Post;
|
2023-11-30 03:55:29 +00:00
|
|
|
}>(),
|
|
|
|
{
|
|
|
|
publish: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
(event: "close"): void;
|
|
|
|
(event: "success", post: Post): void;
|
|
|
|
}>();
|
|
|
|
|
2024-05-27 08:56:57 +00:00
|
|
|
const modal = ref<InstanceType<typeof VModal> | null>(null);
|
2023-11-30 03:55:29 +00:00
|
|
|
|
|
|
|
const { mutate, isLoading } = useMutation({
|
2024-03-25 04:38:08 +00:00
|
|
|
mutationKey: ["uc:create-post"],
|
2023-11-30 03:55:29 +00:00
|
|
|
mutationFn: async ({ data }: { data: PostFormState }) => {
|
|
|
|
const post: Post = {
|
|
|
|
apiVersion: "content.halo.run/v1alpha1",
|
|
|
|
kind: "Post",
|
|
|
|
metadata: {
|
|
|
|
annotations: {
|
|
|
|
[contentAnnotations.CONTENT_JSON]: JSON.stringify(props.content),
|
|
|
|
},
|
|
|
|
name: randomUUID(),
|
|
|
|
},
|
|
|
|
spec: {
|
|
|
|
allowComment: data.allowComment,
|
|
|
|
categories: data.categories,
|
|
|
|
cover: data.cover,
|
|
|
|
deleted: false,
|
|
|
|
excerpt: {
|
|
|
|
autoGenerate: data.excerptAutoGenerate,
|
|
|
|
raw: data.excerptRaw,
|
|
|
|
},
|
|
|
|
htmlMetas: [],
|
|
|
|
pinned: data.pinned,
|
|
|
|
priority: 0,
|
|
|
|
publish: false,
|
|
|
|
publishTime: data.publishTime,
|
|
|
|
slug: data.slug,
|
|
|
|
tags: data.tags,
|
|
|
|
title: data.title,
|
|
|
|
visible: data.visible,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-06-25 04:31:44 +00:00
|
|
|
const { data: createdPost } = await ucApiClient.content.post.createMyPost({
|
2023-11-30 03:55:29 +00:00
|
|
|
post,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (props.publish) {
|
2024-06-25 04:31:44 +00:00
|
|
|
await ucApiClient.content.post.publishMyPost({
|
2023-11-30 03:55:29 +00:00
|
|
|
name: post.metadata.name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return createdPost;
|
|
|
|
},
|
|
|
|
onSuccess(data) {
|
|
|
|
if (props.publish) {
|
|
|
|
Toast.success(t("core.common.toast.publish_success"));
|
|
|
|
} else {
|
|
|
|
Toast.success(t("core.common.toast.save_success"));
|
|
|
|
}
|
|
|
|
|
|
|
|
emit("success", data);
|
2024-05-27 08:56:57 +00:00
|
|
|
modal.value?.close();
|
2023-11-30 03:55:29 +00:00
|
|
|
},
|
|
|
|
onError() {
|
|
|
|
if (props.publish) {
|
|
|
|
Toast.error(t("core.common.toast.publish_failed_and_retry"));
|
|
|
|
} else {
|
|
|
|
Toast.error(t("core.common.toast.save_failed_and_retry"));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
function onSubmit(data: PostFormState) {
|
|
|
|
mutate({ data });
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<VModal
|
2024-01-10 06:03:14 +00:00
|
|
|
ref="modal"
|
2023-11-30 03:55:29 +00:00
|
|
|
:title="title"
|
|
|
|
:width="700"
|
|
|
|
centered
|
2024-01-10 06:03:14 +00:00
|
|
|
@close="emit('close')"
|
2023-11-30 03:55:29 +00:00
|
|
|
>
|
2024-03-18 04:38:07 +00:00
|
|
|
<PostSettingForm
|
|
|
|
:form-state="{
|
|
|
|
title: props.post.spec.title,
|
|
|
|
slug: props.post.spec.slug,
|
|
|
|
allowComment: props.post.spec.allowComment,
|
|
|
|
visible: props.post.spec.visible,
|
|
|
|
pinned: props.post.spec.pinned,
|
|
|
|
excerptAutoGenerate: props.post.spec.excerpt.autoGenerate,
|
|
|
|
}"
|
|
|
|
@submit="onSubmit"
|
|
|
|
/>
|
2023-11-30 03:55:29 +00:00
|
|
|
|
|
|
|
<template #footer>
|
|
|
|
<VSpace>
|
|
|
|
<VButton
|
|
|
|
:loading="isLoading"
|
|
|
|
type="secondary"
|
|
|
|
@click="$formkit.submit('post-setting-form')"
|
|
|
|
>
|
|
|
|
{{
|
|
|
|
props.publish
|
|
|
|
? $t("core.common.buttons.publish")
|
|
|
|
: $t("core.common.buttons.save")
|
|
|
|
}}
|
|
|
|
</VButton>
|
2024-05-27 08:56:57 +00:00
|
|
|
<VButton type="default" @click="modal?.close()">
|
2023-11-30 03:55:29 +00:00
|
|
|
{{ $t("core.common.buttons.close") }}
|
|
|
|
</VButton>
|
|
|
|
</VSpace>
|
|
|
|
</template>
|
|
|
|
</VModal>
|
|
|
|
</template>
|