All files / src/features/Settlements/hooks useGetSettlementActions.tsx

81.81% Statements 18/22
75% Branches 3/4
83.33% Functions 5/6
80% Lines 16/20

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                                                      1x                 10x 10x 10x 10x 10x 10x   10x       10x 4x 4x                         10x                                   10x                                         30x 20x   10x          
import { ReactElement, useEffect } from "react";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import DateSelectDropdownButton from "../components/DateSelectDropdownButton";
import MerchantDropdownFilter from "../components/MerchantDropdownFilter";
import ProcessorDropdownFilter from "../components/ProcessorDropdownFilter";
import GiveButton from "@shared/Button/GiveButton";
import { SettlementPageEnum, TPage } from "../types";
import GiveSettlementExportButton from "../components/GiveSettlementExportButton";
import NiceModal from "@ebay/nice-modal-react";
import { GIVE_SETTLMENT_MERCHANT_PANEL } from "modals/modal_names";
import { useAppDispatch } from "@redux/hooks";
import { useTableFilters } from "@redux/slices/tableFilters";
import {
  QKEY_GET_SETTLEMENT_MERCHANTS,
  QKEY_LIST_ACQUIRER_MERCHANTS,
} from "@constants/queryKeys";
import { resetSearchQueryByKey } from "@redux/slices/search";
import { setSelectedMerchant } from "@redux/slices/merchantFilters";
import { useGetCurrentMerchantId } from "@hooks/common";
 
export type SettlementAction = {
  id: string;
  component: ReactElement;
  hidden?: boolean;
  keepState?: boolean;
};
 
export const useGetSettlementActions = ({
  page,
  keepState,
  disabled,
}: {
  page: TPage;
  keepState?: boolean;
  disabled?: boolean;
}) => {
  const isSettlementPage = page === SettlementPageEnum.SETTLEMENT;
  const { isMerchantProcessorEnabled } = useGetFeatureFlagValues();
  const { isSponsor } = useGetCurrentMerchantId();
  const dispatch = useAppDispatch();
  const { resetTableFilters } = useTableFilters();
  const { merchantId, name } = useGetCurrentMerchantId();
 
  const openPanel = () => {
    NiceModal.show(GIVE_SETTLMENT_MERCHANT_PANEL);
  };
  // Reset filters if needed
  useEffect(() => {
    return () => {
      Iif (!keepState) {
        resetTableFilters(true);
        dispatch(resetSearchQueryByKey(QKEY_LIST_ACQUIRER_MERCHANTS));
        dispatch(
          setSelectedMerchant({
            queryKey: QKEY_GET_SETTLEMENT_MERCHANTS,
            value: { accID: merchantId, name } as any,
          }),
        );
      }
    };
  }, []);
 
  const leftActions: SettlementAction[] = [
    {
      id: "date-select",
      component: <DateSelectDropdownButton disabled={disabled} useCanaryTime />,
      hidden: isSettlementPage,
    },
    {
      id: "merchant-filter",
      component: <MerchantDropdownFilter disabled={disabled} />,
      hidden: false,
    },
    {
      id: "processor-filter",
      component: <ProcessorDropdownFilter disabled={disabled} />,
      hidden: !isMerchantProcessorEnabled || isSponsor,
    },
  ];
 
  const rightActions: SettlementAction[] = [
    {
      id: "export",
      component: <GiveSettlementExportButton />,
      hidden: isSettlementPage,
    },
    {
      id: "view-merchants",
      component: (
        <GiveButton
          variant="filled"
          size="large"
          label="View Merchants"
          onClick={openPanel}
        />
      ),
      hidden: isSettlementPage,
    },
  ];
 
  // Filter out hidden actions
  const filteredLeftActions = leftActions.filter((action) => !action.hidden);
  const filteredRightActions = rightActions.filter((action) => !action.hidden);
 
  return {
    leftActions: filteredLeftActions,
    rightActions: filteredRightActions,
  };
};