import React, { useEffect, useCallback } from "react"; import { Pagination, Row as BRow, Col as BCol, Spinner } from "react-bootstrap"; import { useTable, HeaderGroup, // Column, Row, Cell, usePagination, useAsyncDebounce, } from "react-table"; import { TransactionDetails } from "src/typings/api"; import { DsBlockObj, TxBlockObj } from "@zilliqa-js/core/src/types"; import "./ViewAllTable.css"; interface IViewAllTableParams { // TODO: Work out the correct type to put here columns: any; //Array>; data: T[]; isLoading: boolean; fetchData: ({ pageIndex }: { pageIndex: number }) => void; pageCount: number; } const ViewAllTable: React.FC< IViewAllTableParams > = ({ columns, data, isLoading, fetchData, pageCount: controlledPageCount, }) => { const { getTableProps, getTableBodyProps, headerGroups, prepareRow, page, canPreviousPage, canNextPage, pageCount, gotoPage, nextPage, previousPage, // Get the state from the instance state: { pageIndex }, } = useTable( { columns, data, initialState: { pageIndex: 0 }, manualPagination: true, pageCount: controlledPageCount, }, usePagination ); const fetchDataDebounce = useAsyncDebounce(fetchData, 300); useEffect(() => { fetchDataDebounce({ pageIndex }); }, [pageIndex, fetchData]); const generatePagination = useCallback( (currentPage: number, pageCount: number, delta = 2) => { const separate = (a: number, b: number, isLower: boolean) => { const temp = b - a; if (temp === 0) return [a]; else if (temp === 1) return [a, b]; else if (temp === 2) return [a, a + 1, b]; else return [a, isLower ? -1 : -2, b]; }; return Array(delta * 2 + 1) .fill(0) .map((_, index) => currentPage - delta + index) .filter((page) => 0 < page && page <= pageCount) .flatMap((page, index, { length }) => { if (!index) { return separate(1, page, true); } if (index === length - 1) { return separate(page, pageCount, false); } return [page]; }); }, [] ); return ( <> {data.length === 0 ? null : ( Items Per Page: 10 )} previousPage()} disabled={!canPreviousPage} /> {generatePagination(pageIndex + 1, pageCount).map((page) => { if (page === -1) return ( gotoPage(pageIndex - 5)} /> ); else if (page === -2) return ( gotoPage(pageIndex + 5)} /> ); else if (page === pageIndex + 1) return ( {page} ); else return ( gotoPage(Number(page) - 1)} > {page} ); })} nextPage()} disabled={!canNextPage} />
{isLoading ? (
) : null} {headerGroups.map( ( headerGroup: HeaderGroup< DsBlockObj | TxBlockObj | TransactionDetails > ) => ( {headerGroup.headers.map((column) => ( ))} ) )} {page.map( (row: Row) => { prepareRow(row); return ( {row.cells.map( ( cell: Cell ) => { return ( ); } )} ); } )}
{column.render("Header")}
{cell.render("Cell")}
); }; export default ViewAllTable;