All files / src/features/Merchants/MerchantSidePanel/components ProviderMccSection.tsx

90% Statements 27/30
51.51% Branches 17/33
66.66% Functions 4/6
90% Lines 27/30

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                                                              23x 23x 23x   23x 7x 7x     7x                   7x 7x   7x       7x 7x 7x 7x 7x 7x 7x   7x         7x                       7x     7x       7x                                                                                                   7x     7x                                                                                   7x 7x                                                                                       23x                                    
import { Stack } from "@mui/material";
import {
  CheckCircleIcon,
  LockSimpleIcon,
  ProhibitIcon,
  WarningCircleIcon,
} from "@phosphor-icons/react";
import GiveChip, { TChipColors } from "@shared/Chip/GiveChip";
import SectionCardBase from "@shared/SidePanel/components/SectionCard/SectionCardBase";
import GiveText from "@shared/Text/GiveText";
import GiveTruncateText from "@shared/Text/GiveTruncateText";
import { useMerchantSidePanelContext } from "../Provider/MerchantSidePanelProvider";
import { Virtuoso, VirtuosoHandle } from "react-virtuoso";
import { useRef } from "react";
import { EDIT_ENTERPRISE_MCC_MODAL } from "modals/modal_names";
import NiceModal from "@ebay/nice-modal-react";
import { capitalize } from "lodash";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import GiveEmptyStateWrapper from "@shared/EmptyState/GiveEmptyStateWrapper";
import { useAccessControl } from "@features/Permissions/AccessControl";
import RESOURCE_BASE, {
  NEW_ACTION_DENY_MESSAGE,
  OPERATIONS,
} from "@constants/permissions";
import { checkoutScrollbarStyles } from "@sections/PayBuilder/Checkout/helpers";
import { SHOW_RISK_CATEGORY } from "../constants";
import { useFormContext } from "react-hook-form";
import GiveButton from "@shared/Button/GiveButton";
import { TCategoryCode } from "@components/Merchants/MerchantPreview/data.types";
import QueryBoundary from "./QueryBoundary";
 
const ITEM_HEIGHT = 44;
const MAX_HEIGHT = 240;
const MAX_ITEMS_WITHOUT_SCROLL = MAX_HEIGHT / ITEM_HEIGHT;
 
const getHeight = (itemsLength: number): number => {
  const height = itemsLength * ITEM_HEIGHT;
  Iif (height > MAX_HEIGHT) {
    return MAX_HEIGHT;
  }
  return height;
};
 
type ProviderMccSectionProps = {
  parentCategories?: TCategoryCode[];
};
 
export default function ProviderMccSection({
  parentCategories: propParentCategories,
}: ProviderMccSectionProps = {}) {
  const { data, id, queries } = useMerchantSidePanelContext();
  const { isMobileView } = useCustomThemeV2();
 
  const formContext = useFormContext();
 
  // Use form data if available (create flow), otherwise use API data (side panel)
  const categories =
    formContext?.watch?.("categories") || data?.categoryCodes || [];
  const parentCategories = propParentCategories || data?.parentCategories || [];
  const dataLength = categories.length;
  const displayCount = Math.min(5, dataLength);
  const listHeight = getHeight(displayCount) - 20; //the last item should not have the extra padding
  const shouldShowScrollbar = dataLength > MAX_ITEMS_WITHOUT_SCROLL;
  const scroller = useRef<VirtuosoHandle>(null);
 
  const isUpdateMCCAllowed = useAccessControl({
    resource: RESOURCE_BASE.ENTERPRISE,
    operation: OPERATIONS.UPDATE,
  });
 
  const editHandler = () => {
    NiceModal.show(EDIT_ENTERPRISE_MCC_MODAL, {
      id,
      categories: categories,
      parentCategories: parentCategories,
      // In create flow, use onClose to update form instead of making API call
      ...(formContext?.setValue && {
        onClose: (data: any) => formContext.setValue("categories", data),
      }),
    });
  };
 
  const actions = {
    handleEdit: editHandler,
  };
  const disabledActions = {
    handleEdit: { hidden: !isUpdateMCCAllowed },
  };
 
  return (
    <SectionCardBase
      leftTitle="Merchant Category Codes (MCC)"
      actions={actions}
      disabledActions={disabledActions}
      childrenContainerSx={
        shouldShowScrollbar && !isMobileView
          ? {
              paddingRight: "4px",
              ".custom-virtuoso": {
                ...checkoutScrollbarStyles,
                "&::-webkit-scrollbar-thumb": {
                  ...checkoutScrollbarStyles["&::-webkit-scrollbar-thumb"],
                  border: "none",
                },
                "& > div": {
                  paddingRight: "10px",
                },
              },
            }
          : undefined
      }
    >
      <QueryBoundary
        query={queries?.merchantQuery}
        errorTitle="Merchant Category Codes could not be loaded"
      >
        {dataLength < 1 ? (
          <GiveEmptyStateWrapper
            section="empty-mcc-provider"
            action={{
              handleAction: editHandler,
              disabled: !isUpdateMCCAllowed,
            }}
            actionTooltipProps={{ title: NEW_ACTION_DENY_MESSAGE }}
            isEmpty
          />
        ) : (
          <>
            <Virtuoso
              ref={scroller}
              style={{
                height: listHeight,
                width: "100%",
                overflowY:
                  shouldShowScrollbar && !isMobileView ? "auto" : "hidden",
              }}
              className="custom-virtuoso"
              data={isMobileView ? categories.slice(0, 5) : categories}
              itemContent={(index, category) => {
                const isLastItem = isMobileView
                  ? index === displayCount - 1
                  : index === dataLength - 1;
                return (
                  <CategoryListItem
                    code={category?.code}
                    name={category?.title}
                    riskStatus={category?.riskStatus}
                    isLastItem={isLastItem}
                    key={index}
                  />
                );
              }}
            />
            {isMobileView && dataLength > 5 && (
              <GiveButton
                variant="filled"
                color="light"
                size="large"
                label="See All"
                onClick={editHandler}
                disabled={!isUpdateMCCAllowed}
                sx={{ width: "100%", mt: "24px" }}
              />
            )}
          </>
        )}
      </QueryBoundary>
    </SectionCardBase>
  );
}
 
type CategoryProps = {
  code?: string;
  name?: string;
  riskStatus?: "normal" | "high" | "restricted";
  isLastItem?: boolean;
};
 
function CategoryListItem({
  code,
  name,
  riskStatus,
  isLastItem,
}: CategoryProps) {
  const { isMobileView } = useCustomThemeV2();
  return (
    <Stack
      direction="row"
      alignItems="center"
      gap="16px"
      component="div"
      paddingBottom={isLastItem ? 0 : "20px"}
    >
      <Stack direction="row" alignItems="center" gap="12px" flex={1}>
        <GiveChip
          label={code}
          variant="light"
          color="default"
          sx={{
            minWidth: "48px",
            flexShrink: 0,
          }}
        />
        <GiveTruncateText lineClamp={1} variant="bodyS" flex={1}>
          {name}
        </GiveTruncateText>
      </Stack>
      {!!riskStatus && SHOW_RISK_CATEGORY && (
        <GiveChip
          label={capitalize(riskStatus)}
          variant="light"
          color={riskChipProps[riskStatus].color as TChipColors}
          icon={riskChipProps[riskStatus].icon}
          sx={{
            ".MuiChip-icon": {
              marginRight: "0px",
              marginLeft: "0px",
            },
            "& span": {
              lineHeight: "14px !important",
            },
            gap: "4px !important",
          }}
        />
      )}
    </Stack>
  );
}
 
const riskChipProps = {
  normal: {
    color: "success",
    icon: <CheckCircleIcon />,
  },
  high: {
    color: "warning",
    icon: <WarningCircleIcon />,
  },
  restricted: {
    color: "error",
    icon: <ProhibitIcon />,
  },
  prohibited: {
    color: "errorDark",
    icon: <LockSimpleIcon />,
  },
};