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

52.17% Statements 12/23
23.07% Branches 3/13
33.33% Functions 3/9
60% Lines 12/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                          29x 1094x   1094x 1241x             1094x         1094x                         1094x       1094x   232x                         1094x       1094x                     29x                                                  
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_GET_ALL_CATEGORIES_CODES } from "@constants/queryKeys";
import useListCategories from "@hooks/acquirer-api/useListCategoriesCodes";
import GiveText from "@shared/Text/GiveText";
import { SHOW_RISK_CATEGORY } from "@features/Merchants/MerchantSidePanel/constants";
 
const useGetMCCSelect = () => {
  const dispatch = useAppDispatch();
 
  const searchQuery = useAppSelector((state) =>
    selectQueryString(state, QKEY_GET_ALL_CATEGORIES_CODES),
  );
  const {
    data: categoryCodes,
    setPage,
    isLoading,
    isFetching,
  } = useListCategories({
    all: false,
    searchTerm: searchQuery,
  });
 
  const mccSearchBarProps = {
    handleChange: (val: string) => {
      dispatch(
        setSearchQueryByKey({
          queryKey: QKEY_GET_ALL_CATEGORIES_CODES,
          params: {
            value: val,
          },
        }),
      );
    },
    value: searchQuery,
  };
  const mccClearSearch = () => {
    dispatch(resetSearchQueryByKey(QKEY_GET_ALL_CATEGORIES_CODES));
  };
 
  const mccOptions: SelectorOption[] = useMemo(
    () =>
      categoryCodes.map((item) => ({
        label: `${item.code} - ${item.title}`,
        id: `${item.id}`,
        icon: "",
        value: `${item.code}`,
        hideImage: !!item?.riskLevelName,
        description: item?.riskLevelName && SHOW_RISK_CATEGORY ? (
          <MccText text={item?.riskLevelName} />
        ) : undefined,
      })),
    [categoryCodes],
  );
 
  const mccOnEndReached = () => {
    if (isLoading) return;
    setPage((prev) => prev + 1);
  };
  return {
    mccOptions,
    mccSearchBarProps,
    mccClearSearch,
    mccOnEndReached,
    mccIsLoading: searchQuery ? isLoading : isLoading && !isFetching,
  };
};
 
export default useGetMCCSelect;
 
const MccText = ({ text = "primary" }: { text?: string }) => {
  const colors = {
    low: {
      color: "success",
      text: "Normal",
    },
    high: {
      color: "warning1",
      text: "High",
    },
    prohibited: {
      color: "error",
      text: "Restricted",
    },
  };
 
  const normalizedText = text?.toLowerCase();
  const obj = colors[normalizedText as keyof typeof colors];
 
  return (
    <GiveText color={obj?.color ?? "primary"} fontSize="12px">
      {obj?.text}
    </GiveText>
  );
};