All files / src/components/DonationList DonationsBanner.tsx

81.48% Statements 22/27
42.85% Branches 6/14
57.14% Functions 4/7
81.48% Lines 22/27

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                                                13x                 13x 210x 210x   210x 81x     210x     13x   13x 6x 6x 6x 6x 6x   6x 6x                                   6x                                               36x     6x   6x                                                                                                         6x         6x                                
import * as React from "react";
// router
import { useParams } from "react-router-dom";
// components
import { MenuItem } from "@common/Menu/NewDropdownMenu";
import { StatProps } from "@common/NewStats/Stat";
import NewProductListingBanner from "@components/Product/NewProductListingBanner/NewProductListingBanner";
import { DELETE_DENY_MESSAGE, EDIT_DENY_MESSAGE } from "@constants/permissions";
import { DonationType } from "@customTypes/products/fundraiser.types";
import NiceModal from "@ebay/nice-modal-react";
import { palette } from "@palette";
import PublicURLShareModal from "@sections/PayBuilder/LaunchStep/PublicURLShare/PublicURLShareModal";
import { buildMerchantEndpoints } from "@services/api/utils.api";
import { roundToNearestTenth } from "@utils/index";
import { useProductPermission } from "features/Permissions/AccessControl/hooks";
import {
  DELETE_CONFIRMATION_MODAL,
  EDIT_FUNDRAISER_MODAL,
  MERCHANT_PORTAL_SHARE_MODAL,
} from "modals/modal_names";
import { useNavigate } from "react-router";
import { useGetRebrandedFormTypes } from "@sections/PayBuilder/utils";
import { useExpandedActions } from "@sections/PayBuilder/hooks/useExpandedActions";
 
const mapTypenameToProductType = {
  fundraiser: "fundraisers",
  sweepstake: "sweepstakes",
  event: "events",
  membership: "memberships",
  invoice: "invoices",
  standard: "standard",
};
 
export const usePublicFormUrl = () => {
  const { id } = useParams();
  const [textToCopy, setTextToCopy] = React.useState("");
 
  React.useEffect(() => {
    setTextToCopy(`/${id}`);
  }, []);
 
  return { id, textToCopy, composedUrl: window.location.origin + textToCopy };
};
 
const hideItemsSoldTypes = ["fundraiser"];
 
const DonationsBanner = ({ data }: { data: DonationType }) => {
  const { id, textToCopy } = usePublicFormUrl();
  const navigate = useNavigate();
  const mappedTypeName = mapTypenameToProductType[data?.typeName];
  const rebrandedFormTypes = useGetRebrandedFormTypes();
  const { editPaymentForm } = useExpandedActions(data, EDIT_FUNDRAISER_MODAL);
 
  const url = buildMerchantEndpoints(`products/${id}`);
  const handleDelete = () => {
    NiceModal.show(DELETE_CONFIRMATION_MODAL, {
      variant: "product",
      itemName: data?.name,
      productVariant: mappedTypeName,
      url: url,
    });
  };
 
  // const handleOpenRevenues = () => {
  //   NiceModal.show(REVENUES_PRODUCT_MODAL, {
  //     type: "Fundraiser",
  //     VisitorsViews: false,
  //     Visitors: false,
  //   });
  //   handleCloseMenu();
  // };
 
  const parseStats: StatProps[] = [
    {
      isAmount: true,
      title: "Total Amount",
      value: data?.sumApprovedPurchases / 100,
      isTotal: true,
    },
    {
      isAmount: true,
      title: "Average",
      value: data?.averageApprovedPurchases / 100,
    },
    { title: "Purchases", value: data?.totalApprovedPurchases || 0 },
    {
      title: "Items Sold",
      value: data?.totalItemsSold || 0,
      hidden: hideItemsSoldTypes.includes(data?.typeName || ""),
    },
    { title: "Visitors", value: data?.visitors },
    {
      title: "Conversion",
      value: roundToNearestTenth(data?.conversionRate ?? 0),
      percent: true,
    },
  ].filter((item) => !item.hidden);
 
  const { isEditProductAllowed, isDeleteProductAllowed } =
    useProductPermission();
 
  const menuItems: MenuItem[] = [
    {
      label: "Edit",
      onSelect: editPaymentForm,
      disabled: !isEditProductAllowed,
      tooltipProps: {
        show: !isEditProductAllowed,
        message: EDIT_DENY_MESSAGE,
      },
    },
    {
      label: "Open",
      onSelect: () => navigate(textToCopy, { state: { preview: true } }),
    },
    {
      label: "Share",
      onSelect: () => {
        if (rebrandedFormTypes.includes(data?.typeName)) {
          return NiceModal.show(PublicURLShareModal, {
            idFromProps: id,
            typeFromProps:
              data?.typeName === "standard" ? "Product" : data?.typeName,
          });
        }
        NiceModal.show(MERCHANT_PORTAL_SHARE_MODAL, {
          type: "fundraiser",
          title: "Share Fundraiser",
          productId: id,
        });
      },
    },
    // {
    //   label: "Revenue Sources",
    //   onSelect: handleOpenRevenues,
    // },
    // {
    //   label: "Developer API",
    //   onSelect: handleOpenDeveloperAPI,
    // },
    {
      label: "Delete",
      onSelect: handleDelete,
      labelProps: {
        color: palette.error.hover,
      },
      disabled: !isDeleteProductAllowed,
      tooltipProps: {
        show: !isDeleteProductAllowed,
        message: DELETE_DENY_MESSAGE,
      },
    },
  ];
 
  const previewLink = [
    mappedTypeName === "standard" ? "standards" : mappedTypeName,
    id,
  ].join("/");
 
  return (
    <NewProductListingBanner
      isEditProductAllowed={isEditProductAllowed}
      handleEdit={editPaymentForm}
      previewLink={previewLink}
      type={mappedTypeName}
      stats={parseStats}
      textToCopy={textToCopy}
      data={data}
      menuItems={menuItems}
      parseStats={parseStats}
    />
  );
};
 
export default React.memo(DonationsBanner);