All files / src/shared/GiveExportModal/components ExportFilterButtons.tsx

72.41% Statements 21/29
41.66% Branches 10/24
62.5% Functions 5/8
74.07% Lines 20/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                                                                              5x         1x 1x 1x 1x   1x                 1x 1x     1x       1x           1x       1x   1x 1x                   1x         1x                                                               5x                           5x 5x                           5x                
import { ExportIcon } from "@assets/rebrandIcons";
import { Button } from "@common/Button";
import NiceModal from "@ebay/nice-modal-react";
import { Box, Stack, styled } from "@mui/material";
import { palette } from "@palette";
import { GIVE_EXPORT_MODAL } from "modals/modal_names";
import FilterButton from "@common/TableFilters/FilterButton";
import { Switch_V2 as Switch } from "@common/Switch";
import { useAppSelector } from "@redux/hooks";
import {
  setWatchlistFilters,
  watchListfilter as watchListFilterReducer,
} from "@redux/slices/fundraisers";
import { useDispatch } from "react-redux";
import { useCallback, useMemo } from "react";
import {
  selectMerchantStatusName,
  setMerchantStatusName,
} from "@redux/slices/enterprise/merchants";
import { Text } from "@common/Text";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import { StatusNames } from "@components/Merchants/MerchantList/MerchantStatusFilters";
import { ExportRecordsTotal } from "@common/Table/helpers";
import { useAccessControl } from "features/Permissions/AccessControl";
import RESOURCE_BASE, {
  ACTION_DENY_MESSAGE,
  OPERATIONS,
} from "@constants/permissions";
import { exportType } from "@shared/GiveExportModal/const";
 
type Props = {
  totalRows?: ExportRecordsTotal | number;
  hideTableView?: boolean;
  totalFilteredRows?: number;
};
 
//This component is not effectively is not used on the platform 
//but is used in some components, so right now its better to not remove
//will be cleaned up with the related old componets  
const ExportFilterButtons = ({
  totalRows = 0,
  hideTableView = false,
  totalFilteredRows,
}: Props) => {
  const filteredWatchlist = useAppSelector(watchListFilterReducer);
  const dispatch = useDispatch();
  const { isDesktopView } = useCustomTheme();
  const statusName = useAppSelector(selectMerchantStatusName);
 
  const handleWatchlistFilter = useCallback(() => {
    if (filteredWatchlist) {
      dispatch(setWatchlistFilters(""));
    } else {
      dispatch(setMerchantStatusName(""));
      dispatch(setWatchlistFilters("watchlist:true"));
    }
  }, [filteredWatchlist]);
 
  const currentTab = useMemo(() => {
    const underwritingStatus = !hideTableView
      ? StatusNames.aquirerUnderwriting
      : StatusNames.enterpriseUnderwriting;
    const riskOrApprovedStatus = !hideTableView
      ? StatusNames.aquirerRisk
      : StatusNames.enterpriseApproved;
 
    switch (statusName) {
      case underwritingStatus:
        return "underwriting";
      case riskOrApprovedStatus:
        return "risk";
      default:
        return "portfolio";
    }
  }, [statusName]);
  
const hasFilters = totalFilteredRows ? totalFilteredRows > 0 : false
 
  const handleOpenPanel = () => {
    NiceModal.show(GIVE_EXPORT_MODAL, {
      fieldTypes: exportType.MERCHANT_TABLE,
      counts: {
        total:  typeof totalRows === "number" ? totalRows : totalRows[currentTab],
        filtered: hasFilters ? totalFilteredRows : 0,
      },
      tab: currentTab,
    });
  };
 
  const isExportAllowed = useAccessControl({
    resource: RESOURCE_BASE.MERCHANT,
    operation: OPERATIONS.LIST,
  });
 
  return (
    <Stack direction="row" spacing={1} justifyContent="space-between">
      {!isDesktopView && hideTableView && (
        <WatchlistSwitchContainer
          isFilteredWatchlist={filteredWatchlist !== ""}
        >
          <Switch
            data-testid="mobile-watchlist-only-switch"
            checked={filteredWatchlist !== ""}
            size="small"
            onChange={handleWatchlistFilter}
          />
          <WatchText>Watchlist only</WatchText>
        </WatchlistSwitchContainer>
      )}
      {!hideTableView && <FilterButton />}
      <StyledButton
        background="secondary"
        startIcon={<ExportIcon />}
        onClick={handleOpenPanel}
        disabled={!isExportAllowed}
        tooltipProps={{
          message: ACTION_DENY_MESSAGE,
          show: !isExportAllowed,
        }}
      >
        Export
      </StyledButton>
    </Stack>
  );
};
 
const StyledButton = styled(Button)(() => ({
  padding: "0 12px",
  border: `1px solid ${palette.neutral[10]}`,
  background: palette.background.dimmedWhite,
  color: palette.neutral[70],
  fontWeight: 350,
  height: "32px",
  lineHeight: "16.8px",
  gap: "4px",
  "&:hover": {
    background: palette.background.dimmedWhite,
  },
}));
 
const WatchlistSwitchContainer = styled(Box, {
  shouldForwardProp: (prop) => prop !== "isFilteredWatchlist",
})<{ isFilteredWatchlist: boolean }>(({ isFilteredWatchlist }) => ({
  ...(isFilteredWatchlist && {
    "& .MuiSwitch-track": {
      backgroundColor: `${palette.filled.success} !important`,
    },
  }),
  alignItems: "center",
  height: "32px",
  display: "flex",
  flexDirection: "row",
  gap: "8px",
}));
 
const WatchText = styled(Text)(() => ({
  fontSize: "14px",
  fontWeight: 350,
  lineHeight: "16.8px",
  color: palette.neutral[80],
}));
 
export default ExportFilterButtons;