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

65.51% Statements 19/29
58.82% Branches 20/34
18.18% Functions 2/11
64.28% Lines 18/28

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                                                                                                                    4x 4x 4x           4x 4x       4x     4x 4x   4x   4x   4x                                                                                   4x         4x   4x 4x 4x   4x                                                                                                                                                                                                         21x                  
import { Box } from "@mui/material";
import { Stack } from "@mui/material";
import { FunnelSimpleIcon, PaperPlaneTiltIcon, XIcon } from "@phosphor-icons/react";
import GiveButton from "@shared/Button/GiveButton";
import GiveChip from "@shared/Chip/GiveChip";
import GiveUnderwritingProgressBar from "@shared/ProgressBar/GiveUnderwritingProgressBar";
import GiveText from "@shared/Text/GiveText";
import { styled, useAppTheme } from "@theme/v2/Provider";
import { iconMapper } from "./utils";
import GiveSearchBar from "@shared/SearchBar/GiveSearchBar";
import { useState } from "react";
import useTableSearch from "componentsV2/Table/hooks/useTableSearch";
import ContextualMenu from "@shared/ContextualMenu/ContextualMenu";
import { useNotifications } from "./hooks/useNotifications";
import { ContextualMenuOptionProps } from "@shared/ContextualMenu/ContextualMenu.types";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { TChallengeStatus } from "./types";
import GiveIconButton from "@shared/IconButton/GiveIconButton";
import GiveLink from "@shared/Link/GiveLink";
import { useEnterprisePermissions } from "@components/AcquirerEnterprises/CreateEnterprise/hooks/useEnterprisePermissions";
import { checkPortals } from "@utils/routing";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import { ACTION_DENY_MESSAGE } from "@constants/permissions";
import { useMerchantSidePanelContext } from "@features/Merchants/MerchantSidePanel/Provider/MerchantSidePanelProvider";
 
interface Props {
  name: string;
  identifier: string;
  completed: number;
  incomplete: number;
  disabled: number;
  status: "In Progress" | "Completed";
  isEmptyChallengeList: boolean;
  setChallengeStatusFilter: (v: TChallengeStatus) => void;
  challengeStatusFilter: TChallengeStatus[];
  handleResetFilters: () => void;
  handleUnreadFilter: (val: string) => void;
  hasUnreadMessagesFilter: string;
  handleUserTaggedFilter: (val: string) => void;
  userTaggedInFilter: string;
}
 
export default function ChallengeBodyTop({
  name,
  identifier,
  completed,
  incomplete,
  disabled,
  status,
  isEmptyChallengeList,
  setChallengeStatusFilter,
  challengeStatusFilter,
  handleResetFilters,
  handleUnreadFilter,
  hasUnreadMessagesFilter,
  handleUserTaggedFilter,
  userTaggedInFilter,
}: Props) {
  const { palette } = useAppTheme();
  const { isMobileView } = useCustomThemeV2();
  const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
  const {
    data: {
      merchantInfo: { merchantID },
      status: { statusName },
    },
  } = useMerchantSidePanelContext();
  const { openNotifyMerchantModal } = useNotifications({
    merchantID,
    statusName,
  });
  const { search, handleSearchChange } = useTableSearch({
    queryKey: "items_all_challenges",
  });
  const { isEnterprisePortal } = checkPortals();
  const { merchant_underwriting } = useEnterprisePermissions();
 
  const isDisabledConversation = isEnterprisePortal && !merchant_underwriting;
 
  const open = disabled - incomplete - completed;
 
  const menuOptions: Omit<ContextualMenuOptionProps, "children">[] = [
    {
      type: "label",
      text: "Sort by",
    },
    {
      text: `Open (${open})`,
      onClick: () => setChallengeStatusFilter("open"),
      type: "default",
      isSelected: challengeStatusFilter.includes("open"),
      disabled: open === 0,
    },
    {
      text: `Ready for Verification (${incomplete})`,
      onClick: () => setChallengeStatusFilter("ready_for_verification"),
      type: "default",
      isSelected: challengeStatusFilter.includes("ready_for_verification"),
      disabled: incomplete === 0,
      hidden: identifier === "merchant_onboarding",
    },
    {
      text: `Done (${completed})`,
      onClick: () => setChallengeStatusFilter("closed"),
      type: "default",
      isSelected: challengeStatusFilter.includes("closed"),
      disabled: completed === 0,
    },
    { text: "Filter by", type: "label" },
    {
      text: "Merchant Replied",
      onClick: () => handleUnreadFilter(`%3B(didMerchantReply:"true")`),
      type: "default",
      isSelected: hasUnreadMessagesFilter !== "",
      hidden: identifier !== "enhanced_due_diligence",
    },
    {
      text: "Tagged in Conversation",
      onClick: () => handleUserTaggedFilter("&userTaggedIn=true"),
      type: "default",
      isSelected: userTaggedInFilter !== "",
    },
  ];
  const handleCloseMenu = () => {
    setAnchorEl(null);
  };
 
  const isHideContent =
    identifier === "enhanced_due_diligence" && isEmptyChallengeList;
 
  let filtersAmount = challengeStatusFilter.length;
  filtersAmount += hasUnreadMessagesFilter ? 1 : 0;
  filtersAmount += userTaggedInFilter ? 1 : 0;
 
  return (
    <Stack gap="20px">
      <Stack
        direction="row"
        alignItems="center"
        justifyContent="space-between"
        pr="16px"
        margin="0 12px"
      >
        <GiveTooltip
          width="compact"
          alignment="left"
          title={ACTION_DENY_MESSAGE}
          color="default"
          disableHoverListener={!isDisabledConversation}
        >
          <GiveLink
            component="button"
            Icon={PaperPlaneTiltIcon}
            onClick={() => openNotifyMerchantModal({})}
            disabled={isHideContent || isDisabledConversation}
          >
            Notify Merchant
          </GiveLink>
        </GiveTooltip>
        <GiveChip
          variant="light"
          size="small"
          label={status}
          color={status === "Completed" ? "success" : "default"}
          sx={{ fontSize: "12px" }}
        />
      </Stack>
      <Stack
        gap="20px"
        margin="0px 12px"
        borderBottom={
          isHideContent ? "none" : `1px solid ${palette.border?.primary}`
        }
        pb="20px"
      >
        <IconContainer>{iconMapper(identifier, 24)}</IconContainer>
        <GiveText variant="bodyL">{name} Challenges</GiveText>
        {!isHideContent && (
          <Box width="101%">
            <GiveUnderwritingProgressBar
              completed={completed}
              incomplete={incomplete}
              disabled={open}
            />
          </Box>
        )}
      </Stack>
      {!isHideContent && (
        <Stack direction={isMobileView ? "column" : "row"} gap="20px" pl="12px">
          <GiveSearchBar handleChange={handleSearchChange} value={search} />
          <GiveButton
            label={`Filter & Sort${
              filtersAmount > 0 ? ` (${filtersAmount})` : ""
            }`}
            startIcon={<FunnelSimpleIcon size={18} />}
            endIcon={
              filtersAmount > 0 ? (
                <GiveIconButton
                  component="span"
                  role="button"
                  Icon={XIcon}
                  size="extraSmall"
                  onClick={(e) => {
                    e.stopPropagation();
                    handleResetFilters();
                  }}
                />
              ) : undefined
            }
            variant={isMobileView ? "filled" : "ghost"}
            color="light"
            size="large"
            sx={{
              width: isMobileView
                ? "fit-content"
                : filtersAmount > 0
                ? "300px"
                : "200px",
              border: "none",
            }}
            onClick={(e) => setAnchorEl(e.target as HTMLElement)}
          />
          <ContextualMenu
            anchorEl={anchorEl}
            color={isMobileView ? "secondary" : "tertiary"}
            texture={isMobileView ? "solid" : "blurred"}
            options={menuOptions}
            handleClose={handleCloseMenu}
          />
        </Stack>
      )}
    </Stack>
  );
}
 
const IconContainer = styled(Box)(({ theme }) => ({
  backgroundColor: theme.palette.primitive?.transparent["darken-5"],
  borderRadius: "50%",
  height: "48px",
  width: "48px",
  display: "flex",
  justifyContent: "center",
  alignItems: "center",
}));