"use client"; import { BannerIcon, CloudUploadIcon, CreateIconIon, DeleteIcon, DotsYIcon, EyeIconMdi, SearchIcon, TimesIcon, } from "@/components/icons"; import { close, error, loading, success } from "@/config/swal"; import { deleteArticle, getArticleByCategory, getListArticle, } from "@/service/article"; import { Article } from "@/types/globals"; import { convertDateFormat } from "@/utils/global"; import { Button } from "@heroui/button"; import { Chip, ChipProps, Dropdown, DropdownItem, DropdownMenu, DropdownTrigger, Input, Modal, ModalBody, ModalContent, ModalFooter, ModalHeader, Pagination, Select, SelectItem, Spinner, Switch, Table, TableBody, TableCell, TableColumn, TableHeader, TableRow, Textarea, useDisclosure, } from "@heroui/react"; import Link from "next/link"; import { Fragment, Key, useCallback, useEffect, useState } from "react"; import Cookies from "js-cookie"; import * as z from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { Controller, useForm } from "react-hook-form"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; import { useDropzone } from "react-dropzone"; import Image from "next/image"; import { useRouter } from "next/navigation"; const columns = [ { name: "No", uid: "no" }, { name: "Nama", uid: "name" }, { name: "Email", uid: "email" }, { name: "Komentar", uid: "comment" }, { name: "Aksi", uid: "actions" }, ]; interface Category { id: number; title: string; } const createArticleSchema = z.object({ id: z.string().optional(), title: z.string().min(2, { message: "Judul harus diisi", }), url: z.string().min(2, { message: "Link harus diisi", }), description: z.string().min(2, { message: "Deskripsi harus diisi", }), file: z.string().optional(), }); export default function CommentTable(props: { triggerRefresh: boolean }) { const MySwal = withReactContent(Swal); const { isOpen, onOpen, onOpenChange, onClose } = useDisclosure(); const router = useRouter(); const [page, setPage] = useState(1); const [totalPage, setTotalPage] = useState(1); const [article, setArticle] = useState([]); const [showData, setShowData] = useState("10"); const [search, setSearch] = useState(""); const [categories, setCategoies] = useState([]); const [selectedCategories, setSelectedCategories] = useState([]); const [startDateValue, setStartDateValue] = useState({ startDate: null, endDate: null, }); const [isHeader, setIsHeader] = useState(false); const [files, setFiles] = useState([]); const formOptions = { resolver: zodResolver(createArticleSchema), defaultValues: { title: "", description: "", url: "", file: "" }, }; const { getRootProps, getInputProps } = useDropzone({ onDrop: (acceptedFiles) => { setFiles(acceptedFiles.map((file) => Object.assign(file))); }, maxFiles: 1, accept: { "image/*": [], }, }); type UserSettingSchema = z.infer; const { control, handleSubmit, setValue, formState: { errors }, } = useForm(formOptions); useEffect(() => { initState(); }, [ page, showData, startDateValue, selectedCategories, props.triggerRefresh, ]); useEffect(() => { getCategories(); }, []); async function getCategories() { const res = await getArticleByCategory(); const data = res?.data?.data; setCategoies(data); } const handleRemoveFile = (file: File) => { const uploadedFiles = files; const filtered = uploadedFiles.filter((i) => i.name !== file.name); setFiles([...filtered]); }; async function initState() { const req = { limit: showData, page: page, search: search, startDate: startDateValue.startDate === null ? "" : startDateValue.startDate, endDate: startDateValue.endDate === null ? "" : startDateValue.endDate, category: Array.from(selectedCategories).join(","), sort: "desc", sortBy: "created_at", }; const res = await getListArticle(req); getTableNumber(parseInt(showData), res.data?.data); setTotalPage(res?.data?.meta?.totalPage); } const getTableNumber = (limit: number, data: Article[]) => { if (data) { const startIndex = limit * (page - 1); let iterate = 0; const newData = data.map((value: any) => { iterate++; value.no = startIndex + iterate; return value; }); setArticle(newData); } }; async function doDelete(id: any) { // loading(); const resDelete = await deleteArticle(id); if (resDelete?.error) { error(resDelete.message); return false; } close(); success("Berhasil Hapus"); initState(); } const handleDelete = (id: any) => { MySwal.fire({ title: "Hapus Data", icon: "warning", showCancelButton: true, cancelButtonColor: "#3085d6", confirmButtonColor: "#d33", confirmButtonText: "Hapus", }).then((result) => { if (result.isConfirmed) { doDelete(id); } }); }; const onSubmit = async (values: z.infer) => { loading(); const formData = { title: values.title, description: values.description, isHeader: isHeader, url: values.url, }; console.log("dataas", formData); close(); // setRefresh(!refresh); // MySwal.fire({ // title: "Sukses", // icon: "success", // confirmButtonColor: "#3085d6", // confirmButtonText: "OK", // }).then((result) => { // if (result.isConfirmed) { // } // }); }; const openModal = async (id: number) => { // const res = await getCategoryById(Number(id)); // const data = res?.data?.data; // setValue("id", String(data?.id)); // setValue("title", data?.title); // setValue("description", data?.description); // setValue("url", data?.url); // setValue("file", data?.thumbnailUrl); onOpen(); }; const renderCell = useCallback( (comment: any, columnKey: Key) => { const cellValue = comment[columnKey as keyof any]; switch (columnKey) { case "url": return ( https://www.google.com/ ); case "actions": return (
{/* Detail */} router.push(`/admin/comment/review${comment.id}`) } > Review handleDelete(article.id)} > Delete
); default: return cellValue; } }, [article] ); let typingTimer: NodeJS.Timeout; const doneTypingInterval = 1500; const handleKeyUp = () => { clearTimeout(typingTimer); typingTimer = setTimeout(doneTyping, doneTypingInterval); }; const handleKeyDown = () => { clearTimeout(typingTimer); }; async function doneTyping() { initState(); } return ( <>

Pencarian

} type="text" onChange={(e) => setSearch(e.target.value)} onKeyUp={handleKeyUp} onKeyDown={handleKeyDown} />

Data

{(column) => ( {column.name} )} } > {(item: any) => ( {(columnKey) => ( {renderCell(item, columnKey)} )} )}
setPage(page)} />
); }