All files / src/features/Products/Invoices components.tsx

83.33% Statements 35/42
50% Branches 40/80
92.3% Functions 12/13
85.36% Lines 35/41

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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307                                          3x                 44x                                                                               3x             22x   22x 22x   22x 22x       22x 22x     22x         22x                                                                             66x   45x                                       3x           22x 22x                                                                             22x     22x 22x   22x     3x 22x 22x   22x 22x                       22x     22x                                                                 22x 22x                                   22x         3x         22x   22x       176x            
import GiveTruncateText from "@shared/Text/GiveTruncateText";
import { InvoiceProductDataType } from "../hooks/invoiceColumns/types";
import { Stack, SxProps } from "@mui/material";
import GiveText from "@shared/Text/GiveText";
import { useAppTheme } from "@theme/v2/Provider";
import { TextProps } from "@shared/Text/giveText.types";
import { Grid } from "@mui/material";
import { Box } from "@mui/material";
import SectionCardBase from "@shared/SidePanel/components/SectionCard/SectionCardBase";
import { getDaysDifferenceFromNow } from "./utils";
import { DotIcon } from "@phosphor-icons/react";
import { Chip, InvoiceAmount } from "./atoms";
import { GiveSectionItem } from "features/Merchants/MerchantSidePanel/GiveMerchantFile/GiveSectionItem";
import { parseAmount } from "@utils/index";
import { useMemo } from "react";
import { omitLeadingZeros } from "@utils/helpers";
import { formatCustomerPhoneNumber } from "features/MerchantPortal/Customer/Modals/utils";
import { ContextualMenuOptionProps } from "@shared/ContextualMenu/ContextualMenu.types";
import { formatInUTCMoment } from "@utils/date.helpers";
import GiveLink from "@shared/Link/GiveLink";
 
export const CustomerInfoName = ({
  row,
  variant,
  sx,
}: {
  row: InvoiceProductDataType;
  variant?: TextProps["variant"];
  sx?: SxProps;
}) => {
  return row.customer ? (
    <GiveTruncateText sx={sx ?? {}} lineClamp={1} variant={variant ?? "bodyS"}>
      {`${row.customer.firstName ?? ""} ${row.customer.lastName ?? ""}`}
    </GiveTruncateText>
  ) : (
    <>{"-"}</>
  );
};
 
export function CustomerCol({
  row,
  isDesktopView,
}: {
  row: InvoiceProductDataType;
  isDesktopView?: boolean;
}) {
  const isOneOfNamesDefined = row.customer?.firstName || row.customer?.lastName;
 
  return (
    <Stack direction="row" spacing={2} alignItems="center" width="100%">
      <Stack spacing={0.5} padding={isDesktopView ? "10px" : 0}>
        <CustomerInfoName row={row} />
        {row.customer && (
          <GiveTruncateText
            color={isOneOfNamesDefined ? "secondary" : "primary"}
            variant={isOneOfNamesDefined ? "bodyXS" : "bodyS"}
            lineClamp={1}
            sx={{
              display: isDesktopView || !isOneOfNamesDefined ? "block" : "none",
            }}
          >
            {row.customer.email}
          </GiveTruncateText>
        )}
        {!isDesktopView && <InvoiceAmount amount={row.amount} />}
      </Stack>
    </Stack>
  );
}
 
export const SidePanelMainHeader = ({
  invoiceData,
  commonInvoiceActions,
}: {
  invoiceData: InvoiceProductDataType;
  commonInvoiceActions: ContextualMenuOptionProps[];
}) => {
  const { palette } = useAppTheme();
 
  const overdueDays = getDaysDifferenceFromNow(invoiceData.endsAt);
  const greaterThanOnePlural = overdueDays > 1 ? "s" : "";
  const isOneOfNamesDefined =
    invoiceData.customer?.firstName || invoiceData.customer?.lastName;
  const lastestTransactionStatus = invoiceData.variantsList.length
    ? invoiceData.variantsList[0].displayStatus
    : "";
 
  const isCancelled = invoiceData.statusDisplayName === "Canceled";
  const isPaid = invoiceData.statusDisplayName === "Paid";
 
  const showOverdueCounter =
    overdueDays > 0 &&
    !["Voided", "Refunded"].includes(lastestTransactionStatus) &&
    !isCancelled &&
    !isPaid;
 
  return (
    <Stack gap={1}>
      <Grid container gap={2} alignItems="center">
        <GiveText fontSize="36px" fontWeight={300} color="primary">
          {invoiceData.reference}
        </GiveText>
        <Chip statusDisplayName={invoiceData.statusDisplayName} />
      </Grid>
      {invoiceData.customer && (
        <Box sx={{ display: "flex", alignItems: "center" }}>
          <GiveText style={{ color: palette.text.secondary, fontSize: "14px" }}>
            {`Billed to`}&nbsp;
          </GiveText>
          {isOneOfNamesDefined ? (
            <CustomerInfoName variant="bodyS" row={invoiceData} />
          ) : (
            <GiveTruncateText variant="bodyS" lineClamp={1}>
              {invoiceData.customer?.email ?? "-"}
            </GiveTruncateText>
          )}
          <DotIcon size={20} weight="bold" />
          <InvoiceAmount
            amount={invoiceData.amount}
            color="primary"
            variant="bodyS"
          />
          {showOverdueCounter && (
            <>
              <DotIcon size={20} weight="bold" />
              <GiveText
                fontSize="14px"
                color="error1"
              >{`${overdueDays} Day${greaterThanOnePlural} Overdue`}</GiveText>
            </>
          )}
        </Box>
      )}
      <Stack direction="row" gap="20px" sx={{ marginTop: "8px" }}>
        {commonInvoiceActions
          .filter((item) => !item.hidden)
          .map(({ text, IconRight, onClick }, index) => {
            return (
              <GiveLink
                key={index}
                component="button"
                color={palette.primitive?.blue[100]}
                onClick={onClick as any}
                Icon={IconRight as any}
                sx={{
                  gap: "8px",
                }}
              >
                {text}
              </GiveLink>
            );
          })}
      </Stack>
    </Stack>
  );
};
 
export const SidePanelSecondHeader = ({
  invoiceData,
}: {
  invoiceData: InvoiceProductDataType;
}) => {
  const isOneOfNamesDefined =
    invoiceData.customer?.firstName || invoiceData.customer?.lastName;
  return (
    <SectionCardBase>
      <Grid container width="75%">
        <Stack gap={1}>
          <GiveText variant="bodyS" color="secondary">
            Billed to
          </GiveText>
          <Box>
            {isOneOfNamesDefined ? (
              <CustomerInfoName variant="bodyS" row={invoiceData} />
            ) : (
              "-"
            )}
            <GiveTruncateText variant="bodyS" lineClamp={1} color="secondary">
              {invoiceData.customer?.email ?? "-"}
            </GiveTruncateText>
          </Box>
        </Stack>
        <Stack sx={{ marginLeft: "auto" }} gap={1}>
          <GiveText variant="bodyS" color="secondary">
            Due Date
          </GiveText>
          <Box>
            <GiveText variant="bodyS">
              {invoiceData.endsAt
                ? formatInUTCMoment(invoiceData.endsAt, "MMM DD, YYYY")
                : "-"}
            </GiveText>
            <GiveText variant="bodyS" color="secondary">
              {invoiceData.endsAt ? `11:59 PM ET` : "-"}
            </GiveText>
          </Box>
        </Stack>
      </Grid>
    </SectionCardBase>
  );
};
 
function formatedPhoneNumber(phoneNumber?: string) {
  Iif (!phoneNumber) return {};
 
  const noZeroesPhone =
    phoneNumber && phoneNumber.length > 4 ? omitLeadingZeros(phoneNumber) : "1";
  const formattedPhone = formatCustomerPhoneNumber(`+${noZeroesPhone}`);
 
  return { noZeroesPhone, formattedPhone };
}
 
const useOverviewItemsBuilder = (data: InvoiceProductDataType) => {
  const { customer } = data;
  const isOneOfNamesDefined = customer?.firstName || customer?.lastName;
 
  const items = useMemo(() => {
    return [
      {
        title: "Customer",
        value: isOneOfNamesDefined
          ? `${data.customer.firstName ?? ""} ${data.customer.lastName ?? ""}`
          : "-",
      },
      {
        title: "Customer Email",
        value: customer?.email,
      },
      (function parsePhoneNumber() {
        const { formattedPhone } = formatedPhoneNumber(
          `${customer?.phoneNumber ?? ""}`,
        );
        return {
          title: "Customer Phone Number",
          value: formattedPhone ?? "-",
          isPhoneNumber: true,
        };
      })(),
      {
        title: "Reference",
        value: data.reference,
      },
      {
        title: "Issue Date",
        value: data.startsAt
          ? formatInUTCMoment(data.startsAt, "MMM DD, YYYY")
          : "-",
      },
      {
        title: "Due Date",
        value: data.endsAt
          ? formatInUTCMoment(data.endsAt, "MMM DD, YYYY")
          : "-",
      },
      ...(data.statusDisplayName === "Canceled"
        ? [
            {
              title: "Canceled Date",
              value: formatInUTCMoment(data.statusUpdatedAt, "MMM DD, YYYY"),
            },
          ]
        : []),
      {
        title: "Billing Address",
        value: (function buildAddressString() {
          Eif (!data.customer?.billingAddress) {
            return "-";
          }
          const { country, line1, line2, state } = data.customer.billingAddress;
 
          const lines = [line2, line1].filter(Boolean).join(" ");
 
          const stateCountry = [state, country].filter(Boolean).join(", ");
 
          return [lines, stateCountry].filter(Boolean).join(", ");
        })(),
      },
      {
        title: "Invoice Total (USD)",
        value: parseAmount(data.amount / 100),
      },
    ];
  }, [data]);
 
  return {
    items,
  };
};
 
export const SidePanelOverview = ({
  invoiceData,
}: {
  invoiceData: InvoiceProductDataType;
}) => {
  const { items } = useOverviewItemsBuilder(invoiceData);
 
  return (
    <SectionCardBase leftTitle="Overview" sx={{ paddingTop: "24px" }}>
      <Grid container spacing={2}>
        {items.map((item, index) => (
          <GiveSectionItem key={index} item={item} />
        ))}
      </Grid>
    </SectionCardBase>
  );
};