Skip to content

Commit

Permalink
feat: use listingSize param in tree and hardware
Browse files Browse the repository at this point in the history
- receive param from url and pass to Pagination component
- create function to navigate with the selected pageSize

Close #860
  • Loading branch information
Francisco2002 committed Feb 4, 2025
1 parent 541254d commit d09dc2c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
34 changes: 28 additions & 6 deletions dashboard/src/components/TreeListingPage/TreeTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import {
useReactTable,
} from '@tanstack/react-table';

import { memo, useMemo, useState } from 'react';
import { memo, useCallback, useMemo, useState } from 'react';

import { FormattedMessage } from 'react-intl';

import type { LinkProps } from '@tanstack/react-router';
import { Link, useSearch } from '@tanstack/react-router';
import { Link, useNavigate, useSearch } from '@tanstack/react-router';

import { TooltipDateTime } from '@/components/TooltipDateTime';

Expand Down Expand Up @@ -322,11 +322,16 @@ interface ITreeTable {
}

export function TreeTable({ treeTableRows }: ITreeTable): JSX.Element {
const { origin: unsafeOrigin, listingSize } = useSearch({ strict: false });
const navigate = useNavigate({ from: '/tree' });

const [sorting, setSorting] = useState<SortingState>([]);
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);
const { pagination, paginationUpdater } = usePaginationState('treeListing');
const { pagination, paginationUpdater } = usePaginationState(
'treeListing',
listingSize,
);

const { origin: unsafeOrigin } = useSearch({ strict: false });
const origin = zOrigin.parse(unsafeOrigin);

const columns = useMemo(() => getColumns(origin), [origin]);
Expand Down Expand Up @@ -422,6 +427,15 @@ export function TreeTable({ treeTableRows }: ITreeTable): JSX.Element {
);
}, [modelRows, columns.length, origin]);

const navigateWithPageSize = useCallback(
(pageSize: number) => {
navigate({
search: s => ({ ...s, listingSize: pageSize }),
});
},
[navigate],
);

return (
<div className="flex flex-col gap-6 pb-4">
<div className="flex items-center justify-between gap-4">
Expand All @@ -433,13 +447,21 @@ export function TreeTable({ treeTableRows }: ITreeTable): JSX.Element {
</span>
<div className="flex items-center justify-between gap-10">
<MemoizedInputTime />
<PaginationInfo table={table} intlLabel="global.trees" />
<PaginationInfo
table={table}
intlLabel="global.trees"
onPaginationChange={navigateWithPageSize}
/>
</div>
</div>
<BaseTable headerComponents={tableHeaders}>
<TableBody>{tableBody}</TableBody>
</BaseTable>
<PaginationInfo table={table} intlLabel="global.trees" />
<PaginationInfo
table={table}
intlLabel="global.trees"
onPaginationChange={navigateWithPageSize}
/>
</div>
);
}
34 changes: 28 additions & 6 deletions dashboard/src/pages/Hardware/HardwareTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
useReactTable,
} from '@tanstack/react-table';

import { memo, useMemo, useState } from 'react';
import { memo, useCallback, useMemo, useState } from 'react';

import { FormattedMessage } from 'react-intl';

import type { LinkProps } from '@tanstack/react-router';
import { useNavigate, useSearch, type LinkProps } from '@tanstack/react-router';

import BaseTable, { TableHead } from '@/components/Table/BaseTable';

Expand Down Expand Up @@ -302,10 +302,15 @@ export function HardwareTable({
startTimestampInSeconds,
endTimestampInSeconds,
}: ITreeTable): JSX.Element {
const { listingSize } = useSearch({ strict: false });
const navigate = useNavigate({ from: '/hardware' });

const [sorting, setSorting] = useState<SortingState>([]);
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);
const { pagination, paginationUpdater } =
usePaginationState('hardwareListing');
const { pagination, paginationUpdater } = usePaginationState(
'hardwareListing',
listingSize,
);

const data = useMemo(() => {
return treeTableRows;
Expand Down Expand Up @@ -394,6 +399,15 @@ export function HardwareTable({

const MemoizedInputTime = memo(InputTime);

const navigateWithPageSize = useCallback(
(pageSize: number) => {
navigate({
search: s => ({ ...s, listingSize: pageSize }),
});
},
[navigate],
);

return (
<div className="flex flex-col gap-6 pb-4">
<div className="flex items-center justify-between gap-4">
Expand All @@ -405,13 +419,21 @@ export function HardwareTable({
</span>
<div className="flex items-center justify-between gap-10">
<MemoizedInputTime />
<PaginationInfo table={table} intlLabel="global.hardwares" />
<PaginationInfo
table={table}
intlLabel="global.hardwares"
onPaginationChange={navigateWithPageSize}
/>
</div>
</div>
<BaseTable headerComponents={tableHeaders}>
<TableBody>{tableBody}</TableBody>
</BaseTable>
<PaginationInfo table={table} intlLabel="global.hardwares" />
<PaginationInfo
table={table}
intlLabel="global.hardwares"
onPaginationChange={navigateWithPageSize}
/>
</div>
);
}

0 comments on commit d09dc2c

Please sign in to comment.