Skip to content

Commit

Permalink
fix: fix pagination not working for the mantime tables
Browse files Browse the repository at this point in the history
Fixes #2187
  • Loading branch information
mainawycliffe authored and moshloop committed Aug 18, 2024
1 parent c415667 commit 058c24d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 60 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@tanstack/query-core": "^4.27.0",
"@tanstack/react-query": "^4.0.9",
"@tanstack/react-query-devtools": "^4.0.8",
"@tanstack/react-table": "^8.15.3",
"@tanstack/react-table": "^8.20.1",
"@tanstack/react-virtual": "^3.0.0-beta.30",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
Expand Down Expand Up @@ -220,4 +220,4 @@
"msw": {
"workerDirectory": "public"
}
}
}
6 changes: 2 additions & 4 deletions src/components/JobsHistory/JobsHistorySettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import JobsHistoryTable from "./JobsHistoryTable";
export default function JobsHistorySettingsPage() {
const [searchParams] = useSearchParams();

const pageIndex = parseInt(searchParams.get("pageIndex") ?? "0");
const pageSize = parseInt(searchParams.get("pageSize") ?? "150");
const pageSize = parseInt(searchParams.get("pageSize") ?? "50");

const { data, isLoading, refetch, isRefetching } =
useJobsHistoryForSettingQuery({
Expand Down Expand Up @@ -45,8 +44,7 @@ export default function JobsHistorySettingsPage() {
jobs={jobs ?? []}
isLoading={isLoading || isRefetching}
pageCount={pageCount}
pageIndex={pageIndex}
pageSize={pageSize}
totalJobHistoryItems={totalEntries}
/>
</div>
</SearchLayout>
Expand Down
43 changes: 8 additions & 35 deletions src/components/JobsHistory/JobsHistoryTable.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { PaginationOptions } from "@flanksource-ui/ui/DataTable";
import useReactTableSortState from "@flanksource-ui/ui/DataTable/Hooks/useReactTableSortState";
import MRTDataTable from "@flanksource-ui/ui/MRTDataTable/MRTDataTable";
import { useCallback, useMemo, useState } from "react";
import { useSearchParams } from "react-router-dom";
import { useCallback, useState } from "react";
import { JobsHistoryDetails } from "./JobsHistoryDetails";
import { JobsHistoryTableColumn as jobsHistoryTableColumn } from "./JobsHistoryTableColumn";

Expand Down Expand Up @@ -52,48 +49,20 @@ type JobsHistoryTableProps = {
jobs: JobHistory[];
isLoading?: boolean;
pageCount: number;
pageIndex: number;
pageSize: number;
hiddenColumns?: string[];
totalJobHistoryItems?: number;
};

export default function JobsHistoryTable({
jobs,
isLoading,
pageCount,
pageIndex,
pageSize,
hiddenColumns = []
hiddenColumns = [],
totalJobHistoryItems
}: JobsHistoryTableProps) {
const [params, setParams] = useSearchParams();
const [isModalOpen, setIsModalOpen] = useState(false);
const [selectedJob, setSelectedJob] = useState<JobHistory>();

const [tableSortByState, onSortByChanged] = useReactTableSortState();

const pagination: PaginationOptions = useMemo(() => {
return {
setPagination: (updater) => {
const newParams =
typeof updater === "function"
? updater({
pageIndex,
pageSize
})
: updater;
params.set("pageIndex", newParams.pageIndex.toString());
params.set("pageSize", newParams.pageSize.toString());
setParams(params);
},
pageIndex,
pageSize,
pageCount,
remote: true,
enable: true,
loading: isLoading
};
}, [pageIndex, pageSize, pageCount, isLoading, params, setParams]);

const onRowClick = useCallback(
(row: JobHistory) => {
const jobId = row.id;
Expand All @@ -114,6 +83,10 @@ export default function JobsHistoryTable({
isLoading={isLoading}
onRowClick={onRowClick}
enableServerSideSorting
enableServerSidePagination
manualPageCount={pageCount}
hiddenColumns={hiddenColumns}
totalRowCount={totalJobHistoryItems}
/>
{selectedJob && (
<JobsHistoryDetails
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ export function SchemaResourceJobsTab({
jobs={jobs ?? []}
isLoading={isLoading}
pageCount={pageCount}
pageIndex={pageIndex}
pageSize={pageSize}
totalJobHistoryItems={totalEntries}
hiddenColumns={["resource_id", "resource_type", "resource_name"]}
/>
)}
Expand Down
4 changes: 2 additions & 2 deletions src/ui/DataTable/Hooks/useReactTablePaginationState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export default function useReactTablePaginationState() {
const updated =
typeof param === "function"
? param({
pageIndex: pageIndex,
pageSize: pageSize
pageIndex: pageIndex ?? 0,
pageSize: pageSize ?? 50
})
: param;
params.set("pageIndex", updated.pageIndex.toString());
Expand Down
23 changes: 17 additions & 6 deletions src/ui/MRTDataTable/MRTDataTable.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { VisibilityState } from "@tanstack/react-table";
import {
MantineReactTable,
MRT_ColumnDef,
Expand All @@ -15,6 +16,12 @@ type MRTDataTableProps<T extends Record<string, any> = {}> = {
enableServerSideSorting?: boolean;
enableServerSidePagination?: boolean;
manualPageCount?: number;
hiddenColumns?: string[];
/**
* The total number of rows in the dataset. This is used for server-side
* pagination to determine the total number of pages.
*/
totalRowCount?: number;
};

export default function MRTDataTable<T extends Record<string, any> = {}>({
Expand All @@ -25,7 +32,9 @@ export default function MRTDataTable<T extends Record<string, any> = {}>({
disablePagination = false,
enableServerSideSorting = false,
enableServerSidePagination = false,
manualPageCount
manualPageCount,
totalRowCount,
hiddenColumns = []
}: MRTDataTableProps<T>) {
const { pageIndex, pageSize, setPageIndex } = useReactTablePaginationState();
const [sortState, setSortState] = useReactTableSortState();
Expand All @@ -50,6 +59,8 @@ export default function MRTDataTable<T extends Record<string, any> = {}>({
manualSorting: enableServerSideSorting,
manualPagination: enableServerSidePagination,
pageCount: manualPageCount,
rowCount: totalRowCount,
autoResetPageIndex: false,
onPaginationChange: setPageIndex,
onSortingChange: setSortState,
mantineTableBodyRowProps: ({ row }) => ({
Expand All @@ -75,15 +86,15 @@ export default function MRTDataTable<T extends Record<string, any> = {}>({
density: "xs",
pagination: {
pageIndex,
pageSize: pageSize
pageSize
},
sorting: sortState
},
initialState: {
pagination: {
pageIndex: 0,
pageSize: pageSize
}
columnVisibility: hiddenColumns.reduce((acc: VisibilityState, column) => {
acc[column] = false;
return acc;
}, {})
},
mantinePaginationProps: {
rowsPerPageOptions: ["50", "100", "200"]
Expand Down

0 comments on commit 058c24d

Please sign in to comment.