All files / src/hooks/Reducers useSorting.ts

76.92% Statements 10/13
28.57% Branches 2/7
80% Functions 4/5
76.92% Lines 10/13

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 52 53                            1x 114x 114x 173x     114x 8x 8x 8x         114x                               114x                
import { useAppDispatch, useAppSelector } from "@redux/hooks";
import {
  resetSorting,
  selectSorting,
  setSorting,
  toggleOrder,
} from "@redux/slices/sort";
import { useEffect } from "react";
 
type SortingProps = {
  tableName: string;
  reset?: "unmount";
};
 
const useSorting = ({ tableName, reset = "unmount" }: SortingProps) => {
  const dispatch = useAppDispatch();
  const { attribute, order } = useAppSelector((state) =>
    selectSorting(state, tableName),
  );
 
  useEffect(() => {
    return () => {
      Eif (reset === "unmount") {
        dispatch(resetSorting(tableName));
      }
    };
  }, []);
 
  const toggleSorting = (newAttribute?: string) => {
    if (newAttribute && newAttribute !== attribute) {
      dispatch(
        setSorting({
          tableName,
          params: {
            attribute: newAttribute,
            order: "asc",
          },
        }),
      );
    } else {
      dispatch(toggleOrder(tableName));
    }
  };
 
  return {
    attribute,
    order,
    toggleSorting,
  };
};
 
export default useSorting;