Skip to content

Commit

Permalink
Node: Fix job list pagination
Browse files Browse the repository at this point in the history
This fixes a regression introuced with the MRT port.

Signed-off-by: Zack Cerza <zack@redhat.com>
  • Loading branch information
zmc committed Mar 29, 2024
1 parent 249a7b5 commit 093f369
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
55 changes: 46 additions & 9 deletions src/components/JobList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ import { ReactNode } from "react";
import DescriptionIcon from "@mui/icons-material/Description";
import Typography from "@mui/material/Typography";
import Box from "@mui/material/Box";
import type {
DecodedValueMap,
QueryParamConfigMap,
SetQuery,
} from "use-query-params";
import {
useMaterialReactTable,
MaterialReactTable,
type MRT_ColumnDef,
type MRT_Row,
type MRT_PaginationState,
type MRT_Updater,
} from 'material-react-table';
import type { UseQueryResult } from "@tanstack/react-query";
import { type Theme } from "@mui/material/styles";
Expand All @@ -21,6 +28,8 @@ import useDefaultTableOptions from "../../lib/table";
import sentryIcon from "./assets/sentry.svg";


const DEFAULT_PAGE_SIZE = 25;

const columns: MRT_ColumnDef<Job>[] = [
{
header: "status",
Expand Down Expand Up @@ -196,18 +205,53 @@ function JobDetailPanel(props: JobDetailPanelProps): ReactNode {
};

type JobListProps = {
params: DecodedValueMap<QueryParamConfigMap>;
setter: SetQuery<QueryParamConfigMap>;
manualPagination: Boolean;
query: UseQueryResult<Run> | UseQueryResult<NodeJobs>;
sortMode?: "time" | "id";
}

export default function JobList({ query, sortMode }: JobListProps) {
export default function JobList({
query,
params,
setter,
manualPagination,
sortMode,
}: JobListProps) {
const options = useDefaultTableOptions<Job>();
const data = query.data?.jobs || [];
const onPaginationChange = (updater: MRT_Updater<MRT_PaginationState>) => {
if (!(updater instanceof Function)) return;
const newParams = updater({
pageSize: params.pageSize,
pageIndex: params.page,
});
setter({
page: newParams.pageIndex || 0,
pageSize: newParams.pageSize || DEFAULT_PAGE_SIZE,
});
};
options.state = {};
if (manualPagination === true) {
options.manualPagination = true;
options.rowCount = Infinity;
options.onPaginationChange = onPaginationChange;
options.state.pagination = {
pageIndex: params.page || 0,
pageSize: params.pageSize || DEFAULT_PAGE_SIZE,
};
} else {
options.initialState = {
pagination: { pageIndex: 0, pageSize: DEFAULT_PAGE_SIZE },
};
}
options.state.isLoading = query.isLoading || query.isFetching;
const table = useMaterialReactTable({
rowCount: data.length,
...options,
columns,
data: data,
rowCount: data.length,
enableFacetedValues: true,
initialState: {
...options.initialState,
Expand All @@ -217,20 +261,13 @@ export default function JobList({ query, sortMode }: JobListProps) {
duration: false,
waiting: false,
},
pagination: {
pageIndex: 0,
pageSize: 25,
},
sorting: [
{
id: sortMode === "time"? "started" : "job_id",
desc: true,
},
],
},
state: {
isLoading: query.isLoading || query.isFetching,
},
renderDetailPanel: JobDetailPanel,
muiTableBodyRowProps: ({row, isDetailPanel}) => {
if ( isDetailPanel ) {
Expand Down
12 changes: 9 additions & 3 deletions src/pages/Node/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { NodeParams } from "../../lib/paddles.d";
import { useNode, useNodeJobs } from "../../lib/paddles";

export default function Node() {
const [params, _] = useQueryParams({
const [params, setParams] = useQueryParams({
page: NumberParam,
pageSize: NumberParam,
});
Expand All @@ -23,7 +23,7 @@ export default function Node() {

if (jobsQuery === null) return <Typography>404</Typography>;
if (jobsQuery.isError) return null;

return (
<div>
<Helmet>
Expand All @@ -36,7 +36,13 @@ export default function Node() {
<div>
<div>
<NodeList query={detailsQuery} />
<JobList query={jobsQuery} sortMode={"time"} />
<JobList
query={jobsQuery}
params={params}
setter={setParams}
manualPagination={true}
sortMode={"time"}
/>
</div>
</div>
</div>
Expand Down

0 comments on commit 093f369

Please sign in to comment.