Skip to content

Commit

Permalink
fix(#20): set the start index correctly when some filter is applied
Browse files Browse the repository at this point in the history
  • Loading branch information
mari1912 committed Aug 19, 2024
1 parent 82f83f6 commit 0aac5ed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 6 additions & 1 deletion dashboard/src/hooks/usePagination.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { useState, useEffect, useCallback } from 'react';

import { TFilter, TableFilter } from '@/types/tree/TreeDetails';

export const usePagination = (
totalItems: number,
itemsPerPage: number,
filters?: TFilter,
tableFilter?: TableFilter,
): {
startIndex: number;
endIndex: number;
Expand All @@ -13,8 +17,9 @@ export const usePagination = (
const [endIndex, setEndIndex] = useState(0);

useEffect(() => {
setStartIndex(0); //restart the index whenever there is a change in filters
setEndIndex(totalItems > itemsPerPage ? itemsPerPage : totalItems);
}, [itemsPerPage, totalItems]);
}, [itemsPerPage, totalItems, filters, tableFilter]);

const onClickGoForward = useCallback(() => {
setStartIndex(endIndex);
Expand Down
8 changes: 7 additions & 1 deletion dashboard/src/pages/TreeDetails/Tabs/Build/BuildTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const BuildTab = ({ treeDetailsData }: BuildTab): JSX.Element => {
const navigate = useNavigate({
from: '/tree/$treeId',
});
const { diffFilter } = useSearch({ from: '/tree/$treeId/' });

const accordionContent = useMemo(() => {
return treeDetailsData?.builds.map(row => ({
Expand Down Expand Up @@ -69,7 +70,12 @@ const BuildTab = ({ treeDetailsData }: BuildTab): JSX.Element => {
: accordionContent;

const { startIndex, endIndex, onClickGoForward, onClickGoBack } =
usePagination(filteredContent?.length ?? 0, ITEMS_PER_PAGE);
usePagination(
filteredContent?.length ?? 0,
ITEMS_PER_PAGE,
diffFilter,
filterBy,
);
const cards = useMemo(
() => [
{
Expand Down

0 comments on commit 0aac5ed

Please sign in to comment.