All files / src/sections/PayBuilder/Checkout/components PayBuilderCardNumberField.tsx

81.25% Statements 13/16
30% Branches 3/10
80% Functions 4/5
81.25% Lines 13/16

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                          12x       108x 108x 108x 108x       108x   108x 2x                 108x 25x     108x 23x       108x                    
import CardNumberField from "../../../../componentsV2/Payment/components/CardNumberField";
import { useGetCardInfos } from "@hooks/merchant-api/cart/useGetCardInfos";
import { useFormContext } from "react-hook-form";
import { useEffect } from "react";
import { useCart } from "@sections/PayBuilder/provider/CartContext";
import { setCheckoutCardType } from "@redux/slices/checkout";
import { useAppDispatch } from "@redux/hooks";
import { InputProps } from "@shared/GiveInputs/GiveInput";
 
type IPayBuilderCardNumberField = InputProps & {
  isSupportEnabled?: boolean;
};
 
const PayBuilderCardNumberField = ({
  isSupportEnabled,
  ...rest
}: IPayBuilderCardNumberField) => {
  const dispatch = useAppDispatch();
  const { watch, setError } = useFormContext();
  const customerCoversFees = watch("customerCoversFees");
  const { handleCardNumberCheck, cardObject } = useGetCardInfos({
    configPassFees: !customerCoversFees,
  });
 
  const { setFees } = useCart();
 
  const onCheckCardNumber = (value: string) => {
    Iif (!isSupportEnabled) {
      handleCardNumberCheck(value, (msg) =>
        setError("payment.cardNumber", {
          message: msg,
        }),
      );
    }
  };
 
  const handleCardTypeChange = (value: string) => {
    dispatch(setCheckoutCardType(value));
  };
 
  useEffect(() => {
    Iif (cardObject && !isSupportEnabled)
      setFees && setFees(cardObject?.fees ? +cardObject?.fees / 100 : 0);
  }, [cardObject, isSupportEnabled]);
 
  return (
    <CardNumberField
      handleCardNumberCheck={onCheckCardNumber}
      onCardTypeChange={handleCardTypeChange}
      {...rest}
    />
  );
};
 
export default PayBuilderCardNumberField;