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

100% Statements 27/27
95.45% Branches 21/22
100% Functions 6/6
100% Lines 27/27

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                            13x                   12x   12x   12x   12x   12x   12x     3x   3x 3x     12x 4x 4x   4x 2x 2x   4x 4x 1x         3x 3x     12x 12x 12x     12x                                                 1x                                         1x                                    
import { Stack } from "@mui/material";
import SectionHeader from "./SectionHeader";
import { TicketIcon } from "@phosphor-icons/react";
import GiveText from "@shared/Text/GiveText";
import { usePayBuilderForm } from "@sections/PayBuilder/provider/PayBuilderFormProvider";
import GiveCheckbox from "@shared/GiveCheckbox/GiveCheckbox";
import HFGiveCustomAmount from "@shared/HFInputs/HFGiveCustomAmount/HFGiveCustomAmount";
import { useEffect } from "react";
import WithCheckoutTooltip from "./CheckoutTooltip";
import { useOptionItemHandler } from "../hooks/useOptionItemHandler";
import { CustomDonationInput } from "./YourDonationPreview";
import { MAX_QNT_ALLOWED } from "@sections/PayBuilder/provider/useManagePayFormProvider";
import { InputProps } from "@shared/GiveInputs/GiveInput";
 
const YourEntriesPreview = ({
  isDisabledFields = false,
  isPreviewMode = false,
}: {
  isDisabledFields?: boolean;
  isPreviewMode?: boolean;
}) => {
  const {
    methods,
    parsedValues: { checkoutInputStyles, inputColor },
  } = usePayBuilderForm();
 
  const { setValue } = methods;
 
  const singleEntryPrice = methods.watch("Entries.singleEntryPrice");
 
  const isAnonymous = methods.watch("Entries.isAnonymous");
 
  const MAX = Number(MAX_QNT_ALLOWED);
 
  const { debouncedAddDonationToCart } = useOptionItemHandler();
 
  function handleSelectedEntryChange(value: string | number) {
    const numericValue = Number(value) || 0;
 
    methods.setValue("Entries.selectedEntry", numericValue.toString());
    debouncedAddDonationToCart(singleEntryPrice, "once", numericValue, MAX);
  }
 
  const formatOnBlur = () => {
    const value = methods.getValues("Entries.selectedEntry") || "";
    const numericValue = Number(value.replace(/,/g, "")) || 0;
 
    if (value === "" || numericValue < 1) {
      methods.setValue("Entries.selectedEntry", "1");
      methods.trigger("Entries.selectedEntry");
    }
    const newValue = methods.getValues("Entries.selectedEntry");
    if (Number(newValue) > MAX) {
      methods.setError("Entries.selectedEntry", {
        message: ` You can purchase maximum ${MAX} entries`,
        type: "Entries.selectedEntry",
      });
    } else {
      handleSelectedEntryChange(newValue);
      methods.clearErrors("Entries.selectedEntry");
    }
  };
  useEffect(() => {
    methods.setValue("Entries.selectedEntry", "1");
    debouncedAddDonationToCart(singleEntryPrice, "once", 1, MAX);
  }, []);
 
  return (
    <Stack
      sx={{
        overflowX: "hidden",
      }}
      maxWidth="100%"
      spacing="20px"
    >
      <SectionHeader title="Your Entries" icon={<TicketIcon size={24} />} />
 
      <WithCheckoutTooltip disabled={isDisabledFields && isPreviewMode}>
        <HFGiveCustomAmount
          name="Entries.selectedEntry"
          currency={`${singleEntryPrice ?? 0} usd`}
          label="Amount"
          height="74px"
          value={methods.watch("Entries.selectedEntry")}
          max={Infinity}
          disabled={isDisabledFields}
          onHandleBlur={formatOnBlur}
          helper={methods.formState?.errors?.Entries?.selectedEntry?.message}
          customError={Boolean(
            methods.formState?.errors?.Entries?.selectedEntry?.message,
          )}
          onChange={(value) => {
            methods.setValue("Entries.selectedEntry", value.replace(/,/g, ""));
          }}
          InputComponent={CustomDonationInput as React.FC<InputProps>}
          startIcon={isDisabledFields ? null : <TicketIcon size={20} />}
          simpleNumber
          sx={checkoutInputStyles}
          decimalScale={0}
          thousandSeparator={false}
        />
      </WithCheckoutTooltip>
 
      <WithCheckoutTooltip
        disabled={isDisabledFields && isPreviewMode}
        desktopMarginBottom="-2px"
      >
        <Stack direction="row" alignItems="flex-start" width="100%">
          <Stack direction="row" spacing={1} alignItems="center">
            <GiveCheckbox
              name="Entries.isAnonymous"
              value={isAnonymous}
              checked={isAnonymous}
              onChange={() => setValue("Entries.isAnonymous", !isAnonymous)}
              disabled={isDisabledFields}
              sx={{
                backgroundColor: inputColor,
              }}
            />
 
            <GiveText color="primary" fontSize="14px">
              Make my participation anonymous
            </GiveText>
          </Stack>
        </Stack>
      </WithCheckoutTooltip>
    </Stack>
  );
};
 
export default YourEntriesPreview;