All files / src/features/Merchants/MerchantSidePanel/WithRepository/Challenges ChallengeItemMenu.tsx

50% Statements 7/14
40% Branches 8/20
12.5% Functions 1/8
50% Lines 7/14

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                                                                                                              4x 4x         4x   4x               4x                       4x                                                                                                       4x                    
import {
  BellRingingIcon,
  CheckIcon,
  PencilSimpleIcon,
  ProhibitIcon,
  TrashIcon,
} from "@phosphor-icons/react";
import ContextualMenu from "@shared/ContextualMenu/ContextualMenu";
import { ContextualMenuOptionProps } from "@shared/ContextualMenu/ContextualMenu.types";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { useNotifications } from "./hooks/useNotifications";
import { TChallange } from "./types";
import { BPVAA_DISABLED_TEXT } from "features/Merchants/MerchantSidePanel/constants";
import { NO_PAH_MESSAGE } from "@constants/permissions";
import { useMerchantSidePanelContext } from "@features/Merchants/MerchantSidePanel/Provider/MerchantSidePanelProvider";
 
interface Props {
  anchorEl: HTMLElement | null;
  handleClose: () => void;
  isEDD?: boolean;
  currentChallenge?: TChallange;
  openConversation: ({
    convTag,
    challengeID,
  }: {
    convTag: string;
    challengeID: number;
  }) => void;
  doneDisabled: boolean;
  handleOpenChallenge: (
    challenge: TChallange,
    handleOpenConversation: () => void,
  ) => void;
  handleDeleteChallenge: (challenge: TChallange) => void;
  handleEdit: (challenge: TChallange) => void;
  isPAHAdded: number | "";
}
 
export default function ChallengeItemMenu({
  anchorEl,
  handleClose,
  isEDD,
  currentChallenge,
  openConversation,
  doneDisabled,
  handleOpenChallenge,
  handleDeleteChallenge,
  handleEdit,
  isPAHAdded,
}: Props) {
  const {
    data: {
      merchantInfo: { merchantID },
      status: { statusName },
    },
  } = useMerchantSidePanelContext();
  const { openNotifyMerchantModal } = useNotifications({
    merchantID,
    statusName,
  });
 
  const { isMobileView } = useCustomThemeV2();
 
  const openConv = () => {
    currentChallenge &&
      openConversation({
        convTag: "",
        challengeID: currentChallenge?.id,
      });
  };
 
  const openNotification = () => {
    openNotifyMerchantModal({
      challengeTypeName: currentChallenge?.challengeTypeName,
      challengeID: currentChallenge?.id,
      topicName: isEDD
        ? currentChallenge?.name
        : currentChallenge?.merchantDisplayTitle,
      hideInputs: ["module"],
      defaultMessage: currentChallenge?.predefinedReason || undefined,
    });
  };
 
  const menuOptions: Omit<ContextualMenuOptionProps, "children">[] = [
    {
      text: "Notify Merchant",
      onClick: () => {
        return openNotification();
      },
      IconRight: BellRingingIcon,
      disabled: !isPAHAdded,
      tooltipTitle: NO_PAH_MESSAGE,
      disableTooltip: !!isPAHAdded,
    },
    {
      text: "Edit",
      onClick: currentChallenge
        ? () => handleEdit(currentChallenge)
        : undefined,
      IconRight: PencilSimpleIcon,
      hidden: !isEDD,
    },
    {
      text: "Done",
      onClick: currentChallenge
        ? () => handleOpenChallenge(currentChallenge, openConv)
        : undefined,
      IconRight: CheckIcon,
      hidden: currentChallenge?.statusName === "closed",
      disabled: doneDisabled,
      tooltipTitle: BPVAA_DISABLED_TEXT,
      disableTooltip: !doneDisabled,
    },
    {
      text: "Reject",
      onClick: currentChallenge
        ? () => handleOpenChallenge(currentChallenge, openConv)
        : undefined,
      IconRight: ProhibitIcon,
      type: "destructive",
      hidden:
        currentChallenge?.statusName === "closed" ||
        currentChallenge?.statusName === "rejected",
    },
    {
      text: "Delete",
      onClick: currentChallenge
        ? () => handleDeleteChallenge(currentChallenge)
        : undefined,
      IconRight: TrashIcon,
      type: "destructive",
      hidden: !isEDD,
    },
  ];
 
  return (
    <ContextualMenu
      anchorEl={anchorEl}
      color={isMobileView ? "secondary" : "tertiary"}
      texture={isMobileView ? "solid" : "blurred"}
      options={menuOptions}
      handleClose={handleClose}
    />
  );
}