"use client"; import { BannerIcon, CreateIconIon, DeleteIcon, DotsYIcon, EyeIconMdi, SearchIcon, } from "@/components/icons"; import { error, 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, Pagination, Select, SelectItem, Spinner, Table, TableBody, TableCell, TableColumn, TableHeader, TableRow, } from "@heroui/react"; import Link from "next/link"; import { Key, useCallback, useEffect, useState } from "react"; import Datepicker from "react-tailwindcss-datepicker"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; import Cookies from "js-cookie"; const columns = [ { name: "No", uid: "no" }, { name: "Judul", uid: "title" }, { name: "Banner", uid: "isBanner" }, { name: "Kategori", uid: "category" }, { name: "Tanggal Unggah", uid: "createdAt" }, { name: "Kreator", uid: "createdByName" }, { name: "Status", uid: "isPublish" }, { name: "Aksi", uid: "actions" }, ]; interface Category { id: number; title: string; } type ArticleData = Article & { no: number; createdAt: string; categories: Category[]; }; export default function ArticleTable() { const MySwal = withReactContent(Swal); const username = Cookies.get("username"); 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, }); useEffect(() => { initState(); }, [page, showData, startDateValue, selectedCategories]); useEffect(() => { getCategories(); }, []); async function getCategories() { const res = await getArticleByCategory(); const data = res?.data?.data; setCategoies(data); } 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 renderCell = useCallback( (article: any, columnKey: Key) => { const cellValue = article[columnKey as keyof any]; switch (columnKey) { case "isPublish": return ( // //
// {article.status} //
//

{article.isPublish ? "Publish" : "Draft"}

); case "isBanner": return

{article.isBanner ? "Ya" : "Tidak"}

; case "createdAt": return

{convertDateFormat(article.createdAt)}

; case "category": return (

{article?.categories?.map((list: any) => list.title).join(", ") + " "}

); case "actions": return (
Detail Edit handleDelete(article.id)} className={username === "admin-mabes" ? "" : "hidden"} > {username === "admin-mabes" && ( <> {article?.isBanner ? "Hapus dari Banner" : "Jadikan Banner"} )} 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

Kategori

{/*

Tanggal

setStartDateValue(e)} inputClassName="z-50 w-full text-sm bg-transparent border-1 border-gray-200 px-2 py-[6px] rounded-xl h-[40px] text-gray-600 dark:text-gray-300" />
*/}
{(column) => ( {column.name} )} } > {(item: any) => ( {(columnKey) => ( {renderCell(item, columnKey)} )} )}
setPage(page)} />
); }