All files / src/componentsV2/Table/hooks useTableSort.tsx

71.42% Statements 40/56
57.14% Branches 16/28
72.72% Functions 8/11
70.9% Lines 39/55

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134                                      34x           1223x   1223x   1223x 1223x   1223x   1223x   1223x       236x 236x 236x   236x 235x                 236x     1223x 224x 224x     1223x 236x 236x         2212x                 1223x         1223x         1223x 236x   236x 236x   236x         236x       236x         236x 73x   163x       1223x 224x 224x 224x 224x       1223x                    
import { useAppDispatch } from "@redux/hooks";
import {
  disableTableSortingOrder,
  disableTableSortingOrderProperty,
  setTableSortingOrder,
  setTableSortingOrderProperty,
} from "@redux/slices/app";
import { setDynamicSortByKey, updateSorting } from "@redux/slices/fundraisers";
import React, { Dispatch, SetStateAction, useEffect, useState } from "react";
import { TableColumnType, TableData, TableOrder } from "../Table.types";
 
type Props = {
  small?: boolean;
  setSorting?: Dispatch<SetStateAction<string>>;
  initialSortingProperty?: string;
  defaultSort?: { key: string; isDesc?: boolean };
  sortReduxKey?: any;
};
 
export const useTableSort = ({
  small,
  setSorting,
  defaultSort = { key: "createdAt", isDesc: true },
  sortReduxKey,
}: Props) => {
  const dispatch = useAppDispatch();
 
  const { key: defaultKey, isDesc: isDefaultDesc } = defaultSort || {};
 
  const [sortKey, setSortKey] = useState<null | keyof TableData>(null);
  const [order, setOrder] = useState<TableOrder>("desc");
  const [contextMenuAnchorEl, setContextMenuAnchorEl] =
    useState<HTMLElement | null>(null);
  const [contextMenuColumn, setContextMenuColumn] =
    useState<TableColumnType | null>(null);
 
  const getNewOrder = (
    columnKey: keyof TableData,
    customOrder?: TableOrder,
  ) => {
    let newOrder: TableOrder = "asc";
    if ((!sortKey || sortKey !== columnKey) && !customOrder) {
      newOrder = "asc";
 
      if (defaultKey === columnKey && isDefaultDesc) {
        newOrder = "desc";
      }
    } else E{
      if (customOrder) {
        newOrder = customOrder;
      } else {
        newOrder = order === "asc" ? "desc" : "asc";
      }
    }
    return newOrder;
  };
 
  useEffect(() => {
    setSortKey("createdAt");
    setOrder("desc");
  }, []);
 
  useEffect(() => {
    Eif (sortKey !== defaultKey) {
      setSortingData(defaultKey);
    }
  }, [defaultKey, isDefaultDesc]);
 
  const handleClick =
    (column: TableColumnType) => (event: React.MouseEvent<HTMLElement>) => {
      if (column.isComplexSort) {
        setContextMenuAnchorEl(event.currentTarget);
        setContextMenuColumn(column);
      } else {
        setSortingData(column.key);
      }
    };
 
  const handleContextMenuClose = () => {
    setContextMenuAnchorEl(null);
    setContextMenuColumn(null);
  };
 
  const handleComplexSortSelect = (key: string, order?: TableOrder) => {
    setSortingData(key, order);
    handleContextMenuClose();
  };
 
  const setSortingData = (key: string, order?: TableOrder) => {
    setSortKey(key);
 
    const newOrder = getNewOrder(key, order);
    setOrder(newOrder);
 
    const newSorting = (newOrder === "asc" ? key : `-${key}`).replace(
      "--",
      "",
    ) as TableOrder;
 
    Iif (setSorting) {
      setSorting(newSorting);
      return;
    }
    Iif (small) {
      dispatch(setTableSortingOrderProperty(key));
      dispatch(setTableSortingOrder(newSorting));
      return;
    }
    if (sortReduxKey) {
      dispatch(setDynamicSortByKey({ key: sortReduxKey, params: newSorting }));
    } else {
      dispatch(updateSorting(newSorting));
    }
  };
 
  useEffect(() => {
    return () => {
      dispatch(disableTableSortingOrder());
      dispatch(disableTableSortingOrderProperty());
      dispatch(updateSorting("-name")); //set back to initial value
    };
  }, []);
 
  return {
    handleClick,
    sortKey,
    order,
    contextMenuAnchorEl,
    contextMenuColumn,
    handleContextMenuClose,
    handleComplexSortSelect,
  };
};