All files / src/features/Campaigns/CampaignDetails/components InventoryItem.tsx

88.46% Statements 23/26
61.9% Branches 13/21
77.77% Functions 7/9
95.65% Lines 22/23

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                                      2x   2x 4x   4x   4x     4x       4x     12x                               2x             12x 12x   12x 6x       12x   12x       12x 12x                 12x                   12x                                                                                       12x                               2x 20x                  
import { useCallback, useMemo, useState } from "react";
import { Stack } from "@mui/material";
import GiveText from "@shared/Text/GiveText";
import GiveTruncateText from "@shared/Text/GiveTruncateText";
import GiveThumbnail from "@shared/Thumbnail/GiveThumbnail";
import GiveProgressBar from "@shared/ProgressBar/GiveProgressBar";
import { styled, useAppTheme } from "@theme/v2/Provider";
import { addSizeToImage } from "@utils/image.helpers";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import GiveButton from "@shared/Button/GiveButton";
 
type Ticket = {
  imageURL: string | null;
  name: string;
  numSold: number;
  inventory: number;
  price: number;
};
 
const MAX_VISIBLE_VARIANTS = 10;
 
export const Inventory = ({ variants }: { variants: Ticket[] }) => {
  const [viewAll, setViewAll] = useState(false);
 
  const touggleView = () => setViewAll((v) => !v);
 
  const hasManyVariants = variants.length > MAX_VISIBLE_VARIANTS;
 
  const displayedVariants =
    viewAll || !hasManyVariants
      ? variants
      : variants.slice(0, MAX_VISIBLE_VARIANTS);
 
  return (
    <Stack gap="8px">
      {displayedVariants.map((variant, index) => {
        return <InventoryItem key={index} {...variant} />;
      })}
 
      {hasManyVariants && (
        <GiveButton
          size="large"
          variant="filled"
          color="light"
          label={viewAll ? "View Less" : "View More"}
          onClick={touggleView}
        />
      )}
    </Stack>
  );
};
 
export const InventoryItem = ({
  imageURL = "",
  name,
  numSold,
  inventory,
  price,
}: Ticket) => {
  const { palette } = useAppTheme();
  const { isMobileView } = useCustomThemeV2();
 
  const percentage = useMemo(
    () => (numSold / inventory) * 100,
    [numSold, inventory],
  );
 
  const formatPrice = useCallback(
    (cents: number): string => {
      Iif (cents === 0) {
        return "0";
      }
 
      const dollars = cents / 100;
      return dollars.toLocaleString("en-US", {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2,
      });
    },
    [price],
  );
 
  const sum = (
    <Stack alignItems="flex-end" gap="4px">
      <GiveText variant="bodyS" color="primary" whiteSpace="nowrap">
        {formatPrice(price)} USD
      </GiveText>
      <GiveText variant="bodyXS" color="secondary" whiteSpace="nowrap">
        Per Ticket
      </GiveText>
    </Stack>
  );
 
  return (
    <Container>
      <Stack direction="row" width="100%" gap="16px" alignItems="center">
        <GiveThumbnail
          size="medium"
          type="image"
          imageUrl={addSizeToImage(imageURL || "", "small")}
        />
        <Stack gap={inventory ? "8px" : "12px"} width="100%" maxWidth="280px">
          <Stack direction="row" justifyContent="space-between" gap="16px">
            <GiveTruncateText variant="bodyS" color="primary" lineClamp={2}>
              {name}
            </GiveTruncateText>
 
            {isMobileView && sum}
          </Stack>
          <Stack gap="8px">
            {inventory ? (
              <>
                <GiveProgressBar
                  showDot={false}
                  customColor={palette.gradient["ocean-blue"]?.default}
                  type="default"
                  value={percentage}
                  variant="custom"
                  height={4}
                />
                <Stack direction="row" justifyContent="space-between">
                  <TextStack label="Available:" value={inventory} />
                  <TextStack label="Sold:" value={numSold} />
                </Stack>
              </>
            ) : (
              <TextStack label="Sold:" value={numSold} />
            )}
          </Stack>
        </Stack>
      </Stack>
 
      {!isMobileView && sum}
    </Container>
  );
};
 
const Container = styled(Stack)(({ theme }) => ({
  padding: "12px",
  borderRadius: "8px",
  display: "grid",
  gridTemplateColumns: "minmax(0, 100%) auto",
  alignItems: "center",
  gap: "16px",
  "&:hover": {
    backgroundColor: theme.palette.primitive?.transparent["darken-5"],
  },
}));
 
type TextStackProps = {
  label: string;
  value: number | string;
};
const TextStack = ({ label, value }: TextStackProps) => (
  <Stack direction="row" gap="4px">
    <GiveText variant="bodyXS" color="secondary">
      {label}
    </GiveText>
    <GiveText variant="bodyXS" color="primary">
      {value}
    </GiveText>
  </Stack>
);