From a4f94e1c3a698f825e3033105e2ad591b846e83a Mon Sep 17 00:00:00 2001 From: Rama Priyanto Date: Mon, 23 Dec 2024 00:58:20 +0700 Subject: [PATCH] little update --- components/form/form-edit-article.tsx | 566 +++++++++++++------------- 1 file changed, 284 insertions(+), 282 deletions(-) diff --git a/components/form/form-edit-article.tsx b/components/form/form-edit-article.tsx index 9d50f69..adde604 100644 --- a/components/form/form-edit-article.tsx +++ b/components/form/form-edit-article.tsx @@ -1,298 +1,300 @@ -'use client' -import { getArticleById } from '@/service/article'; -import { zodResolver } from '@hookform/resolvers/zod'; -import { Button, Card, Chip, Input, Select, SelectItem, Selection } from '@nextui-org/react'; -import JoditEditor from 'jodit-react'; -import Link from 'next/link'; -import { usePathname } from 'next/navigation'; -import React, { useEffect, useRef, useState } from 'react'; -import { useForm } from 'react-hook-form'; -import Swal from 'sweetalert2'; -import withReactContent from 'sweetalert2-react-content'; +"use client"; +import { getArticleById } from "@/service/article"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { + Button, + Card, + Chip, + Input, + Select, + SelectItem, + Selection, +} from "@nextui-org/react"; +import JoditEditor from "jodit-react"; +import Link from "next/link"; +import { usePathname } from "next/navigation"; +import React, { useEffect, useRef, useState } from "react"; +import { useForm } from "react-hook-form"; +import Swal from "sweetalert2"; +import withReactContent from "sweetalert2-react-content"; import * as z from "zod"; const articleSchema = z.object({ - title: z.string().min(1, { message: "Required" }), - article: z.string().min(1, { message: "Required" }), - slug: z.string().min(1, { message: "Required" }), - tags: z.string().min(0, { message: "Required" }).optional(), - description: z.string().min(1, { message: "Required" }).optional(), - + title: z.string().min(1, { message: "Required" }), + article: z.string().min(1, { message: "Required" }), + slug: z.string().min(1, { message: "Required" }), + tags: z.string().min(0, { message: "Required" }).optional(), + description: z.string().min(1, { message: "Required" }).optional(), }); export default function FormUpdateArticle() { - // const [id, setId] = useState(); - const [title, setTitle] = useState(""); - const [slug, setSlug] = useState(""); - const [tags, setTags] = useState([]); - const [newTags, setNewTags] = useState(""); - const editor = useRef(null); - const [content, setContent] = useState(''); - const MySwal = withReactContent(Swal); - const [article, setArticle] = useState(); - const [typeId, setTypeId] = React.useState(new Set([])); - const pathname = usePathname(); - const splitPathname = pathname.split('/'); - const id = splitPathname[splitPathname.length - 1]; - console.log(id, "pathnamesplit") + // const [id, setId] = useState(); + const [title, setTitle] = useState(""); + const [slug, setSlug] = useState(""); + const [tags, setTags] = useState([]); + const [newTags, setNewTags] = useState(""); + const editor = useRef(null); + const [content, setContent] = useState(""); + const MySwal = withReactContent(Swal); + const [article, setArticle] = useState(); + const [typeId, setTypeId] = useState(""); + const pathname = usePathname(); + const splitPathname = pathname.split("/"); + const id = splitPathname[splitPathname.length - 1]; + console.log(id, "pathnamesplit"); - const formOptions = { resolver: zodResolver(articleSchema) }; - type MicroIssueSchema = z.infer; - const { - register, - control, - handleSubmit, - setValue, - formState: { errors }, - } = useForm(formOptions); + const formOptions = { resolver: zodResolver(articleSchema) }; + type MicroIssueSchema = z.infer; + const { + register, + control, + handleSubmit, + setValue, + formState: { errors }, + } = useForm(formOptions); - const editorConfig = { - readonly: true, + const editorConfig = { + readonly: true, + }; + + const TypeId = [ + { + key: 1, + label: "Article", + }, + { + key: 2, + label: "Magazine", + }, + ]; + + const CategoryArticle = [ + { + key: 1, + label: "Article", + }, + { + key: 2, + label: "Magazine", + }, + ]; + + const handleClose = (tagsToRemove: string) => { + setTags(tags.filter((tag) => tag !== tagsToRemove)); + if (tags.length === 1) { + setTags([]); + } + }; + + const handleAddTags = (e: any) => { + if (newTags.trim() !== "") { + setTags([...tags, newTags.trim()]); + setNewTags(""); + e.preventDefault(); + } + }; + + const handleKeyDown = (event: any) => { + if (event.key === "Enter") { + handleAddTags(event); + } + }; + + useEffect(() => { + async function initState() { + const res = await getArticleById(id); + setArticle(res.data?.data); + setTitle(res.data?.data?.title); + setTypeId(res.data?.data?.typeId); + setSlug(res.data?.data?.slug); + const tagsArray = res.data.data?.tags + ? res.data.data.tags.split(",") + : []; + setTags(tagsArray); + + console.log("Data Aritcle", res.data?.data); } - const TypeId = [ - { - key: 1, - label: "Article" - }, - { - key: 2, - label: "Magazine" - }, - ] + initState(); + }, []); - const CategoryArticle = [ - { - key: 1, - label: "Article" - }, - { - key: 2, - label: "Magazine" - }, - ] - - const handleClose = (tagsToRemove: string) => { - setTags(tags.filter((tag) => tag !== tagsToRemove)); - if (tags.length === 1) { - setTags([]); - } + async function save(data: any) { + const formData = { + id: id, + title: title, + typeId: parseInt(String(Array.from(article)[0])), + slug: slug, + tags: tags.join(","), + description: content, + htmlDescription: content, }; - const handleAddTags = (e: any) => { - if (newTags.trim() !== "") { - setTags([...tags, newTags.trim()]); - setNewTags(""); - e.preventDefault(); - } - }; + console.log("Form Data:", formData); + // const response = await createArticle(formData); - const handleKeyDown = (event: any) => { - if (event.key === "Enter") { - handleAddTags(event); - } - }; + // if (response?.error) { + // error(response.message); + // return false; + // } + } - - useEffect(() => { - async function initState() { - const res = await getArticleById(id); - setArticle(res.data?.data); - setTitle(res.data?.data?.title) - setTypeId(res.data?.data?.typeId) - setSlug(res.data?.data?.slug) - const tagsArray = res.data.data?.tags ? res.data.data.tags.split(",") : []; - setTags(tagsArray); - - console.log("Data Aritcle", res.data?.data); - } - - initState(); - }, []); - - - async function save(data: any,) { - const formData = { - id: id, - title: title, - typeId: parseInt(String(Array.from(article)[0])), - slug: slug, - tags: tags.join(','), - description: content, - htmlDescription: content - }; - - console.log("Form Data:", formData); - // const response = await createArticle(formData); - - // if (response?.error) { - // error(response.message); - // return false; - // } - }; - - async function onSubmit(data: any) { - MySwal.fire({ - title: "Simpan Data", - text: "", - icon: "warning", - showCancelButton: true, - cancelButtonColor: "#d33", - confirmButtonColor: "#3085d6", - confirmButtonText: "Simpan", - }).then((result) => { - if (result.isConfirmed) { - save(data); - } - }); - } - return ( -
-
- -
- -
- {(title?.length > 0 && errors.title) && errors.title.message} -
-
-
- -
- {errors.article?.message} -
- {/*

{article}

*/} -
-
- -
- {(slug?.length > 0 && errors.slug) && errors.slug.message} -
-
-
- setNewTags(e.target.value)} - onKeyDown={handleKeyDown} - placeholder="Tambahkan tag baru dan tekan Enter" - /> -
- {(tags.length === 0 && errors.tags) && errors.tags.message} -
-
- {tags.map((tag, index) => ( - handleClose(tag)}> - {tag} - - ))} -
-
-
-

Description

- setContent(newContent)} - className="dark:text-black" - - /> -
- {(content.length === 0 && errors.description) && errors.description.message} -
-
-
-

Attachment (Opsional)

-
- -
-
-
- - - - -
-
-
-
- ) + async function onSubmit(data: any) { + MySwal.fire({ + title: "Simpan Data", + text: "", + icon: "warning", + showCancelButton: true, + cancelButtonColor: "#d33", + confirmButtonColor: "#3085d6", + confirmButtonText: "Simpan", + }).then((result) => { + if (result.isConfirmed) { + save(data); + } + }); + } + return ( +
+
+ +
+ +
+ {title?.length > 0 && errors.title && errors.title.message} +
+
+
+ +
+ {errors.article?.message} +
+
+
+ +
+ {slug?.length > 0 && errors.slug && errors.slug.message} +
+
+
+ setNewTags(e.target.value)} + onKeyDown={handleKeyDown} + placeholder="Tambahkan tag baru dan tekan Enter" + /> +
+ {tags.length === 0 && errors.tags && errors.tags.message} +
+
+ {tags.map((tag, index) => ( + handleClose(tag)} + > + {tag} + + ))} +
+
+
+

Description

+ setContent(newContent)} + className="dark:text-black" + /> +
+ {content.length === 0 && + errors.description && + errors.description.message} +
+
+
+

Attachment (Opsional)

+
+ +
+
+
+ + + + +
+
+
+
+ ); }