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

100% Statements 22/22
78.33% Branches 47/60
100% Functions 3/3
100% Lines 21/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 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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373                                                                                                                  2x                                     44x 44x 44x 44x 44x 44x 44x 44x       44x   44x                                                                       44x 44x       44x             44x   44x                                                                                                                                                                                         308x                                                                                                                                                                                                                       44x       44x                 286x 206x                                    
import { BODY_DATA } from "@common/CampaignCard/CampaignDetailsModal/types";
import { CampaignType } from "@common/Campaigns/hooks/useReportMenuActions";
import FadeUpWrapper from "@components/animation/FadeUpWrapper";
import LoadingSpinner from "@components/Snipper/LoadingSpinner";
import { EDIT_DENY_MESSAGE } from "@constants/permissions";
import { Grid, List, Stack } from "@mui/material";
import { PaletteIcon, ShareIcon } from "@phosphor-icons/react";
import { useFormatPaymentFormDate } from "@sections/PayBuilder/utils";
import GiveButton from "@shared/Button/GiveButton";
import GiveLink from "@shared/Link/GiveLink";
import SectionCardBase from "@shared/SidePanel/components/SectionCard/SectionCardBase";
import GiveText from "@shared/Text/GiveText";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import { useAppTheme } from "@theme/v2/Provider";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { GiveSectionItem } from "features/Merchants/MerchantSidePanel/GiveMerchantFile/GiveSectionItem";
import { useProductPermission } from "features/Permissions/AccessControl/hooks";
import { memo } from "react";
import { StyledSectionStack } from "../styles";
import FigureComponent from "./chart/Figure";
import VerticalBarChart from "./chart/VerticalBarChart";
import { Inventory } from "./InventoryItem";
import PanelEmptyState from "./PanelEmptyState";
import { HeroImage } from "@sections/PayBuilder/components/products/Hero/HeroImage";
import { MediaItem } from "@sections/PayBuilder/provider/provider.type";
import { SweepstakeTime } from "@sections/PayBuilder/views/fundraiser/components/SweepstakeTime";
import ProgressBarWithAmount from "@sections/PayBuilder/views/fundraiser/components/ProgressBarWithAmount";
import { calculatePercentage } from "@sections/PayBuilder/views/fundraiser/FundraiserRightCard";
import { WinnerComponent } from "@sections/PayBuilder/views/fundraiser/components/Atom.component";
import { useImageSource } from "@sections/PayBuilder/Forms/About/utils";
import EventsEndDateSection from "./EventsEndDateSection";
import { useInvoicesSidePanel } from "features/Products/Invoices/hooks";
import { TransactionsSection } from "./TransactionsSections";
import CampaignReviewsSection from "./reviews/CampaignReviewsSection";
import { ContextualMenuOptionProps } from "@shared/ContextualMenu/ContextualMenu.types";
 
interface Props {
  handleEdit?: () => void;
  handleShare?: () => void;
  BodyData: BODY_DATA;
  handleExpand?: () => void;
  /** Dismisses the panel — the reviews card navigates away from it. */
  closePanel?: () => void;
  isloading: boolean;
  chooseTransaction?: (id: string) => void;
  selectedTransaction?: string | null;
  hasEnded?: boolean;
  handleADBRedirection?: () => void;
  isEditFormStyleDisabled?: boolean;
  imageURL?: string;
  handlePreview: () => void;
  hasEndedMessage: string;
  campaign: CampaignType;
  campaignId: number;
  commonInvoiceActions: ContextualMenuOptionProps[];
}
 
const Body = ({
  BodyData,
  imageURL,
  isloading,
  hasEnded,
  handleADBRedirection,
  handleShare,
  handleEdit,
  handleExpand,
  closePanel,
  isEditFormStyleDisabled,
  hasEndedMessage,
  selectedTransaction,
  chooseTransaction,
  campaign,
  campaignId,
  commonInvoiceActions,
}: Props) => {
  const { isADBEnabled, isShowEventTicketsSoldEnabled, isEventReviewsEnabled } =
    useGetFeatureFlagValues();
  const { isEditProductAllowed } = useProductPermission();
  const { palette } = useAppTheme();
  const disabledADB = hasEnded || !isEditProductAllowed;
  const isEvent = campaign === "event";
  const isFundraiser = campaign === "fundraiser";
  const isInvoice = campaign === "invoice";
  const percentage = calculatePercentage(
    String(BodyData?.sumApprovedPurchases / 100),
    String(BodyData?.targetValue / 100),
  );
  const showProgressBar = (BodyData?.targetValue ?? 0) > 0;
 
  const overviewItems = [
    {
      title: "Items Sold",
      value: BodyData?.totalItemsSold,
      hide: isFundraiser || isInvoice || isEvent,
    },
    {
      title: "Conversion Rate",
      value: BodyData?.conversion ? `${BodyData?.conversion} %` : 0,
    },
    {
      title: "Visitors",
      value: BodyData?.Visitors || 0,
    },
    {
      title: "Purchases",
      value: BodyData?.totalApprovedPurchases || 0,
    },
    {
      title: "Average (USD)",
      value: parseFloat(BodyData.averageApprovedPurchases as string)
        ? BodyData.averageApprovedPurchases
        : 0,
    },
    {
      title: "Average Tickets Purchased",
      value: BodyData.averageTotalSoldVariants,
      hide: !isEvent,
    },
    {
      title: "Tickets Sold",
      value: BodyData.totalSoldVariants,
      hide: !isEvent || !isShowEventTicketsSoldEnabled,
    },
  ];
 
  const endDate = (BodyData.campaignEndsAt || BodyData.startsAt || 0) * 1000;
  const { data } = useImageSource(
    BodyData?.imageStyles ? (BodyData?.imageStyles as MediaItem) : "",
  );
 
  const { invoicesSections } = useInvoicesSidePanel({
    chooseTransaction,
    handleExpand,
    selectedTransaction,
    data: BodyData,
    commonInvoiceActions,
  });
  const { formatPaymentFormDate } = useFormatPaymentFormDate();
 
  const sectionsList = [
    {
      section: (
        <HeroImage
          canvasData={{
            resizeType: "fill",
          }}
          src={data}
        />
      ),
      delay: 100,
      hidden: !imageURL,
    },
    {
      section: (
        <>
          <GiveText color="primary" variant="h2">
            {BodyData.name}
          </GiveText>
          <GiveText color="secondary" variant="bodyS" mt={1}>
            Created:{" "}
            {
              formatPaymentFormDate(
                BodyData.createdAt,
                false,
                undefined,
                "abbreviated",
              ) as string
            }
          </GiveText>
        </>
      ),
      delay: 100,
    },
    {
      section: (
        <Stack
          flexDirection="row"
          alignItems="center"
          justifyContent="space-between"
        >
          <Stack flexDirection="row" alignItems="center" gap="20px">
            {isADBEnabled && (
              <GiveTooltip
                width="compact"
                alignment="left"
                title={
                  !isEditProductAllowed ? EDIT_DENY_MESSAGE : hasEndedMessage
                }
                placement="top"
                color="default"
                disableHoverListener={!disabledADB}
              >
                <GiveLink
                  component="button"
                  onClick={handleADBRedirection}
                  Icon={PaletteIcon}
                  color="secondary"
                  disabled={disabledADB}
                >
                  Advanced Builder
                </GiveLink>
              </GiveTooltip>
            )}
 
            <GiveLink
              component="button"
              onClick={handleShare}
              Icon={ShareIcon}
              color="secondary"
              data-testid="campaign-share-button"
            >
              Share
            </GiveLink>
          </Stack>
 
          <GiveButton
            color="light"
            size="large"
            onClick={handleEdit}
            variant="filled"
            label="Edit"
            disabled={isEditFormStyleDisabled}
            data-testid="campaign-edit-button"
          />
        </Stack>
      ),
    },
    {
      section: (
        <SectionCardBase leftTitle="Overview">
          <Grid container spacing={2}>
            {overviewItems.map((item, index) =>
              item?.hide ? null : <GiveSectionItem key={index} item={item} />,
            )}
          </Grid>
        </SectionCardBase>
      ),
    },
    {
      hidden: !(campaign === "event" || campaign === "sweepstake"),
      section: (
        <SectionCardBase>
          {campaign === "sweepstake" ? (
            <Stack spacing="20px">
              {showProgressBar && BodyData?.sumApprovedPurchases > 0 && (
                <ProgressBarWithAmount
                  totalContribution={BodyData?.sumApprovedPurchases / 100}
                  donationTarget={BodyData?.targetValue / 100}
                  customColor={palette.gradient?.["ocean-blue"]?.default}
                  percentage={percentage}
                />
              )}
              <SweepstakeTime endDate={endDate} campaignId={campaignId} />
              {hasEnded && BodyData?.sumApprovedPurchases > 0 && (
                <WinnerComponent {...BodyData.winner} />
              )}
            </Stack>
          ) : (
            <EventsEndDateSection
              endDate={endDate}
              hasEnded={hasEnded}
              hasEndedMessage={hasEndedMessage}
              showTime={Boolean(
                BodyData.includeTime && BodyData.campaignEndsAt,
              )}
            />
          )}
        </SectionCardBase>
      ),
    },
    {
      section: (
        <StyledSectionStack>
          {BodyData.total === 0 || BodyData.total === "0.00" ? (
            <PanelEmptyState section="chart" />
          ) : (
            <Stack
              direction="column"
              gap="10px"
              alignItems="stretch"
              marginRight="24px"
            >
              <FigureComponent isEvent={isEvent} total={BodyData.total} />
              <VerticalBarChart height={280} data={BodyData.statsData} />
            </Stack>
          )}
        </StyledSectionStack>
      ),
    },
 
    {
      hidden: !isEvent,
      section: (
        <SectionCardBase
          leftTitle="Inventory"
          childrenContainerSx={{
            padding: "16px",
          }}
          sx={{ paddingTop: "24px" }}
        >
          {BodyData.variants ? (
            <Inventory variants={BodyData.variants} />
          ) : (
            <PanelEmptyState section="inventory" />
          )}
        </SectionCardBase>
      ),
    },
    {
      section: (
        <TransactionsSection
          BodyData={BodyData as any}
          chooseTransaction={chooseTransaction}
          handleExpand={handleExpand}
          section="latestPurchases"
          selectedTransaction={selectedTransaction}
          campaign={campaign}
        />
      ),
    },
    {
      // PayBuilder 034 — post-event feedback, the last section of the panel.
      // AC001 places it "below the event map", but no frame in the Figma
      // section draws a map at any viewport and the panel has never had one, so
      // it appends here, after Latest Transactions (see the plan doc).
      //
      // Only for an event that has already ended: reviews cannot exist before
      // then, and the annotation on frame 7878-60582 is explicit that the card
      // itself is withheld until the event is over.
      hidden: !isEvent || !hasEnded || !isEventReviewsEnabled,
      section: (
        <CampaignReviewsSection
          productId={campaignId}
          onNavigate={closePanel}
        />
      ),
    },
  ];
 
  const usedSections =
    invoicesSections && campaign === "invoice"
      ? invoicesSections
      : sectionsList;
 
  return (
    <>
      <Stack flex={1} width="100%" gap="16px">
        {isloading ? (
          <LoadingSpinner />
        ) : (
          <>
            {usedSections.map(
              ({ section, delay, hidden = false }: any, index) => {
                if (hidden) return;
                return (
                  <FadeUpWrapper
                    key={index}
                    delay={(delay || 100) + index * 80}
                  >
                    {section}
                  </FadeUpWrapper>
                );
              },
            )}
          </>
        )}
      </Stack>
    </>
  );
};
 
export default memo(Body);