All files / src/features/Settings/Team TeamMembersTable.tsx

77.41% Statements 24/31
77.77% Branches 21/27
50% Functions 5/10
77.41% Lines 24/31

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                                                    3x                         90x 90x   89x     89x     89x       89x           88x         87x 87x 87x   87x 87x       87x 19x                       87x                     87x       87x                       13x         87x                       1x     87x   87x                                                                                         87x                                                                                                  
import Table from "componentsV2/Table/Table";
import { Box, Stack } from "@mui/material";
import {
  GIVE_ADD_TEAM_MEMBER_MODAL,
  MEMBER_MOBILE,
  TEAM_MEMBER_PANEL_MODAL,
} from "modals/modal_names";
import MemberListHeader from "features/Settings/Team/MemberListHeader";
import { useMemo, useCallback } from "react";
import useTableSearch from "componentsV2/Table/hooks/useTableSearch";
import { QKEY_LIST_TEAM_MEMBERS } from "@constants/queryKeys";
import { useTable } from "componentsV2/Table/hooks/useTable";
import { useComponentHeight } from "@hooks/common/useComponentHeight";
import NiceModal from "@ebay/nice-modal-react";
import ContextualMenu from "@shared/ContextualMenu/ContextualMenu";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import GiveMotionWrapper from "@shared/Motion/GiveMotionWrapper";
import useListTeamMembersV2 from "./hooks/useListTeamMembersV2";
import useGetTeamMembersTable from "./hooks/useGetTeamMembersTable";
import GiveText from "@shared/Text/GiveText";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { checkPortals } from "@utils/routing";
import { useAccessControl } from "@features/Permissions/AccessControl";
import RESOURCE_BASE, { OPERATIONS } from "@constants/permissions";
import { getPahSectionHeight } from "./helpers/pahSectionLayout";
 
const TeamMembersTable: React.FC = () => {
  const {
    members,
    owner,
    pahRows,
    isLoading,
    isOwnerLoading,
    page,
    setPageDispatcher,
    handlePageChange,
    totalRows,
    hasNextPage,
    isFetchingNextPage,
  } = useListTeamMembersV2();
  const { isMerchantPermissionsRebrandingEnabled } = useGetFeatureFlagValues();
  const { isAcquirerPortal, isMerchantPortal, isEnterprisePortal } =
    checkPortals();
 
  const isTeamMemberSidePanelEnabledForMerchant =
    isMerchantPortal && isMerchantPermissionsRebrandingEnabled;
 
  const isTeamMemberPanelRebranded =
    isAcquirerPortal ||
    isEnterprisePortal ||
    isTeamMemberSidePanelEnabledForMerchant;
 
  const isMembersListAllowed = useAccessControl({
    withPortal: true,
    resource: RESOURCE_BASE.MEMBER,
    operation: OPERATIONS.LIST,
  });
 
  const { search, searchQuery, handleSearchChange } = useTableSearch({
    queryKey: QKEY_LIST_TEAM_MEMBERS,
  });
 
  const { getColumns, anchorEl, selectedRow, getOptionsList, closeMenu } =
    useGetTeamMembersTable();
  const { isMobileView, isLargeTabletView } = useCustomThemeV2();
  const isTabletOrMobileView = isMobileView || isLargeTabletView;
 
  const optionsList = useMemo(
    () => getOptionsList(selectedRow),
    [getOptionsList, selectedRow],
  );
 
  const emptyStateAction = useMemo(
    () => ({
      handleAction: () => {
        NiceModal.show(GIVE_ADD_TEAM_MEMBER_MODAL, {
          roleName: "a Member",
          roleValue: "member",
        });
      },
      disabled: false,
    }),
    [],
  );
 
  const handleScroll = useCallback(
    (e: React.UIEvent<HTMLDivElement>) => {
      const { scrollTop, scrollHeight, clientHeight } = e.currentTarget;
      const scrollPercentage = (scrollTop + clientHeight) / scrollHeight;
      if (scrollPercentage > 0.8 && hasNextPage && !isFetchingNextPage) {
        handlePageChange();
      }
    },
    [hasNextPage, isFetchingNextPage, handlePageChange],
  );
 
  const emptyAction = () => {
    //
  };
 
  const mainTableState = useTable({
    allRows: members,
    isLoading,
    totalRows,
    page,
    setPageDispatcher,
    setPage: handlePageChange,
    openModal:
      isMobileView && !isTeamMemberPanelRebranded
        ? MEMBER_MOBILE
        : TEAM_MEMBER_PANEL_MODAL,
    canOpenModal: (row) =>
      isTeamMemberPanelRebranded ||
      row.memberStatus === "joined" ||
      row.memberStatus === "invited",
  });
 
  const ownerTableState = useTable({
    allRows: pahRows,
    isLoading: isOwnerLoading,
    totalRows: pahRows.length,
    page: 1,
    setPageDispatcher: emptyAction,
    setPage: emptyAction,
    openModal:
      isMobileView && !isTeamMemberPanelRebranded
        ? MEMBER_MOBILE
        : TEAM_MEMBER_PANEL_MODAL,
    canOpenModal: (row) =>
      isTeamMemberPanelRebranded || row.memberStatus === "joined",
  });
 
  const { height } = useComponentHeight();
  const OwnerTable = (
    <Stack>
      <MemberListHeader search={search} onSearchChange={handleSearchChange} />
 
      {Boolean(owner) && (
        <>
          <Stack
            direction="column"
            // The nested PAH table's container is height:100% / -webkit-fill-
            // available, so this parent MUST keep a definite height or the rows
            // collapse to nothing. getPahSectionHeight grows it per row so a
            // pending reassignment's second PAH row fits instead of clipping.
            height={getPahSectionHeight(pahRows.length, isTabletOrMobileView)}
          >
            <GiveText variant="bodyL" paddingTop="52px" paddingBottom="16px">
              Primary Account Holder
            </GiveText>
            <Table
              allRows={pahRows}
              columns={getColumns(false)}
              totalRows={pahRows.length}
              type="empty-settings-team-members"
              isLoading={isOwnerLoading}
              page={1}
              setPageDispatcher={() => {
                return;
              }}
              setPage={() => {
                return;
              }}
              {...ownerTableState}
              hideCaret
              openModal={TEAM_MEMBER_PANEL_MODAL}
              enabledInfiniteScroll
              disablePadding={true}
              minHeight={"-webkit-fill-available"}
            />
          </Stack>
          <GiveText variant="bodyL" paddingTop="32px" paddingBottom="16px">
            Members
          </GiveText>
        </>
      )}
    </Stack>
  );
 
  return (
    <Box paddingLeft={0} height="calc(100vh - 140px)">
      <GiveMotionWrapper
        delay={30}
        containerProps={{ sx: { height: "100%", zIndex: 2 } }}
        customStyle={{
          height: "100%",
          display: "flex",
          justifyContent: "center",
        }}
      >
        <Table
          allRows={members}
          columns={getColumns(true)}
          top={height}
          totalRows={members.length}
          type="empty-settings-team-members"
          isListPermissionAllowed={isMembersListAllowed}
          sectionName="Members"
          isLoading={isLoading}
          emptyStateAction={emptyStateAction}
          queryKey={QKEY_LIST_TEAM_MEMBERS}
          onScroll={handleScroll}
          searchQuery={searchQuery}
          handleSearchChange={handleSearchChange}
          page={page}
          setPageDispatcher={setPageDispatcher}
          setPage={handlePageChange}
          {...mainTableState}
          disablePadding={true}
          openModal={TEAM_MEMBER_PANEL_MODAL}
          enabledInfiniteScroll
          hideCaret
          Head={OwnerTable}
        />
      </GiveMotionWrapper>
 
      <ContextualMenu
        anchorEl={anchorEl}
        color="tertiary"
        texture="blurred"
        options={optionsList}
        handleClose={closeMenu}
      />
    </Box>
  );
};
 
export default TeamMembersTable;