"use client"; import { useEffect, useState, useCallback } from "react"; import SortableTable, { Pagination, SortableTableProps } from "./Table"; import { PrismaClient } from "@repo/db"; import { getData } from "./pagiantedTableActions"; interface PaginatedTableProps extends Omit, "data"> { prismaModel: keyof PrismaClient; rowsPerPage?: number; showEditButton?: boolean; searchFields: string[]; } export function PaginatedTable({ prismaModel, rowsPerPage = 10, showEditButton = false, searchFields, ...restProps }: PaginatedTableProps) { const [data, setData] = useState([]); const [page, setPage] = useState(0); const [total, setTotal] = useState(0); const [searchTerm, setSearchTerm] = useState(""); const [debouncedSearchTerm, setDebouncedSearchTerm] = useState(searchTerm); const debounce = (func: Function, delay: number) => { let timer: NodeJS.Timeout; return (...args: any[]) => { clearTimeout(timer); timer = setTimeout(() => func(...args), delay); }; }; const handleSearchChange = useCallback( debounce((value: string) => { setDebouncedSearchTerm(value); }, 500), [] ); useEffect(() => { getData( prismaModel, rowsPerPage, page * rowsPerPage, debouncedSearchTerm, searchFields ).then((result) => { if (result) { setData(result.data); setTotal(result.total); } }); }, [page, debouncedSearchTerm]); return (
{searchFields.length > 0 && (
{ setSearchTerm(e.target.value); handleSearchChange(e.target.value); }} className="input input-bordered w-full max-w-xs justify-end" />
)}
); }