Replies: 1 comment
-
Solved like this: import dayjs from "dayjs";
import React, { useMemo } from "react";
import { GenericTable } from "../components/Tables";
import { useReadHitsHitsGetQuery } from '../service/scannerApi';
export default function HitsList() {
const defaultPageSize = 10;
// const { first_seen, last_seen, total_data, avg_resp_size, ip, isp, org, country, city, vendor } = props;
const columns = useMemo(() => [
{
Header: "First Seen",
accessor: "first_seen",
Cell: ({ cell: { value } }) => (dayjs(value).format())
},
{
Header: "Last Seen",
accessor: "last_seen",
Cell: ({ cell: { value } }) => (dayjs(value).format())
},
{
Header: "IP",
accessor: "ip"
},
{
Header: "ISP",
accessor: "isp"
},
{
Header: "Org",
accessor: "country"
},
{
Header: "City",
accessor: "city"
},
])
return (
<>
<GenericTable
columns={columns}
useData={useReadHitsHitsGetQuery}
defaultPageSize={defaultPageSize}
/>
</>
);
}; export const GenericTable = ({
columns,
useData,
defaultPageSize
}) => {
const [data, setData] = useState([])
const [loading, setLoading] = useState(true)
const [total, setTotal] = useState(0)
const [controlledPageCount, setPageCount] = useState(0)
const [recordsPerPage, setRecordsPerPage] = useState(defaultPageSize)
const {
getTableProps,
getTableBodyProps,
headerGroups,
prepareRow,
page,
canPreviousPage,
canNextPage,
pageOptions,
pageCount,
gotoPage,
nextPage,
previousPage,
state: {
pageIndex,
pageSize,
},
} = useTable(
{
columns,
data,
initialState: {
pageIndex: 0,
pageSize: recordsPerPage
},
manualPagination: true,
pageCount: controlledPageCount,
},
usePagination
)
const {
data: result,
isFetching,
isLoading
} = useData({ page: pageIndex + 1, size: pageSize })
useEffect(() => {
setLoading(isLoading || isFetching)
setData(!(isLoading || isFetching) ? result?.items : [])
setTotal(!(isLoading || isFetching) && result?.total)
setPageCount(!(isLoading || isFetching) &&
result?.total ? Math.floor(result.total / pageSize) : 0)
}, [result, pageIndex, pageSize])
return (
<Card border="light" className="table-wrapper table-responsive shadow-sm">
<Card.Body className="pt-0">
{loading ? (
<Spinner animation="grow" />
) : (
<Table
hover
className="user-table align-items-center"
{...getTableProps()}
>
<thead>
{
headerGroups.map(headerGroup => (
<tr {...headerGroup.getHeaderGroupProps()}>
{
headerGroup.headers.map(column => (
<th
className="border-bottom"
{...column.getHeaderProps()}
>
{
column.render('Header')}
</th>
))}
</tr>
))}
</thead>
<tbody {...getTableBodyProps()}>
{
page.map((row, i) => {
prepareRow(row)
return (
<tr {...row.getRowProps()}>
{
row.cells.map(cell => {
return (
<td {...cell.getCellProps()}>
{
cell.render('Cell')}
</td>
)
})}
</tr>
)
})}
</tbody>
</Table>
)}
<Card.Footer
className="px-3 border-0 d-lg-flex align-items-center justify-content-between">
<Pagination className="mb-2 mb-lg-0">
<Pagination.First
disabled={pageIndex === 0}
onClick={() => gotoPage(0)}
/>
<Pagination.Prev
disabled={!canPreviousPage}
onClick={() => previousPage()}
/>
<Pagination.Next
disabled={!canNextPage}
onClick={() => nextPage()}
/>
<Pagination.Last
disabled={pageIndex === pageCount}
onClick={() => gotoPage(pageCount - 1)}
/>
</Pagination>
<small className="fw-bold">
{total} records / page{" "}
<strong>{pageIndex + 1}</strong> of {pageOptions.length}
</small>
</Card.Footer>
</Card.Body>
</Card>
)
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
I'm trying to build a reusable, generic table based on react-table + redux-toolkit but I'm struggling with hooks that call hooks. I need re-fetch the data whenever
pageIndex
changes.I understand the reason behind this error, but in my use case I can't find a solution that doesn't rely on a callback.
Beta Was this translation helpful? Give feedback.
All reactions