All files / src/hooks/common usePagination.ts

0% Statements 0/15
100% Branches 0/0
0% Functions 0/3
0% Lines 0/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51                                                                                                     
import { useCallback } from "react";
import { useDispatch, useSelector } from "react-redux";
import {
  PaginationKeys,
  paginationSelector,
  setPaginationKey,
} from "../../store/features/pagination/paginationSlice.ts";
 
interface UsePaginationResult {
  page: number;
  limit: number;
  handleChangePage: (event: any, newPage: number) => void;
  handleChangeRowsPerPage: (
    event: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>,
  ) => void;
}
 
const usePagination = (): UsePaginationResult => {
  const dispatch = useDispatch();
  const pagination = useSelector(paginationSelector);
 
  const { page, limit } = pagination;
 
  const handleChangePage = useCallback(
    (event, newPage) => {
      dispatch(setPaginationKey({ key: PaginationKeys.PAGE, value: newPage }));
    },
    [dispatch],
  );
 
  const handleChangeRowsPerPage = useCallback(
    (event) => {
      let newPageSize = parseInt(event.target.value, 10);
      dispatch(
        setPaginationKey({ key: PaginationKeys.LIMIT, value: newPageSize }),
      );
      dispatch(setPaginationKey({ key: PaginationKeys.PAGE, value: 0 }));
    },
    [dispatch],
  );
 
  return {
    page,
    limit,
    handleChangePage,
    handleChangeRowsPerPage,
  };
};
 
export default usePagination;