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

89.47% Statements 17/19
70.58% Branches 12/17
71.42% Functions 5/7
88.88% Lines 16/18

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                                          29x             1094x 1094x   1094x         1094x               1094x 1241x               1094x                 1094x   1x                     1094x             1094x             1094x   248x                   1094x     1094x                    
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 { QKEY_LIST_ENTERPRISES } from "@constants/queryKeys";
import { useInfiniteEnterprises } from "@common/BusinessProfileInputs/EnterprisesSelect";
import {
  composePermission,
  useAccessControl,
} from "@features/Permissions/AccessControl";
import RESOURCE_BASE, { OPERATIONS } from "@constants/permissions";
import { useGetCurrentMerchantId } from "@hooks/common";
import {
  FilterPagesEnum,
  FilterPageValuesTYpes,
} from "@shared/GiveFilter/constants";
 
const useGetProviderSelect = ({
  enabled,
  filterPage,
}: {
  enabled?: boolean;
  filterPage?: FilterPageValuesTYpes;
}) => {
  const dispatch = useAppDispatch();
  const { isSponsor } = useGetCurrentMerchantId();
 
  const hasListProviderPermission = useAccessControl({
    resource: RESOURCE_BASE.ENTERPRISE,
    operation: OPERATIONS.LIST,
  });
 
  const sponsorHasListProviderPermission = useAccessControl({
    resource: composePermission(
      RESOURCE_BASE.ENTERPRISE,
      RESOURCE_BASE.SPONSOR,
    ),
    operation: OPERATIONS.LIST,
  });
 
  const searchQuery = useAppSelector((state) =>
    selectQueryString(state, QKEY_LIST_ENTERPRISES),
  );
 
  const {
    data: merchants,
    loadNextPage,
    isLoading,
    isFetching,
  } = useInfiniteEnterprises({
    searchText: searchQuery,
    enabled:
      enabled &&
      (hasListProviderPermission || sponsorHasListProviderPermission),
    hasLoadingOnSearch: true,
    tabFilter: isSponsor && sponsorHasListProviderPermission ? "-approved" : "",
  });
 
  const providerSearchBarProps = {
    handleChange: (val: string) => {
      dispatch(
        setSearchQueryByKey({
          queryKey: QKEY_LIST_ENTERPRISES,
          params: {
            value: val,
          },
        }),
      );
    },
    value: searchQuery,
  };
  const providerClearSearch = () => {
    dispatch(resetSearchQueryByKey(QKEY_LIST_ENTERPRISES));
  };
 
  // use accID as value for transactions/transactions-basic pages so we can filter by
  // parent account ID instead of name (avoids matching multiple providers with the same name)
  const useAccIDAsValue =
    !!filterPage &&
    [
      FilterPagesEnum.ACQUIRER_MANAGE_MONEY,
      FilterPagesEnum.ACQUIRER_PROCESSING,
      FilterPagesEnum.PROVIDER_PROCESSING,
    ].includes(filterPage);
 
  const providerOptions: SelectorOption[] = useMemo(
    () =>
      merchants.map((item) => ({
        label: item.name,
        id: item.accID,
        icon: item.imageURL ? `${item?.imageURL}/thumb` : "",
        value: useAccIDAsValue ? item.accID : item.name,
        iconType: "provider",
      })),
    [merchants, useAccIDAsValue],
  );
 
  const providerOnEndReached = () => {
    loadNextPage();
  };
  return {
    providerOptions,
    providerSearchBarProps,
    providerClearSearch,
    providerOnEndReached,
    providerIsLoading: searchQuery ? isLoading : isLoading && !isFetching,
  };
};
 
export default useGetProviderSelect;