"use client"; import * as React from "react"; import { ColumnDef, ColumnFiltersState, PaginationState, SortingState, VisibilityState, flexRender, getCoreRowModel, getFilteredRowModel, getPaginationRowModel, getSortedRowModel, useReactTable, } from "@tanstack/react-table"; import { Input } from "@/components/ui/input"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { data } from "./data"; import TablePagination from "./table-pagination"; import { Button } from "@/components/ui/button"; import { format } from "date-fns"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { ChevronLeft, ChevronRight, Eye, MoreVertical, Trash2, } from "lucide-react"; import { title } from "process"; import { getCookiesDecrypt } from "@/lib/utils"; import { listDataAudio, listDataImage, listDataVideo, listSPIT, } from "@/service/content/content"; import { pages } from "next/dist/build/templates/app-page"; export type CompanyData = { no: number; title: string; categoryName: string; createdAt: string; creatorGroup: string; publishedOn: string; isPublish: any; isPublishOnPolda: any; isDone: string; }; export const columns: ColumnDef[] = [ { accessorKey: "no", header: "No", cell: ({ row }) => (

{row.getValue("no")}

), }, { accessorKey: "title", header: "Judul", cell: ({ row }) => (

{row.getValue("title")}

), }, { accessorKey: "categoryName", header: "Kategori", cell: ({ row }) => ( {row.getValue("categoryName")} ), }, { accessorKey: "createdAt", header: "Tanggal Unggah", cell: ({ row }) => { const createdAt = row.getValue("createdAt") as | string | number | undefined; const formattedDate = createdAt && !isNaN(new Date(createdAt).getTime()) ? format(new Date(createdAt), "dd-MM-yyyy HH:mm:ss") : "-"; return {formattedDate}; }, }, { accessorKey: "creatorGroup", header: "Sumber ", cell: ({ row }) => ( {row.getValue("creatorGroup")} ), }, { accessorKey: "publishedOn", header: "Penempatan File", cell: ({ row }) => { const isPublish = row.original.isPublish; const isPublishOnPolda = row.original.isPublishOnPolda; let displayText = "-"; if (isPublish && !isPublishOnPolda) { displayText = "Mabes"; } else if (isPublish && isPublishOnPolda) { displayText = "Mabes & Polda"; } else if (!isPublish && isPublishOnPolda) { displayText = "Polda"; } return (
{displayText}
); }, }, { accessorKey: "isDone", header: "Status", cell: ({ row }) => { const isDone = row.getValue("isDone"); return (
); }, }, { id: "actions", accessorKey: "action", header: "Actions", enableHiding: false, cell: ({ row }) => { return ( View Delete ); }, }, ]; const TableSPIT = () => { const [spitTable, setSpitTable] = React.useState([]); const [sorting, setSorting] = React.useState([]); const [columnFilters, setColumnFilters] = React.useState( [] ); const [columnVisibility, setColumnVisibility] = React.useState({}); const [rowSelection, setRowSelection] = React.useState({}); const [pagination, setPagination] = React.useState({ pageIndex: 0, // Halaman pertama pageSize: 10, // Jumlah baris per halaman }); const [page, setPage] = React.useState(1); // Halaman aktif const [totalPage, setTotalPage] = React.useState(1); // Total halaman const [limit, setLimit] = React.useState(6); // Jumlah baris per halaman const [search, setSearch] = React.useState(title); const userId = getCookiesDecrypt("uie"); const userLevelId = getCookiesDecrypt("ulie"); const [categories, setCategories] = React.useState(); const [categoryFilter, setCategoryFilter] = React.useState([]); const [statusFilter, setStatusFilter] = React.useState([1, 2]); const [startDateString, setStartDateString] = React.useState(""); const [endDateString, setEndDateString] = React.useState(""); const [filterByCreator, setFilterByCreator] = React.useState(""); const [filterBySource, setFilterBySource] = React.useState(""); const roleId = getCookiesDecrypt("urie"); const table = useReactTable({ data: spitTable, columns, onSortingChange: setSorting, onColumnFiltersChange: setColumnFilters, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), getSortedRowModel: getSortedRowModel(), getFilteredRowModel: getFilteredRowModel(), onColumnVisibilityChange: setColumnVisibility, onRowSelectionChange: setRowSelection, state: { sorting, columnFilters, columnVisibility, rowSelection, }, }); React.useEffect(() => { initState(); }, [page, limit]); async function initState() { try { const isForSelf = Number(roleId) == 4; let isPublish; if (statusFilter.length > 1) { isPublish = ""; } else if (statusFilter.length === 1) { if (statusFilter.includes(1)) { isPublish = false; } else { isPublish = true; } } else { isPublish = undefined; } const res = await listSPIT(pages, limit, search, isPublish); const data = res.data.data.content.map((item: any, index: number) => ({ no: (page - 1) * limit + index + 1, title: item.title, categoryName: item.categoryName, creatorGroup: item.creatorGroup, assignmentType: item.assignmentType?.name || "-", createdAt: item.createdAt, isDone: item.isDone, publishedOn: item.publishedOn, isPublish: item.isPublish, isPublishOnPolda: item.isPublishOnPolda, })); setSpitTable(data); setTotalPage(res.data.totalPages); } catch (error) { console.error("Error fetching tasks:", error); } } return (
) => table.getColumn("status")?.setFilterValue(event.target.value) } className="max-w-sm " />
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext() )} ))} ))} {table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( {flexRender(cell.column.columnDef.cell, cell.getContext())} ))} )) ) : ( No results. )}
{table.getPageOptions().map((page, pageIndex) => ( ))}
); }; export default TableSPIT;