All files / src/sections/PayBuilder/items PaymentFormItem.tsx

63.63% Statements 14/22
45% Branches 9/20
50% Functions 2/4
61.9% Lines 13/21

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                                                              45x             45x                 2x               2x   2x   2x           2x   2x   2x 2x                       2x     2x                                                                                                                                                                               45x                                
import { Stack } from "@mui/material";
import { Box } from "@mui/material";
import { DotsSixVerticalIcon, PencilSimpleIcon, TrashIcon } from "@phosphor-icons/react";
import GiveAvatar from "@shared/Avatar/GiveAvatar";
import GiveIconButton from "@shared/IconButton/GiveIconButton";
import GiveText from "@shared/Text/GiveText";
import { styled, useAppTheme } from "@theme/v2/Provider";
import { ProductItemType } from "../types";
import { addSizeToImage } from "@utils/image.helpers";
import GiveSwitch from "@shared/Switch/GiveSwitch";
import { useSortable } from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities";
import GiveTruncateText from "@shared/Text/GiveTruncateText";
import { CampaignMapperValues } from "features/Minibuilders/PaymentFormMinibuilder/useCreateCampaignFn";
import { paymentTypeSuffixes } from "./AddProductItemModal";
import useCheckFormType from "../components/hooks/useCheckFormType";
import { parseAmount } from "@utils/index";
import { CURRENCY } from "@constants/constants";
 
type Props = {
  item: ProductItemType;
  handleUpdateItem: (item: ProductItemType) => void;
  handleEditItem: () => void;
  formType?: CampaignMapperValues;
  handleDeleteItem: ({ id, title }: { id: string; title: string }) => void;
  // AC006 — assigned rows summary for this ticket (e.g. "Rows C-D"), shown by the price.
  seatingRowsLabel?: string;
  // AC006 — error when seating is on and this ticket has no rows assigned yet.
  seatingRowsError?: string;
};
 
const recurringTags: Record<string, string> = {
  once: "",
  monthly: "Monthly",
  quarterly: "Quarterly",
  yearly: "Yearly",
};
 
const PaymentFormItem = ({
  item,
  handleUpdateItem,
  handleEditItem,
  formType,
  handleDeleteItem,
  seatingRowsLabel,
  seatingRowsError,
}: Props) => {
  const { palette } = useAppTheme();
  const {
    isDragging,
    attributes,
    listeners,
    setNodeRef,
    transform,
    transition,
  } = useSortable({ id: item?.id });
 
  const { isInvoice } = useCheckFormType();
 
  const style = {
    zIndex: +isDragging,
    transform: CSS.Translate.toString(transform),
    transition,
  };
 
  const thumbnail = addSizeToImage(item?.thumbnail || "", "small");
 
  const { in_stock, id, title, amount, paymentType } = item;
 
  let recurrenceText = "";
  switch (formType) {
    case "membership":
      recurrenceText = paymentTypeSuffixes[paymentType];
      break;
    case "product":
      recurrenceText = recurringTags[paymentType];
      break;
    // This case is added since in BE it's called standard and in FE it's product. as shown in useCheckFormType. TODO: Remove after refactor
    case "standard":
      recurrenceText = recurringTags[paymentType];
      break;
    default:
      recurrenceText = "";
  }
 
  return (
    <Container ref={setNodeRef} style={style} data-testid="product-item">
      <Stack direction="row" spacing={2.5} alignItems="center">
        {thumbnail && (
          <GiveAvatar size="64px" shape="square" imageUrl={thumbnail} />
        )}
        <Stack spacing={0.5}>
          <GiveTruncateText variant="bodyS" fontWeight="500" lineClamp={1}>
            {item?.title}
          </GiveTruncateText>
          <Stack direction="row" alignItems="center" gap="8px" flexWrap="wrap">
            <GiveText variant="bodyS">
              {item?.amount} {CURRENCY} {recurrenceText}
            </GiveText>
            {seatingRowsLabel ? (
              <GiveText variant="bodyS">{seatingRowsLabel}</GiveText>
            ) : null}
          </Stack>
          {seatingRowsError ? (
            <GiveText variant="bodyXS" color="error1">
              {seatingRowsError}
            </GiveText>
          ) : null}
        </Stack>
      </Stack>
      <Stack direction="row" spacing={2} alignItems="center">
        {isInvoice ? (
          <Stack direction="row" spacing={0.5}>
            <GiveIconButton
              Icon={TrashIcon}
              size="small"
              variant="ghost"
              color={palette.primitive?.error["50"]}
              onClick={() => handleDeleteItem({ id: id, title: title })}
            />
            <GiveIconButton
              Icon={PencilSimpleIcon}
              size="small"
              variant="ghost"
              onClick={handleEditItem}
            />
            <Stack>
              <GiveText variant="bodyS" color="primary" textAlign="end">
                {parseAmount(
                  parseFloat((amount || "").replace(/,/g, "")) *
                    Number(in_stock),
                )}{" "}
                {CURRENCY}
              </GiveText>
              <GiveText variant="bodyXXS" color="secondary">
                {amount} {CURRENCY} x {in_stock}
              </GiveText>
            </Stack>
          </Stack>
        ) : (
          <>
            <GiveIconButton
              Icon={PencilSimpleIcon}
              color={palette.icon?.["icon-primary"]}
              size="small"
              variant="ghost"
              className="edit-button"
              onClick={handleEditItem}
              data-testid="edit-item-button"
            />
            <GiveSwitch
              aria-label="display item"
              checked={item?.display || false}
              onChange={() =>
                handleUpdateItem({ ...item, display: !item?.display })
              }
            />
            <GiveIconButton
              Icon={DotsSixVerticalIcon}
              color={palette.icon?.["icon-secondary"]}
              size="small"
              variant="ghost"
              style={{ cursor: isDragging ? "grabbing" : "grab" }}
              {...attributes}
              {...listeners}
            />
          </>
        )}
      </Stack>
    </Container>
  );
};
 
const Container = styled(Box)(({ theme }) => ({
  borderBottom: `1px solid ${theme.palette.border?.primary}`,
  padding: "16px 0",
  display: "flex",
  justifyContent: "space-between",
  [theme.breakpoints.up("v2_md")]: {
    "& .edit-button": {
      visibility: "hidden",
    },
    "&:hover .edit-button": {
      visibility: "visible",
    },
  },
}));
 
export default PaymentFormItem;