"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 { 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 { listDataImage } from "@/service/content/content"; import page from "../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 TableImage = () => { const [sorting, setSorting] = React.useState([]); const [columnFilters, setColumnFilters] = React.useState( [] ); const [columnVisibility, setColumnVisibility] = React.useState({}); const [rowSelection, setRowSelection] = React.useState({}); const [imageTable, setImageTable] = React.useState([]); const [pagination, setPagination] = React.useState({ pageIndex: 0, // Halaman pertama pageSize: 10, // Jumlah baris per halaman }); const [totalPage, setTotalPage] = React.useState(10); const [limit, setLimit] = React.useState(10); // Jumlah baris per halaman const [search, setSearch] = React.useState(""); const userId = getCookiesDecrypt("uie"); const userLevelId = getCookiesDecrypt("ulie"); const [categories, setCategories] = React.useState(); const [categoryFilter, setCategoryFilter] = React.useState([]); const [statusFilter, setStatusFilter] = React.useState([]); 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: imageTable, 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(); }, [limit]); async function initState() { try { const isForSelf = Number(roleId) === 4; const res = await listDataImage( pagination.pageSize, // Ambil nilai dari pagination.pageSize pagination.pageIndex + 1, // API sering menggunakan page 1-based isForSelf, !isForSelf, categoryFilter?.sort().join(","), statusFilter?.sort().join(",").includes("1") ? "1,2" : statusFilter?.sort().join(","), statusFilter?.sort().join(",").includes("1") ? userLevelId : "", filterByCreator, filterBySource, startDateString, endDateString ); setupData(res.data?.data); } catch (error) { console.error("Error fetching tasks:", error); } } function setupData(rawData: { content: any[]; totalPages: number; totalElements: number; }) { if (rawData?.content) { const data: CompanyData[] = rawData.content.map((item, index) => ({ no: pagination.pageIndex * pagination.pageSize + index + 1, title: item.title, categoryName: item.categoryName, creatorGroup: item.creatorGroup, createdAt: item.createdAt, isDone: item.isDone, publishedOn: item.publishedOn, isPublish: item.isPublish, isPublishOnPolda: item.isPublishOnPolda, })); setImageTable(data); setTotalPage(rawData.totalPages); console.log(data, "dataImage"); } } 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 TableImage;