From db89e73d112eb8481388a021d47e25a47e130ade Mon Sep 17 00:00:00 2001 From: Rama Priyanto Date: Fri, 17 Jan 2025 17:23:54 +0700 Subject: [PATCH] fix:article create edit --- app/(admin)/admin/article/create/page.tsx | 2 +- .../admin/article/detail/[id]/page.tsx | 10 +- app/(admin)/admin/article/edit/[id]/page.tsx | 13 +- .../form/article/create-article-form.tsx | 290 ++++++---- components/form/article/edit-article-form.tsx | 526 ++++++++++++++++++ .../form/article/generate-ai-single-form.tsx | 396 +++++++++++++ .../form/article/get-seo-score-form.tsx | 139 +++++ components/form/form-detail-article.tsx | 352 +----------- components/form/form-edit-article.tsx | 1 - service/article.ts | 4 +- 10 files changed, 1259 insertions(+), 474 deletions(-) create mode 100644 components/form/article/edit-article-form.tsx create mode 100644 components/form/article/generate-ai-single-form.tsx create mode 100644 components/form/article/get-seo-score-form.tsx diff --git a/app/(admin)/admin/article/create/page.tsx b/app/(admin)/admin/article/create/page.tsx index 81f519c..3c625d1 100644 --- a/app/(admin)/admin/article/create/page.tsx +++ b/app/(admin)/admin/article/create/page.tsx @@ -4,7 +4,7 @@ import { Card } from "@nextui-org/react"; export default function CreateArticle() { return ( -
+
{/* */}
diff --git a/app/(admin)/admin/article/detail/[id]/page.tsx b/app/(admin)/admin/article/detail/[id]/page.tsx index e9e3476..9b5cbcd 100644 --- a/app/(admin)/admin/article/detail/[id]/page.tsx +++ b/app/(admin)/admin/article/detail/[id]/page.tsx @@ -1,10 +1,10 @@ -import FormDetailArticle from "@/components/form/form-detail-article"; -import { Card } from "@nextui-org/react"; +import EditArticleForm from "@/components/form/article/edit-article-form"; export default function DetailArticlePage() { return ( - - - +
+ {/* */} + +
); } diff --git a/app/(admin)/admin/article/edit/[id]/page.tsx b/app/(admin)/admin/article/edit/[id]/page.tsx index c8ad851..3586d72 100644 --- a/app/(admin)/admin/article/edit/[id]/page.tsx +++ b/app/(admin)/admin/article/edit/[id]/page.tsx @@ -1,10 +1,9 @@ -import FormUpdateArticle from '@/components/form/form-edit-article' -import { Card } from '@nextui-org/react' +import EditArticleForm from "@/components/form/article/edit-article-form"; export default function UpdateArticlePage() { - return ( - - - - ) + return ( +
+ +
+ ); } diff --git a/components/form/article/create-article-form.tsx b/components/form/article/create-article-form.tsx index ce1400c..fed6890 100644 --- a/components/form/article/create-article-form.tsx +++ b/components/form/article/create-article-form.tsx @@ -13,10 +13,15 @@ import { Button } from "@nextui-org/button"; import { CloudUploadIcon, TimesIcon } from "@/components/icons"; import Image from "next/image"; import { Switch } from "@nextui-org/switch"; -import { getArticleByCategory } from "@/service/article"; +import { createArticle, getArticleByCategory } from "@/service/article"; import ReactSelect from "react-select"; import makeAnimated from "react-select/animated"; import { Chip } from "@nextui-org/react"; +import GenerateSingleArticleForm from "./generate-ai-single-form"; +import { htmlToString } from "@/utils/global"; +import { close, error, loading } from "@/config/swal"; +import { useRouter } from "next/navigation"; +import Link from "next/link"; // const CustomEditor = dynamic( // () => { @@ -47,9 +52,6 @@ const createArticleSchema = z.object({ slug: z.string().min(2, { message: "Slug harus diisi", }), - categoryId: z.string().min(2, { - message: "Pilih Kategori", - }), description: z.string().min(2, { message: "Deskripsi harus diisi", }), @@ -64,6 +66,7 @@ const createArticleSchema = z.object({ export default function CreateArticleForm() { const animatedComponents = makeAnimated(); const MySwal = withReactContent(Swal); + const router = useRouter(); const editor = useRef(null); const [files, setFiles] = useState([]); const [useAi, setUseAI] = useState(false); @@ -132,10 +135,50 @@ export default function CreateArticleForm() { }); }; + function removeImgTags(htmlString: string) { + const parser = new DOMParser(); + const doc = parser.parseFromString(String(htmlString), "text/html"); + + const images = doc.querySelectorAll("img"); + images.forEach((img) => img.remove()); + return doc.body.innerHTML; + } + const save = async (values: z.infer) => { - console.log("values"); + loading(); + const formData = { + title: values.title, + typeId: 1, + slug: values.slug, + categoryId: values.category.map((item) => item.id).join(","), + tags: values.tags.join(","), + description: htmlToString(removeImgTags(values.description)), + htmlDescription: removeImgTags(values.description), + }; + + const response = await createArticle(formData); + + if (response?.error) { + error(response.message); + return false; + } + close(); + successSubmit("/admin/article"); }; + function successSubmit(redirect: string) { + MySwal.fire({ + title: "Sukses", + icon: "success", + confirmButtonColor: "#3085d6", + confirmButtonText: "OK", + }).then((result) => { + if (result.isConfirmed) { + router.push(redirect); + } + }); + } + const watchTitle = watch("title"); const generateSlug = (title: string) => { return title @@ -219,7 +262,7 @@ export default function CreateArticleForm() { value={value} onChange={onChange} labelPlacement="outside" - className="w-full mb-3" + className="w-full " classNames={{ inputWrapper: [ "border-1 rounded-lg", @@ -231,9 +274,9 @@ export default function CreateArticleForm() { )} /> {errors?.title && ( -

{errors.title?.message}

+

{errors.title?.message}

)} -

Slug

+

Slug

{errors?.slug && ( -

{errors.slug?.message}

+

{errors.slug?.message}

)} - +

Bantuan AI

-

Deskripsi

+ {useAi && ( + setValue("description", data)} + /> + )} + +

Deskripsi

)} /> + {errors?.description && ( +

+ {errors.description?.message} +

+ )} -

File Media

+

File Media

- {/* Drop files here or click to upload. */} Tarik file disini atau klik untuk upload.

@@ -313,106 +366,123 @@ export default function CreateArticleForm() { ) : null}
-
-

Thubmnail

- -

Kategori

- ( - - "!rounded-xl bg-white !border-1 !border-gray-200", - }} - classNamePrefix="select" - onChange={onChange} - closeMenuOnSelect={false} - components={animatedComponents} - isClearable={true} - isSearchable={true} - isMulti={true} - placeholder="Kategori..." - name="sub-module" - options={listCategory} - /> +
+
+

Thubmnail

+ +

Kategori

+ ( + + "!rounded-lg bg-white !border-1 !border-gray-200 dark:!border-stone-500", + }} + classNamePrefix="select" + onChange={onChange} + closeMenuOnSelect={false} + components={animatedComponents} + isClearable={true} + isSearchable={true} + isMulti={true} + placeholder="Kategori..." + name="sub-module" + options={listCategory} + /> + )} + /> + {errors?.category && ( +

+ {errors.category?.message} +

)} - /> - {errors?.category && ( -

{errors.category?.message}

- )} -

Tags

- ( -