All files / src/features/MerchantPortal/Customer/Modals/components SubscriptionItem.tsx

100% Statements 18/18
78.12% Branches 25/32
100% Functions 5/5
100% Lines 18/18

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                                                        53x                                           79x 79x 79x 79x 1x 1x     79x 1x 1x   79x 79x   79x 79x                                                                                             79x                                                                               1x                 1x                     632x                                  
import { Box, Stack } from "@mui/material";
import GiveText from "shared/Text/GiveText";
import GiveCollapsableComponent from "features/Merchants/MerchantSidePanel/GiveMerchantFile/businessOwners/GiveCollapsableComponent";
import GiveIconButton from "shared/IconButton/GiveIconButton";
import { DotsThreeIcon } from "@phosphor-icons/react";
import { GiveSectionItem } from "features/Merchants/MerchantSidePanel/GiveMerchantFile/GiveSectionItem";
import { RecurringPurchasesListItem } from "types/customer.types";
import { format } from "date-fns";
import { parseAmount } from "utils";
import ContextualMenu from "shared/ContextualMenu/ContextualMenu";
import { useState } from "react";
import { useCustomThemeV2 } from "theme/hooks/useCustomThemeV2";
import NiceModal from "@ebay/nice-modal-react";
import { STOP_RECURRENCE_MODAL } from "modals/modal_names";
import { CustomGridContainer } from "features/Merchants/MerchantSidePanel/GiveMerchantFile/bankAccounts/CustomGridContainer";
import { capitalize } from "lodash";
import { getProductType } from "components/Customers/Table/renderPurchasesStatistics";
import { CANCELLED_STATUSES } from "@components/ManageMoney/TransactionTable/consts";
 
type Props = {
  isFirst: boolean;
  isLast: boolean;
  subscription: RecurringPurchasesListItem;
  customerName: string;
  customerEmail: string;
  merchantID?: number;
};
 
export const SubscriptionItem = ({
  isFirst,
  isLast,
  subscription,
  customerName,
  customerEmail,
  merchantID,
}: Props) => {
  const {
    productName,
    nextTransactionAt,
    createdAt,
    quantity,
    //recurringCount,
    variantName,
    recurringInterval,
    productType,
    //charged,
    //fees,
    cost,
    unitPrice,
    sourceType,
  } = subscription;
  const { isMobileView } = useCustomThemeV2();
  const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
  const onCloseMenu = (e: React.MouseEvent<HTMLElement>) => {
    e?.stopPropagation();
    setAnchorEl(null);
  };
 
  const on3DotsClick = (e: React.MouseEvent<HTMLElement>) => {
    e?.stopPropagation();
    setAnchorEl(e.currentTarget);
  };
  const isCanceled = CANCELLED_STATUSES.has(subscription.recurringStatus);
  const isFromApi = sourceType === "givesync_api_client";
  const displayVariantName =
    variantName === "Any Amount" ? "Custom Amount" : variantName;
  const items = [
    {
      title: "Payment Form",
      value: productName,
    },
    {
      title: "Starting Date",
      value: format(createdAt * 1000, "MM/dd/yyyy"),
    },
    {
      title: isCanceled ? "Termination Date" : "Next Transaction",
      value: nextTransactionAt
        ? format(nextTransactionAt * 1000, "MM/dd/yyyy")
        : "-",
    },
    { title: "Type", value: capitalize(getProductType(productType)) },
    {
      title: "Recurrence",
      value: `${capitalize(recurringInterval)} ${
        isCanceled ? "(Canceled)" : ""
      }`,
    },
    {
      title: "Item",
      value: isFromApi ? "1" : displayVariantName,
    },
    {
      title: "Subscription Type ",
      value: `${quantity || 1} x ${
        !isFromApi ? displayVariantName : ""
      } ${capitalize(recurringInterval)} at cost ${parseAmount(
        (unitPrice || cost) / 100,
      )} USD`,
    },
    {
      title: "Amount (USD) ",
      value: parseAmount(((unitPrice || cost) / 100) * (quantity || 1)),
    },
    // { hide for now, we might need it later
    //   title: "Credit Card Fees (USD)",
    //   value: parseAmount(fees / 100),
    // },
    // { hde for now, we might need it later
    //   title: "Charged (USD)",
    //   value: parseAmount(charged / 100),
    // },
  ];
  return (
    <GiveCollapsableComponent
      isFirst={isFirst}
      isLast={isLast}
      title={
        <Stack
          flex={1}
          alignItems="center"
          direction="row"
          pr="8px"
          justifyContent="space-between"
        >
          <Stack>
            <GiveText variant="bodyM">
              {subscription.productName} {isCanceled ? "(Canceled)" : ""}
            </GiveText>
            <GiveText variant="bodyS" color="secondary">
              {capitalize(getProductType(productType))} x{quantity || 1}
            </GiveText>
          </Stack>
          <Stack>
            {subscription.recurringStatus === "active" && (
              <GiveIconButton
                data-testid="subscription-item-3dots"
                variant="ghost"
                Icon={DotsThreeIcon}
                onClick={on3DotsClick}
              />
            )}
          </Stack>
          <ContextualMenu
            handleClose={onCloseMenu as any}
            anchorEl={anchorEl}
            color={isMobileView ? "primary" : "tertiary"}
            texture={isMobileView ? "solid" : "blurred"}
            options={[
              {
                text: "Cancel Recurrence",
                hidden: false,
                onClick: () => {
                  NiceModal.show(STOP_RECURRENCE_MODAL, {
                    subscription,
                    //if no name we should show the email insted
                    customerName:
                      customerName === "No Name" ? customerEmail : customerName,
                    customerEmail,
                    source: "customer",
                    merchantAccId: merchantID,
                  });
                  setAnchorEl(null);
                },
              },
            ]}
          />
        </Stack>
      }
      withExpandIcon={true}
    >
      <Box data-testid="subscription-details" padding="8px 0 16px 0">
        {items.map((item, i) => {
          return (
            <Box padding="16px 16px 0 32px" key={item.title}>
              <CustomGridContainer
                container
                spacing={2}
                padding="4px"
                highlight={i % 2 !== 0}
              >
                <GiveSectionItem item={item} />
              </CustomGridContainer>
            </Box>
          );
        })}
      </Box>
    </GiveCollapsableComponent>
  );
};