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

84.61% Statements 22/26
80% Branches 20/25
55.55% Functions 5/9
88% Lines 22/25

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                                      53x           16x               16x 11x 11x 11x 11x 56x 56x       56x       56x 56x 56x     11x     16x       16x                                               53x     26x   53x                                 32x 32x                         78x                                                                                    
import SectionCardBase from "shared/SidePanel/components/SectionCard/SectionCardBase";
import { SubscriptionItem } from "./SubscriptionItem";
import { RecurringPurchasesListItem } from "types/customer.types";
import { SubscriptionsEmptyState } from "./SubscriptionsEmptyState";
import { useMemo } from "react";
import { isArray } from "lodash";
import { useState } from "react";
import { useAppTheme } from "@theme/v2/Provider";
import GiveButton from "@shared/Button/GiveButton";
import GiveMotionWrapper from "@shared/Motion/GiveMotionWrapper";
import { Box } from "@mui/material";
 
type Props = {
  subscriptions?: RecurringPurchasesListItem[];
  customerName: string;
  customerEmail: string;
  merchantID?: number;
};
 
export const Subscriptions = ({
  subscriptions = [],
  customerName,
  customerEmail,
  merchantID,
}: Props) => {
  const [showMore, setShowMore] = useState<{
    active: boolean;
    inactive: boolean;
  }>({
    active: false,
    inactive: false,
  });
 
  const { actives, inactives } = useMemo(() => {
    const activesList: RecurringPurchasesListItem[] = [];
    const inactivesList: RecurringPurchasesListItem[] = [];
    Eif (!!subscriptions && isArray(subscriptions))
      for (const sub of subscriptions) {
        const nowTime = new Date().getTime();
        const nextTxnTime = new Date(
          (sub.nextTransactionAt || 0) * 1000,
        ).getTime();
        const terminatesInFuture =
          sub.nextTransactionAt &&
          nowTime < nextTxnTime &&
          sub.recurringStatus !== "error";
 
        const isActive = sub.recurringStatus === "active" || terminatesInFuture;
        isActive && activesList.push(sub);
        !isActive && inactivesList.push(sub);
      }
 
    return { actives: activesList, inactives: inactivesList };
  }, [subscriptions]);
 
  const handleShowMoreClick = (type: "active" | "inactive") => {
    setShowMore((prev) => ({ ...prev, [type]: true }));
  };
 
  return (
    <>
      <SubscriptionSection
        title="Active Subscriptions"
        subscriptions={actives}
        showAll={showMore.active}
        onShowMore={() => handleShowMoreClick("active")}
        customerEmail={customerEmail}
        customerName={customerName}
        merchantID={merchantID}
      />
      <SubscriptionSection
        title="Previous Subscriptions"
        subscriptions={inactives}
        showAll={showMore.inactive}
        onShowMore={() => handleShowMoreClick("inactive")}
        customerEmail={customerEmail}
        customerName={customerName}
        merchantID={merchantID}
      />
    </>
  );
};
 
const getVisibleSubscriptions = (
  subscriptions: RecurringPurchasesListItem[],
  showAll: boolean,
) => (showAll ? subscriptions : subscriptions.slice(0, 5));
 
const SubscriptionSection = ({
  title,
  subscriptions,
  showAll,
  onShowMore,
  customerName,
  customerEmail,
  merchantID,
}: {
  title: string;
  subscriptions: RecurringPurchasesListItem[];
  showAll: boolean;
  customerName: string;
  customerEmail: string;
  merchantID?: number;
  onShowMore: () => void;
}) => {
  const { palette } = useAppTheme();
  return (
    <>
      <SectionCardBase
        leftTitle={`${title} (${subscriptions.length})`}
        childrenContainerSx={{
          padding: 0,
          borderRadius: "20px",
          overflow: "hidden",
        }}
      >
        {subscriptions.length > 0 ? (
          getVisibleSubscriptions(subscriptions, showAll).map(
            (subscription, i) => (
              <GiveMotionWrapper key={i} delay={30}>
                <SubscriptionItem
                  subscription={subscription}
                  isFirst={i === 0}
                  isLast={i === 4}
                  customerName={
                    customerName.trim() || subscription.cardholderName
                  }
                  customerEmail={customerEmail}
                  merchantID={merchantID}
                />
              </GiveMotionWrapper>
            ),
          )
        ) : (
          <SubscriptionsEmptyState />
        )}
      </SectionCardBase>
      {subscriptions.length > 5 && !showAll && (
        <Box sx={{ marginTop: "-40px" }}>
          <GiveButton
            label="Show More"
            onClick={onShowMore}
            sx={{
              margin: "24px 0px",
              width: "100%",
              height: "42px",
              borderRadius: "40px",
              padding: "11px 20px",
              backgroundColor: palette.primitive?.transparent["darken-5"],
              border: "none",
              color: palette.primitive?.neutral[90],
              "&:hover": {
                backgroundColor: palette.primitive?.neutral[10],
              },
            }}
          />
        </Box>
      )}
    </>
  );
};