All files / modules/20-rbac/pages/UserGroupDetails/views MemberList.tsx

94.92% Statements 56/59
72.37% Branches 55/76
86.67% Functions 13/15
94.83% Lines 55/58

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              10x 10x   10x                     10x 10x   10x 10x 10x 10x 10x 10x 10x 10x   10x 26x 26x               10x 26x   26x     10x 31x 31x 31x 31x 31x 31x                       31x               1x 1x 1x     1x 1x         1x                                 31x 1x 1x 1x     31x         2x                   1x 1x                                                                                                     10x 9x 9x 9x       9x                       9x   9x 9x       18x             18x             18x                   9x 9x                                     10x  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React, { useMemo, useState } from 'react'
import { useParams } from 'react-router-dom'
import type { CellProps, Column, Renderer } from 'react-table'
import {
  Layout,
  Button,
  Text,
  Avatar,
  Popover,
  Container,
  NoDataCard,
  TableV2,
  useConfirmationDialog
} from '@wings-software/uicore'
import { FontVariation, Intent } from '@harness/design-system'
import { Classes, Menu, Position, PopoverInteractionKind, MenuItem } from '@blueprintjs/core'
import type { ProjectPathProps, UserGroupPathProps } from '@common/interfaces/RouteInterfaces'
import { useGetUsersInUserGroup, useRemoveMember, UserInfo } from 'services/cd-ng'
import { useToaster } from '@common/components'
import { useStrings } from 'framework/strings'
import { useMutateAsGet } from '@common/hooks'
import RbacMenuItem from '@rbac/components/MenuItem/MenuItem'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import css from '../UserGroupDetails.module.scss'
 
const RenderColumnUser: Renderer<CellProps<UserInfo>> = ({ row }) => {
  const data = row.original
  return (
    <Layout.Horizontal spacing="small" flex={{ alignItems: 'center', justifyContent: 'flex-start' }}>
      <Avatar name={data.name || data.email} email={data.email} hoverCard={false} />
      <Text>{data.name}</Text>
    </Layout.Horizontal>
  )
}
 
const RenderColumnEmail: Renderer<CellProps<UserInfo>> = ({ row }) => {
  const data = row.original
 
  return <Text>{data.email}</Text>
}
 
const RenderColumnMenu: Renderer<CellProps<UserInfo>> = ({ row, column }) => {
  const data = row.original
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
  const [menuOpen, setMenuOpen] = useState(false)
  const { showSuccess, showError } = useToaster()
  const { getString } = useStrings()
  const { mutate: deleteUser } = useRemoveMember({
    identifier: data.uuid || '',
    pathParams: {
      identifier: (column as any).userGroupIdentifier
    },
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier
    }
  })
 
  const { openDialog } = useConfirmationDialog({
    contentText: getString('rbac.userGroupPage.userList.deleteConfirmation', { name: data.name }),
    titleText: getString('rbac.userGroupPage.userList.deleteTitle'),
    confirmButtonText: getString('common.remove'),
    cancelButtonText: getString('cancel'),
    intent: Intent.DANGER,
    buttonIntent: Intent.DANGER,
    onCloseDialog: async didConfirm => {
      /* istanbul ignore else */ if (didConfirm && data) {
        try {
          const deleted = await deleteUser(data.uuid || '', {
            headers: { 'content-type': 'application/json' }
          })
          /* istanbul ignore else */ if (deleted) {
            showSuccess(
              getString('rbac.userGroupPage.userList.deleteSuccessMessage', {
                name: data.name
              })
            )
            ;(column as any).refetchMembers?.()
          } else {
            /* istanbul ignore next */
            showSuccess(
              getString('rbac.userGroupPage.userList.deleteFailureMessage', {
                name: data.name
              })
            )
          }
        } catch (err) {
          /* istanbul ignore next */
          showError(err?.data?.message || err?.message)
        }
      }
    }
  })
 
  const handleDelete = (e: React.MouseEvent<HTMLElement, MouseEvent>): void => {
    e.stopPropagation()
    setMenuOpen(false)
    openDialog()
  }
 
  return (column as any).ssoLinked ? null : (
    <Layout.Horizontal flex={{ justifyContent: 'flex-end' }}>
      <Popover
        isOpen={menuOpen}
        onInteraction={nextOpenState => {
          setMenuOpen(nextOpenState)
        }}
        className={Classes.DARK}
        position={Position.BOTTOM_RIGHT}
      >
        <Button
          minimal
          icon="Options"
          data-testid={`menu-${data.uuid}`}
          onClick={e => {
            e.stopPropagation()
            setMenuOpen(true)
          }}
        />
        <Menu>
          {data.externallyManaged ? (
            <Popover
              position={Position.TOP}
              fill
              usePortal
              inheritDarkTheme={false}
              interactionKind={PopoverInteractionKind.HOVER}
              hoverCloseDelay={50}
              content={
                <div className={css.popover}>
                  <Text font={{ variation: FontVariation.SMALL }}>{getString('rbac.unableToEditSCIMMembership')}</Text>
                </div>
              }
            >
              <div
                onClick={(event: React.MouseEvent<HTMLElement, MouseEvent>) => {
                  event.stopPropagation()
                }}
              >
                <MenuItem icon="trash" text={getString('common.remove')} onClick={handleDelete} disabled />
              </div>
            </Popover>
          ) : (
            <RbacMenuItem
              icon="trash"
              text={getString('common.remove')}
              onClick={handleDelete}
              permission={{
                resourceScope: {
                  accountIdentifier: accountId,
                  orgIdentifier,
                  projectIdentifier
                },
                resource: {
                  resourceType: ResourceType.USERGROUP,
                  resourceIdentifier: (column as any).userGroupIdentifier
                },
                permission: PermissionIdentifier.MANAGE_USERGROUP
              }}
            />
          )}
        </Menu>
      </Popover>
    </Layout.Horizontal>
  )
}
 
const MemberList: React.FC<{ ssoLinked?: boolean }> = ({ ssoLinked }) => {
  const { getString } = useStrings()
  const [page, setPage] = useState<number>(0)
  const { accountId, orgIdentifier, projectIdentifier, userGroupIdentifier } = useParams<
    ProjectPathProps & UserGroupPathProps
  >()
 
  const { data, refetch } = useMutateAsGet(useGetUsersInUserGroup, {
    body: {},
    identifier: userGroupIdentifier,
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier,
      pageIndex: page,
      pageSize: 10
    }
  })
 
  const users = useMemo(() => data?.data?.content, [data?.data])
 
  const columns: Column<UserInfo>[] = useMemo(() => {
    return [
      {
        Header: getString('users'),
        id: 'user',
        accessor: (row: UserInfo) => row.name,
        width: '45%',
        Cell: RenderColumnUser
      },
      {
        Header: getString('email'),
        id: 'email',
        accessor: (row: UserInfo) => row.email,
        width: '50%',
        Cell: RenderColumnEmail
      },
      {
        Header: '',
        id: 'menu',
        accessor: (row: UserInfo) => row.uuid,
        width: '5%',
        Cell: RenderColumnMenu,
        refetchMembers: refetch,
        userGroupIdentifier: userGroupIdentifier,
        disableSortBy: true,
        ssoLinked: ssoLinked
      }
    ]
  }, [refetch])
  Eif (users?.length)
    return (
      <Container className={css.memberList}>
        <TableV2<UserInfo>
          data={users}
          columns={columns}
          hideHeaders={true}
          pagination={{
            itemCount: data?.data?.totalItems || 0,
            pageSize: data?.data?.pageSize || 10,
            pageCount: data?.data?.totalPages || 0,
            pageIndex: data?.data?.pageIndex || 0,
            gotoPage: (pageNumber: number) => setPage(pageNumber)
          }}
        />
      </Container>
    )
  return <NoDataCard icon="nav-project" message={getString('rbac.userDetails.noMembersMessage')} />
}
 
export default MemberList