All files / src/components/PaymentForm/PaymentCard RadioPaymentCard.tsx

0% Statements 0/13
0% Branches 0/14
0% Functions 0/5
0% Lines 0/13

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                                                                                                                                                                                                                                                       
import React, { useMemo } from "react";
import { Box, SxProps, FormControl, FormGroup } from "@mui/material";
import PaymentCard, { PaymentCardProps } from "./PaymentCard";
import { VisualIndicator } from "@assets/icons";
import { CustomAmountInputType } from "@common/PublicForm/types";
import { Text } from "@common/Text";
import { selectCart } from "@redux/slices/cart";
import { CartItemType } from "@redux/slices/cart/CartItemType";
import { useAppSelector } from "@redux/hooks";
import { palette } from "@palette";
import { buttonLabelsMap } from "./constants";
 
type RadioPaymentCardProps = PaymentCardProps &
  Partial<CustomAmountInputType> & {
    isChecked: boolean;
    onChange?: (id: string) => void;
    name?: string;
    radioSx?: SxProps;
    ticketId?: string;
    display?: boolean;
    disabled?: boolean;
    isDesktop?: boolean;
    numberID: number;
    quantity: number;
    typeName: keyof typeof buttonLabelsMap;
  };
 
const RadioPaymentCard = ({
  isChecked,
  onChange,
  name,
  radioSx,
  display,
  isDesktop,
  numberID,
  quantity,
  ...props
}: RadioPaymentCardProps) => {
  const { cartItems } = useAppSelector(selectCart);
 
  const currentAmount = useMemo(() => {
    return (
      cartItems.find((item: CartItemType) => item.productVariantID === numberID)
        ?.quantity || 0
    );
  }, [cartItems]);
 
  const backgroundColor = React.useMemo(() => {
    const qLeft = quantity - currentAmount;
    switch (true) {
      case qLeft < 21 && qLeft > 0:
        return {
          backgroundColor: "#FFE8DA80",
          color: "#FF8124",
        };
      case qLeft < 1:
        return {
          backgroundColor: "#E1E1DE",
          color: "#575353",
        };
      default:
        return {
          backgroundColor: palette.success.light,
          color: palette.success.main,
        };
    }
  }, [currentAmount]);
 
  return (
    <PaymentCard
      selected={isChecked}
      onChange={onChange}
      name={name}
      actions={
        <Box
          sx={{
            display: "flex",
            flexDirection: "row",
            alignItems: "center",
            justifyContent: "center",
          }}
        >
          <FormControl
            fullWidth
            component="fieldset"
            onClick={(event: React.MouseEvent<HTMLElement>) =>
              event.stopPropagation()
            }
          >
            <FormGroup>
              <VisualIndicator width={24} height={24} />
            </FormGroup>
          </FormControl>
        </Box>
      }
      quantityLeftComponent={
        display &&
        !props.disabled && (
          <Text
            sx={{
              textAlign: "center",
              color: backgroundColor.color,
              backgroundColor: backgroundColor.backgroundColor,
              borderRadius: "4px",
              paddingInline: "12px",
              minWidth: "80px",
              marginY: isDesktop ? "initial" : 0,
            }}
            my={2}
            data-testid={`ticket-${props.ticketId}-left-amount`}
          >
            {quantity - currentAmount > 0
              ? quantity - currentAmount + " left"
              : "Sold Out"}
          </Text>
        )
      }
      {...props}
    />
  );
};
 
export default RadioPaymentCard;