"use client"; import { getArticleById } from "@/services/article"; import { zodResolver } from "@hookform/resolvers/zod"; import { Button, Card, Chip, Input, Select, SelectItem, Selection, } from "@heroui/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(), }); const TypeId = [ { key: "1", label: "Article", }, { key: "2", label: "Magazine", }, ]; 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] = useState(""); const pathname = usePathname(); const splitPathname = pathname.split("/"); const id = splitPathname[splitPathname.length - 1]; const formOptions = { resolver: zodResolver(articleSchema) }; type MicroIssueSchema = z.infer; const { register, control, handleSubmit, setValue, formState: { errors }, } = useForm(formOptions); const editorConfig = { readonly: true, }; 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(String(res.data?.data?.typeId)); setSlug(res.data?.data?.slug); const tagsArray = res.data.data?.tags ? res.data.data.tags.split(",") : []; setTags(tagsArray); } 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, }; // 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}
{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)

); }