All files / src/features/Merchants/MerchantSidePanel/components/UnderwriterRiskAnalystAssignCard ProcessorInput.tsx

95.34% Statements 41/43
92% Branches 46/50
90% Functions 9/10
95.12% Lines 39/41

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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199                                                                                                    366x 366x 366x 366x     366x 366x 366x 366x 366x     366x         366x 51x 8x     366x 79x 39x 78x           366x 3x   2x 2x     1x 1x 1x       1x           1x     1x                 3x     366x 3x 1x 1x   2x                               2x         366x   366x                                                                                 4x 4x             27x          
import RESOURCE_BASE, {
  ACTION_DENY_MESSAGE,
  OPERATIONS,
} from "@constants/permissions";
import { useModal } from "@ebay/nice-modal-react";
import { useAccessControl } from "@features/Permissions/AccessControl";
import GiveSelect from "@shared/GiveInputs/GiveSelect";
import GiveText from "@shared/Text/GiveText";
import { TextProps } from "@shared/Text/giveText.types";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { GIVE_CONFIRMATION_POP_UP } from "modals/modal_names";
import { useEffect, useMemo, useRef, useState } from "react";
import { ProcessorValue } from "@features/Merchants/MerchantSidePanel/types";
import { useGetCurrentMerchantId } from "@hooks/common";
import { useGetProcessors } from "@hooks/acquirer-api/merchants/useGetProcessors";
import {
  MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS,
  processorMap,
} from "@features/Merchants/MerchantSidePanel/constants";
import useUpdateMerchant from "@features/Merchants/MerchantSidePanel/hooks/useUpdateMerchant";
import { useQueryClient } from "react-query";
import HFGiveSelect from "@shared/HFInputs/HFGiveSelect/HFGiveSelect";
import ContextualMenuHeader from "@shared/ContextualMenu/ContextualMenuHeader";
 
export default function ProcessorInput({
  processorValue,
  onChange,
  disabled = false,
  isApproved = false,
  withoutConfirm = false,
  id,
  name = "processorName",
  controlled,
  label = "Processor",
  placeholder = "Select an option",
  useContextualMenuHeader = false,
}: {
  processorValue?: ProcessorValue | "";
  onChange?: (value: ProcessorValue) => void;
  disabled?: boolean;
  isApproved?: boolean;
  withoutConfirm?: boolean;
  id?: number;
  name?: string;
  controlled?: boolean;
  label?: string | null;
  placeholder?: string;
  useContextualMenuHeader?: boolean;
}) {
  const { merchantId, isSponsor } = useGetCurrentMerchantId();
  const { data, isLoading } = useGetProcessors(merchantId);
  const { show, remove } = useModal(GIVE_CONFIRMATION_POP_UP);
  const [localValue, setLocalValue] = useState<ProcessorValue | "">(
    processorValue || "",
  );
  const { isMobileView } = useCustomThemeV2();
  const selectRef = useRef<{ closeMenu: () => void } | null>(null);
  const { updateMerchantMutation } = useUpdateMerchant(undefined, id);
  const { mutateAsync } = updateMerchantMutation;
  const queryClient = useQueryClient();
 
  const hasEditMerchantPermission =
    useAccessControl({
      resource: RESOURCE_BASE.MERCHANT,
      operation: OPERATIONS.UPDATE,
    }) && !isSponsor;
 
  useEffect(() => {
    if (processorValue && processorValue !== localValue)
      setLocalValue(processorValue);
  }, [processorValue]);
 
  const options = useMemo(() => {
    if (!data) return [];
    return data.map((processorItem) => {
      return {
        label: processorMap[processorItem.name],
        value: processorItem.name,
      };
    });
  }, [data, localValue]);
  const handleSubmit = async (value: ProcessorValue) => {
    if (onChange) {
      //flow should be used for local changes, like edit modal, where API call happens on different submit
      setLocalValue(value);
      onChange(value);
    } else {
      //if no custom change logic, we directly update merchant with endpoint call
      const hadNoValue = !localValue;
      setLocalValue(value);
      const data = await mutateAsync({
        categoryCodeID: null,
        processorName: value,
      });
      Iif (hadNoValue && id) {
        queryClient.invalidateQueries([
          MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_LEGAL_ENTITY,
          id,
        ]);
      }
      queryClient.setQueryData(
        [MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET, id],
        (oldData: any) => {
          return {
            ...oldData,
            processor: data?.processorName,
            categoryCodeID: data?.categoryCodeID,
            categoryCodeTitle: data?.categoryCodeTitle,
          };
        },
      );
    }
    remove();
  };
 
  const onChangeValue = (value: ProcessorValue) => {
    if (withoutConfirm) {
      handleSubmit(value);
      return;
    }
    show({
      modalType: "success",
      title: "Switch Payment Processor",
      customSubmitBtnText: "Yes, Switch",
      description: (
        <GiveText {...defaultTextProps} color="secondary">
          You’re are switching payment processor {localValue ? `from ` : " "}
          {!!localValue && (
            <GiveText {...defaultTextProps}>
              {processorMap[localValue]}{" "}
            </GiveText>
          )}
          to <GiveText {...defaultTextProps}>{processorMap[value]}</GiveText>.
        </GiveText>
      ),
      actions: {
        handleSuccess: { onClick: () => handleSubmit(value) },
      },
    });
  };
 
  const Selector = controlled ? HFGiveSelect : GiveSelect;
 
  return (
    <GiveTooltip
      disableFocusListener={hasEditMerchantPermission && !isApproved}
      disableHoverListener={hasEditMerchantPermission && !isApproved}
      disableTouchListener={hasEditMerchantPermission && !isApproved}
      title={
        isApproved
          ? "You can’t change the processor after the merchant is approved"
          : ACTION_DENY_MESSAGE
      }
      placement="top-start"
      alignment="end"
    >
      <Selector
        ref={selectRef}
        name={name}
        label={label}
        placeholder={placeholder}
        options={options}
        useContextualMenu
        contextualMenuProps={{
          ...(!isMobileView && {
            color: "transparent",
            texture: "blurred",
          }),
          ...(isMobileView &&
            useContextualMenuHeader && {
              Header: (
                <ContextualMenuHeader
                  title="Select Processor"
                  onBackClick={() => selectRef.current?.closeMenu()}
                />
              ),
            }),
        }}
        disabled={
          !hasEditMerchantPermission || isApproved || disabled || isLoading
        }
        value={localValue}
        withIconOffset={false}
        onChange={(e) => {
          const value = e?.target?.value as ProcessorValue;
          if (value !== localValue) onChangeValue(value);
        }}
      />
    </GiveTooltip>
  );
}
 
const defaultTextProps: Partial<TextProps> = {
  component: "span",
  variant: "bodyS",
  color: "primary",
};