All files / modules/45-projects-orgs/modals/ProjectModal/views InviteListRenderer.tsx

89.13% Statements 41/46
62.5% Branches 20/32
44.44% Functions 4/9
88.64% Lines 39/44

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              18x 18x 18x 18x 18x 18x 18x 18x 18x 18x   18x 18x 18x                     18x   18x 81x 81x 81x 81x 81x 81x 81x       81x 81x 81x   81x 1x         1x 1x 1x 1x             81x 1x 1x 1x 1x           81x                                                                                       1x                                                                                                                                                                                   18x  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Free Trial 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/05/PolyForm-Free-Trial-1.0.0.txt.
 */
 
import React, { useState } from 'react'
import cx from 'classnames'
import { Menu } from '@blueprintjs/core'
import { SelectOption, Layout, Text, Button, Avatar, Container, ButtonVariation } from '@wings-software/uicore'
import { useParams } from 'react-router-dom'
import { FontVariation, Color } from '@harness/design-system'
import { Select } from '@blueprintjs/select'
import { useToaster } from '@common/exports'
import { useDeleteInvite, useUpdateInvite, Invite } from 'services/cd-ng'
import { useStrings } from 'framework/strings'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import { InviteType } from '@rbac/modals/RoleAssignmentModal/views/RoleAssignmentForm'
import useRBACError from '@rbac/utils/useRBACError/useRBACError'
import css from './Steps.module.scss'
 
interface InviteListProps {
  user: Invite
  isCommunity?: boolean
  projectIdentifier?: string
  orgIdentifier?: string
  roles: SelectOption[]
  reload: () => void
}
 
const CustomSelect = Select.ofType<SelectOption>()
 
const InviteListRenderer: React.FC<InviteListProps> = props => {
  const { user, reload, roles, isCommunity } = props
  const { accountId } = useParams<AccountPathProps>()
  const { getRBACErrorMessage } = useRBACError()
  const { getString } = useStrings()
  const [approved, setApproved] = useState<boolean>(false)
  const { mutate: deleteInvite } = useDeleteInvite({})
  const defaultRole = {
    label: getString('customText', { text: 'Assign a role' }),
    value: ''
  }
  const [role, setRole] = useState<SelectOption>(defaultRole)
  const { mutate: updateInvite } = useUpdateInvite({ inviteId: '', queryParams: { accountIdentifier: accountId } })
  const { showSuccess, showError } = useToaster()
 
  const handleUpdate = async (type: InviteType): Promise<void> => {
    const dataToSubmit: Invite = {
      ...user,
      inviteType: type,
      approved: type === InviteType.USER_INITIATED ? true : false
    }
    try {
      const updated = await updateInvite(dataToSubmit, { pathParams: { inviteId: user.id } })
      /* istanbul ignore else */ if (updated) reload()
      showSuccess(getString('projectsOrgs.projectInviteSuccess'))
    } catch (err) {
      /* istanbul ignore next */
      showError(getRBACErrorMessage(err))
    }
  }
 
  const handleDelete = async (): Promise<void> => {
    try {
      const deleted = await deleteInvite(user.id, { headers: { 'content-type': 'application/json' } })
      /* istanbul ignore else */ if (deleted) reload()
      showSuccess(getString('projectsOrgs.projectDeleteSuccess'))
    } catch (err) {
      /* istanbul ignore next */
      showError(getRBACErrorMessage(err))
    }
  }
  return (
    <Container className={css.invites} padding={{ left: 'xsmall', top: 'medium', bottom: 'medium' }}>
      {user?.inviteType == InviteType.ADMIN_INITIATED ? (
        <Layout.Horizontal>
          <Layout.Horizontal spacing="medium" className={cx(css.align, css.pendingUser)} width="60%">
            <Avatar name={user.name || user.email} email={user.email} size="normal" />
            <Layout.Vertical padding={{ left: 'small' }}>
              <Layout.Horizontal spacing="small">
                <Text font={{ weight: 'bold' }} color={Color.BLACK} className={css.name} lineClamp={1}>
                  {user.name || user.email.split('@')[0]}
                </Text>
                <Text
                  font={{ size: 'xsmall', weight: 'bold' }}
                  className={cx(css.colorBar, css.pending)}
                  color={Color.PRIMARY_7}
                >
                  {getString('projectsOrgs.pendingInvitation')}
                </Text>
              </Layout.Horizontal>
              <Text className={css.email} lineClamp={1}>
                {user.email}
              </Text>
              {!isCommunity && (
                <Layout.Horizontal spacing="xsmall">
                  <Text font={{ size: 'xsmall', weight: 'bold' }} color={Color.BLACK}>
                    {getString('projectsOrgs.roleAssigned')}
                  </Text>
                  <Text font={{ variation: FontVariation.TINY }} className={css.role} lineClamp={1}>
                    {user?.roleBindings?.[0]?.roleName}
                  </Text>
                </Layout.Horizontal>
              )}
            </Layout.Vertical>
          </Layout.Horizontal>
          <Layout.Horizontal width="40%" padding={{ right: 'medium' }} className={cx(css.align, css.toEnd)}>
            <Button
              inline
              variation={ButtonVariation.ICON}
              icon="repeat"
              tooltip={getString('projectsOrgs.resendInvitation')}
              tooltipProps={{
                isDark: true
              }}
              onClick={() => {
                handleUpdate(InviteType.ADMIN_INITIATED)
              }}
            />
            <Button
              inline
              variation={ButtonVariation.ICON}
              tooltip={getString('projectsOrgs.deleteInvitation')}
              tooltipProps={{
                isDark: true
              }}
              icon="trash"
              iconProps={{ size: 20 }}
              onClick={handleDelete}
            />
          </Layout.Horizontal>
        </Layout.Horizontal>
      ) : (
        <Layout.Horizontal>
          <Layout.Horizontal spacing="medium" className={css.align} width="60%">
            <Avatar name={user.name || user.email} email={user.email} size="normal" />
            <Layout.Vertical padding={{ left: 'small' }}>
              <Layout.Horizontal spacing="small">
                <Text font={{ weight: 'bold' }} color={Color.BLACK} className={css.name} lineClamp={1}>
                  {user.name}
                </Text>
                <Text
                  font={{ size: 'xsmall', weight: 'bold' }}
                  className={cx(css.colorBar, css.request)}
                  color={Color.YELLOW_500}
                >
                  {getString('projectsOrgs.requestAccess')}
                </Text>
              </Layout.Horizontal>
              <Text className={css.email} lineClamp={1}>
                {user.email}
              </Text>
              <Text font={{ size: 'xsmall', weight: 'bold' }} color={Color.BLACK}>
                {getString('projectsOrgs.noProjectRole')}
              </Text>
            </Layout.Vertical>
          </Layout.Horizontal>
          <Layout.Horizontal width="40%" padding={{ right: 'medium' }} className={cx(css.align, css.toEnd)}>
            {!approved ? (
              <Button
                inline
                minimal
                icon="command-approval"
                onClick={() => {
                  setApproved(true)
                }}
              />
            ) : (
              <Layout.Horizontal>
                <CustomSelect
                  items={roles}
                  filterable={false}
                  itemRenderer={(item, { handleClick }) => (
                    <div key={item.label}>
                      <Menu.Item
                        text={item.label}
                        onClick={(e: React.MouseEvent<HTMLElement, MouseEvent>) => handleClick(e)}
                      />
                    </div>
                  )}
                  onItemSelect={item => {
                    setRole(item)
                  }}
                  popoverProps={{ minimal: true }}
                >
                  <Button inline minimal rightIcon="chevron-down" text={role.label} />
                </CustomSelect>
                <Button
                  inline
                  minimal
                  icon="command-approval"
                  disabled={role === defaultRole}
                  onClick={() => {
                    handleUpdate(InviteType.USER_INITIATED)
                  }}
                />
              </Layout.Horizontal>
            )}
            <Button inline minimal icon="remove" onClick={handleDelete} />
          </Layout.Horizontal>
        </Layout.Horizontal>
      )}
    </Container>
  )
}
 
export default InviteListRenderer