All files / src/features/Permissions/ModalV2/components/AddPermissionModal PermissionListContent.tsx

97.29% Statements 36/37
95.65% Branches 22/23
100% Functions 11/11
97.05% Lines 33/34

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                                                                          4x                                             65x   65x                 65x 1x 1x     65x   65x 165x     65x                                 2x                                         165x 164x   41x   41x   41x   41x           165x       165x             329x   329x 123x   329x 123x   329x 41x     329x 329x 329x 329x         9x                                             165x                 165x                        
import React from "react";
import {
  ActionButton,
  ActionsContainer,
  ListSkeleton,
  OpenCodeEditorButton,
} from "./AddPermissionModal.styles";
import { Box, Stack, styled } from "@mui/material";
import {
  PermissionItem,
  PermissionSearchBar,
} from "./AddPermissionModal.atoms";
import { CodeIconWithBar } from "@assets/icons";
import FilterPermissionList from "../FilterPermissionList";
import FadeUpWrapper from "@components/animation/FadeUpWrapper";
import NoResultsState from "@common/EmptyState/NoResultsState";
import useAddPermissionsModal from "../../hooks/useAddPermissionsModal";
import { TPermissionsData } from "../../types";
import { shouldBeHidden } from "@constants/constants";
import {
  composePermission,
  useAccessControl,
} from "features/Permissions/AccessControl";
import RESOURCE_BASE, {
  CREATE_DENY_MESSAGE,
  OPERATIONS,
} from "@constants/permissions";
import { GroupedVirtuoso } from "react-virtuoso";
import { palette } from "@palette";
import { Text } from "@common/Text";
 
export interface IPermissionListProps {
  onClose: () => void;
  onAdd: (logs: TPermissionsData) => void;
  memberId: number;
}
 
const PermissionListContent = ({
  onClose,
  onAdd,
  setIsCodeEditor,
  memberId,
}: IPermissionListProps & {
  setIsCodeEditor: React.Dispatch<React.SetStateAction<boolean>>;
}) => {
  const {
    listMutated,
    clearChanges,
    logs,
    searchString,
    handleSearch,
    selectedGroup,
    filters,
    orderedKeys,
    handleSelect,
    isSearching,
    dataIsLoading,
    permissionsData,
    setPermissionStatus,
    filteredGroups,
  } = useAddPermissionsModal(memberId);
 
  const isAddAllowed = useAccessControl({
    resource: composePermission(
      RESOURCE_BASE.MEMBER,
      RESOURCE_BASE.ACCESS_POLICIES,
    ),
    operation: OPERATIONS.UPDATE,
    withPortal: true,
  });
 
  const onCancel = () => {
    clearChanges();
    onClose();
  };
 
  const onSubmit = () => onAdd(logs);
 
  const lengths = Object.keys(filteredGroups).map(
    (k) => filteredGroups[k].length,
  );
 
  return (
    <>
      <PermissionSearchBar value={searchString} onChange={handleSearch} />
      <Stack
        padding="16px 12px"
        direction="row"
        justifyContent="space-between"
        alignItems="center"
      >
        <FilterPermissionList
          defaultValue={selectedGroup}
          filterData={filters}
          onHandleSelect={handleSelect}
        />
        {!shouldBeHidden.customPermission && (
          <OpenCodeEditorButton
            background="tertiary"
            onClick={() => setIsCodeEditor(true)}
            disabled={!isAddAllowed}
            tooltipProps={{
              show: !isAddAllowed,
              message: CREATE_DENY_MESSAGE,
            }}
          >
            <CodeIconWithBar fill="#6D9CF8" /> Add Custom Permission
          </OpenCodeEditorButton>
        )}
      </Stack>
      <Stack direction="column" alignItems="stretch" flexGrow={1}>
        {dataIsLoading || isSearching ? (
          <ListSkeleton />
        ) : !orderedKeys.length ? (
          <NoResultsState searchQuery={searchString} />
        ) : (
          <GroupedVirtuoso
            groupCounts={lengths}
            style={{ height: 400 }}
            groupContent={(index) => {
              const text = () => {
                switch (index) {
                  case 0:
                    return "Merchant";
                  case 1:
                    return "Provider";
                  case 2:
                    return "Acquirer";
                  case 3:
                    return "User";
                  default:
                    return "";
                }
              };
              const textValue =
                selectedGroup.value != "All"
                  ? selectedGroup?.title
                  : `${text()} permissions`;
 
              return (
                <StyledBox>
                  <Tag>{textValue}</Tag>
                </StyledBox>
              );
            }}
            itemContent={(i, group) => {
              let index = i;
 
              if (group === 1) {
                index = i - lengths[0];
              }
              if (group === 2) {
                index = i - lengths[0] - lengths[1];
              }
              if (group === 3) {
                index = i - lengths[0] - lengths[1] - lengths[2];
              }
              const key =
                filteredGroups[Object.keys(filteredGroups)[group]][index];
              const data = permissionsData.data[key];
              const { permissionName, description, isAttached } = data;
              return (
                <PermissionItem
                  permissionName={permissionName}
                  description={description}
                  assigned={isAttached}
                  onClick={() => setPermissionStatus(key, data)}
                />
              );
            }}
          />
        )}
      </Stack>
      {listMutated && (
        <FadeUpWrapper delay={200}>
          <ActionsContainer>
            <ActionButton background="tertiary" size="small" onClick={onCancel}>
              Cancel
            </ActionButton>
            <ActionButton background="primary" size="small" onClick={onSubmit}>
              Save
            </ActionButton>
          </ActionsContainer>
        </FadeUpWrapper>
      )}
    </>
  );
};
 
const StyledBox = styled(Box)(() => ({
  backgroundColor: "#FBFBFB",
  display: "flex",
  alignItems: "center",
  height: 32,
  borderBottom: `1px solid ${palette.liftedWhite[100]}`,
}));
export default PermissionListContent;
 
const Tag = styled(Text)(() => ({
  padding: "2px 8px",
  borderRadius: "4px",
  background: palette.liftedWhite[100],
  color: palette.black[100],
  fontSize: "12px",
  lineHeight: "14.4px",
  marginLeft: "8px",
  marginRight: "auto",
  width: "fit-content",
  height: "fit-content",
}));