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 | 149x 2x 2x 2x 2x 1x 2x 2x 2x 1x 2x 2x 2x 2x 149x 443x 443x 149x 71x 223x | import React, { Dispatch, SetStateAction, useEffect, useState } from "react";
import {
AmexCardIcon,
DiscoverCardIcon,
MasterCardIcon,
VisaCardIcon,
} from "@assets/builderIcons";
import { RHFInput } from "@common/Input";
import { InputAdornment, Stack } from "@mui/material";
import { useFormContext } from "react-hook-form";
import { formatCardNumber } from "@utils/index";
import { useAppDispatch } from "@redux/hooks";
import { setCheckoutCardType } from "@redux/slices/checkout";
import { Text } from "@common/Text";
import GiveText from "@shared/Text/GiveText";
type CardNumberInputProps = {
setCardFees?: Dispatch<SetStateAction<number>>;
handleCardNumberCheck: (cardNumber: string, setError: any) => Promise<void>;
cardObject: any;
};
const CardNumberInput = ({
setCardFees,
cardObject,
handleCardNumberCheck,
}: CardNumberInputProps) => {
const { setError } = useFormContext();
const [cardType, setCardType] = useState<string>("");
const dispatch = useAppDispatch();
React.useEffect(() => {
dispatch(setCheckoutCardType(cardType));
}, [cardType]);
const maxCardNumberLength = cardType && cardType === "AMEX" ? 17 : 19;
const normalizeInput = (value: string) => {
if (!value) return value;
const card = formatCardNumber(value);
const allowedLength = card?.cardType && card?.cardType === "AMEX" ? 17 : 19;
if (card?.formattedNumber?.length === allowedLength) {
handleCardNumberCheck(value, setError);
}
if (card?.cardType) setCardType(card.cardType);
return card.formattedNumber;
};
useEffect(() => {
Iif (cardObject) setCardFees && setCardFees(+cardObject?.fees || 0);
}, [cardObject]);
const getCardIcon = React.useCallback(() => {
const _type = cardType.toUpperCase();
return getCardBrandIcon(_type);
}, [cardType]);
return (
<RHFInput
name="cardNumber"
label="Card number"
handleNormalizeInput={normalizeInput}
fullWidth
InputProps={{
endAdornment: (
<InputAdornment position="start" sx={{ alignItems: "center" }}>
{getCardIcon()}
</InputAdornment>
),
}}
inputProps={{
maxLength: maxCardNumberLength,
}}
placeholder="•••• •••• •••• ••••"
sx={{
letterSpacing: "2px",
"& .MuiFormHelperText-root": {
letterSpacing: "0px",
},
}}
/>
);
};
export const getCardBrandIcon = (
type: string,
isPaymentForm = true,
iconSize?: { width: number; height: number },
isMobileView?: boolean,
palette?: any,
) => {
const { width, height } = iconSize || { width: 34, height: 24 };
switch (type) {
case "VISA":
return <VisaCardIcon width={width} height={height} />;
case "MASTERCARD":
return <MasterCardIcon width={width} height={height} />;
case "AMERICAN EXPRESS":
return <AmexCardIcon width={width} height={height} />;
case "AMEX":
return <AmexCardIcon width={width} height={height} />;
case "DISCOVER":
return <DiscoverCardIcon width={width} height={height} />;
case "hide":
// when you don't want to display any icon
return null;
default:
return isPaymentForm ? (
<Stack
direction="row"
spacing={1}
sx={{ filter: "drop-shadow(0px 2px 2px rgba(0, 0, 0, 0.05));" }}
>
<VisaCardIcon width={width} height={height} />
<MasterCardIcon width={width} height={height} />
{isMobileView ? (
<>
<Stack
width="40px"
height="26px"
justifyContent="center"
alignItems="center"
sx={{
backgroundColor: palette.common?.white,
border: `1px solid ${palette.border?.primary}`,
borderRadius: "4px",
}}
>
<GiveText variant="bodyXS" color="secondary">
+2
</GiveText>
</Stack>
</>
) : (
<>
<AmexCardIcon width={width} height={height} />
<DiscoverCardIcon width={width} height={height} />
</>
)}
</Stack>
) : (
<Text>{type}</Text>
);
}
};
export default CardNumberInput;
|