2023-08-22 17:38:37 -04:00
|
|
|
import {
|
|
|
|
useState,
|
|
|
|
type ComponentProps,
|
|
|
|
useCallback,
|
|
|
|
type Dispatch,
|
|
|
|
type SetStateAction,
|
|
|
|
} from 'react'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
2023-06-30 04:30:20 -04:00
|
|
|
import { Dropdown, MenuItem } from 'react-bootstrap'
|
|
|
|
import { User } from '../../../../../../types/group-management/user'
|
2023-08-22 17:38:37 -04:00
|
|
|
import useAsync from '../../../../shared/hooks/use-async'
|
|
|
|
import {
|
|
|
|
type FetchError,
|
|
|
|
postJSON,
|
|
|
|
} from '../../../../infrastructure/fetch-json'
|
|
|
|
import Icon from '../../../../shared/components/icon'
|
|
|
|
import { ManagedUserAlert } from '../../utils/types'
|
2023-08-22 09:30:34 -04:00
|
|
|
import { useGroupMembersContext } from '../../context/group-members-context'
|
2023-06-30 04:30:20 -04:00
|
|
|
|
2023-08-22 17:38:37 -04:00
|
|
|
type ResendManagedUserInviteResponse = {
|
|
|
|
success: boolean
|
|
|
|
}
|
|
|
|
|
2023-06-30 04:30:20 -04:00
|
|
|
type ManagedUserDropdownButtonProps = {
|
|
|
|
user: User
|
2023-07-13 04:50:43 -04:00
|
|
|
openOffboardingModalForUser: (user: User) => void
|
2023-08-22 17:38:37 -04:00
|
|
|
groupId: string
|
|
|
|
setManagedUserAlert: Dispatch<SetStateAction<ManagedUserAlert>>
|
2023-06-30 04:30:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function ManagedUserDropdownButton({
|
|
|
|
user,
|
2023-07-13 04:50:43 -04:00
|
|
|
openOffboardingModalForUser,
|
2023-08-22 17:38:37 -04:00
|
|
|
groupId,
|
|
|
|
setManagedUserAlert,
|
2023-06-30 04:30:20 -04:00
|
|
|
}: ManagedUserDropdownButtonProps) {
|
|
|
|
const { t } = useTranslation()
|
2023-08-22 09:30:34 -04:00
|
|
|
const { removeMember } = useGroupMembersContext()
|
2023-08-22 17:38:37 -04:00
|
|
|
const [isOpened, setIsOpened] = useState(false)
|
|
|
|
const {
|
|
|
|
runAsync: runResendManagedUserInviteAsync,
|
|
|
|
isLoading: isResendingManagedUserInvite,
|
|
|
|
} = useAsync<ResendManagedUserInviteResponse>()
|
|
|
|
|
|
|
|
const userNotManaged =
|
|
|
|
!user.isEntityAdmin && !user.invite && !user.enrollment?.managedBy
|
|
|
|
|
|
|
|
const handleResendManagedUserInvite = useCallback(
|
|
|
|
async user => {
|
|
|
|
try {
|
|
|
|
const result = await runResendManagedUserInviteAsync(
|
|
|
|
postJSON(
|
|
|
|
`/manage/groups/${groupId}/resendManagedUserInvite/${user._id}`
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if (result.success) {
|
|
|
|
setManagedUserAlert({
|
|
|
|
variant: 'resendManagedUserInviteSuccess',
|
|
|
|
email: user.email,
|
|
|
|
})
|
|
|
|
setIsOpened(false)
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if ((err as FetchError)?.response?.status === 429) {
|
|
|
|
setManagedUserAlert({
|
|
|
|
variant: 'resendManagedUserInviteTooManyRequests',
|
|
|
|
email: user.email,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
setManagedUserAlert({
|
|
|
|
variant: 'resendManagedUserInviteFailed',
|
|
|
|
email: user.email,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsOpened(false)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[setManagedUserAlert, groupId, runResendManagedUserInviteAsync]
|
|
|
|
)
|
|
|
|
|
|
|
|
const onResendManagedUserInviteClick = () => {
|
|
|
|
handleResendManagedUserInvite(user)
|
|
|
|
}
|
2023-07-13 04:50:43 -04:00
|
|
|
|
|
|
|
const onDeleteUserClick = () => {
|
|
|
|
openOffboardingModalForUser(user)
|
|
|
|
}
|
|
|
|
|
2023-08-22 09:30:34 -04:00
|
|
|
const onRemoveFromGroup = () => {
|
|
|
|
removeMember(user)
|
|
|
|
}
|
|
|
|
|
2023-06-30 04:30:20 -04:00
|
|
|
return (
|
2023-08-03 06:06:13 -04:00
|
|
|
<span className="managed-user-actions">
|
2023-08-22 17:38:37 -04:00
|
|
|
<Dropdown
|
|
|
|
id={`managed-user-dropdown-${user.email}`}
|
|
|
|
open={isOpened}
|
|
|
|
onToggle={open => setIsOpened(open)}
|
|
|
|
>
|
2023-07-13 04:50:43 -04:00
|
|
|
<Dropdown.Toggle
|
|
|
|
bsStyle={null}
|
|
|
|
className="btn btn-link action-btn"
|
|
|
|
noCaret
|
|
|
|
>
|
|
|
|
<i
|
|
|
|
className="fa fa-ellipsis-v"
|
|
|
|
aria-hidden="true"
|
|
|
|
aria-label={t('actions')}
|
|
|
|
/>
|
|
|
|
</Dropdown.Toggle>
|
2023-08-22 17:38:37 -04:00
|
|
|
<Dropdown.Menu className="dropdown-menu-right managed-user-dropdown-menu">
|
|
|
|
{userNotManaged ? (
|
|
|
|
<MenuItemButton onClick={onResendManagedUserInviteClick}>
|
|
|
|
{t('resend_managed_user_invite')}
|
|
|
|
{isResendingManagedUserInvite ? (
|
|
|
|
<Icon type="spinner" spin style={{ marginLeft: '5px' }} />
|
|
|
|
) : null}
|
|
|
|
</MenuItemButton>
|
|
|
|
) : null}
|
2023-07-13 04:50:43 -04:00
|
|
|
{user.enrollment ? (
|
|
|
|
<MenuItemButton
|
|
|
|
className="delete-user-action"
|
2023-08-22 09:30:34 -04:00
|
|
|
data-testid="delete-user-action"
|
2023-07-13 04:50:43 -04:00
|
|
|
onClick={onDeleteUserClick}
|
|
|
|
>
|
|
|
|
{t('delete_user')}
|
|
|
|
</MenuItemButton>
|
2023-08-22 09:30:34 -04:00
|
|
|
) : user.isEntityAdmin ? (
|
|
|
|
<MenuItem data-testid="no-actions-available">
|
2023-06-30 04:30:20 -04:00
|
|
|
<span className="text-muted">{t('no_actions')}</span>
|
|
|
|
</MenuItem>
|
2023-08-22 09:30:34 -04:00
|
|
|
) : (
|
|
|
|
<MenuItemButton
|
|
|
|
onClick={onRemoveFromGroup}
|
|
|
|
className="delete-user-action"
|
|
|
|
data-testid="remove-user-action"
|
|
|
|
>
|
|
|
|
{t('remove_from_group')}
|
|
|
|
</MenuItemButton>
|
2023-07-13 04:50:43 -04:00
|
|
|
)}
|
|
|
|
</Dropdown.Menu>
|
2023-08-22 17:38:37 -04:00
|
|
|
</Dropdown>
|
2023-08-03 06:06:13 -04:00
|
|
|
</span>
|
2023-06-30 04:30:20 -04:00
|
|
|
)
|
|
|
|
}
|
2023-08-22 17:38:37 -04:00
|
|
|
|
|
|
|
function MenuItemButton({
|
|
|
|
children,
|
|
|
|
onClick,
|
|
|
|
className,
|
|
|
|
...buttonProps
|
|
|
|
}: ComponentProps<'button'>) {
|
|
|
|
return (
|
|
|
|
<li role="presentation" className={className}>
|
|
|
|
<button
|
|
|
|
className="managed-user-menu-item-button"
|
|
|
|
role="menuitem"
|
|
|
|
onClick={onClick}
|
|
|
|
{...buttonProps}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
}
|