All files / src/shared/GiveFilter/hooks useGetMerchantSelect.tsx

86.66% Statements 13/15
90% Branches 9/10
71.42% Functions 5/7
85.71% Lines 12/14

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                        29x         1094x             1094x             1094x 1241x     1094x   1x                     1094x       1094x   250x                                 1094x     1094x                    
import { useInfiniteMerchants } from "@components/Settlement/hooks/useSettlementInfiniteMerchants";
import { SelectorOption } from "@pages/ManageMoney/Modal/filter.types";
import { useAppDispatch, useAppSelector } from "@redux/hooks";
import {
  resetSearchQueryByKey,
  selectQueryString,
  setSearchQueryByKey,
} from "@redux/slices/search";
import { useMemo } from "react";
import { FilterPagesEnum, FilterPageValuesTYpes } from "../constants";
import { QKEY_LIST_ENTERPRISE_MERCHANTS } from "@constants/queryKeys";
 
const useGetMerchantSelect = ({
  filterPage,
}: {
  filterPage: FilterPageValuesTYpes;
}) => {
  const dispatch = useAppDispatch();
 
  const {
    data: merchants,
    isLoading,
    isFetching,
    loadNextPage,
  } = useInfiniteMerchants({
    disableStats:
      filterPage === FilterPagesEnum.PROVIDER_PROCESSING ||
      filterPage === FilterPagesEnum.ACQUIRER_MANAGE_MONEY, //for now we use this select only in providerProcessing and acquirer/provider manage moneyfilter panel
    query: QKEY_LIST_ENTERPRISE_MERCHANTS,
  });
 
  const searchQuery = useAppSelector((state) =>
    selectQueryString(state, QKEY_LIST_ENTERPRISE_MERCHANTS),
  );
 
  const merchantSearchBarProps = {
    handleChange: (val: string) => {
      dispatch(
        setSearchQueryByKey({
          queryKey: QKEY_LIST_ENTERPRISE_MERCHANTS,
          params: {
            value: val,
          },
        }),
      );
    },
    value: searchQuery,
  };
  const merchantClearSearch = () => {
    dispatch(resetSearchQueryByKey(QKEY_LIST_ENTERPRISE_MERCHANTS));
  };
 
  const merchantOptions: SelectorOption[] = useMemo(
    () =>
      merchants.map((item) => ({
        label: item.name,
        id: item.accID,
        icon: item.imageURL ? `${item?.imageURL}/thumb` : "",
        value: [
          FilterPagesEnum.ACQUIRER_MANAGE_MONEY,
          FilterPagesEnum.ACQUIRER_PROCESSING,
          FilterPagesEnum.PROVIDER_PROCESSING,
          FilterPagesEnum.ACQUIRER_DISPUTES,
        ].includes(filterPage)
          ? item.accID
          : item.name,
        iconType: "merchant",
      })),
    [merchants, filterPage],
  );
 
  const merchantOnEndReached = () => {
    loadNextPage();
  };
  return {
    merchantOptions,
    merchantSearchBarProps,
    merchantClearSearch,
    merchantOnEndReached,
    merchantIsLoading: searchQuery ? isLoading : isLoading && !isFetching,
  };
};
 
export default useGetMerchantSelect;