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-23 14:53:01 -04:00
|
|
|
type resendInviteResponse = {
|
2023-08-22 17:38:37 -04:00
|
|
|
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,
|
2023-08-23 14:53:01 -04:00
|
|
|
} = useAsync<resendInviteResponse>()
|
|
|
|
|
|
|
|
const {
|
|
|
|
runAsync: runResendGroupInviteAsync,
|
|
|
|
isLoading: isResendingGroupInvite,
|
|
|
|
} = useAsync<resendInviteResponse>()
|
2023-08-22 17:38:37 -04:00
|
|
|
|
|
|
|
const userNotManaged =
|
|
|
|
!user.isEntityAdmin && !user.invite && !user.enrollment?.managedBy
|
|
|
|
|
2023-08-23 14:53:01 -04:00
|
|
|
const userPending = user.invite
|
|
|
|
|
2023-08-22 17:38:37 -04:00
|
|
|
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({
|
2023-08-23 14:53:01 -04:00
|
|
|
variant: 'resendInviteTooManyRequests',
|
2023-08-22 17:38:37 -04:00
|
|
|
email: user.email,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
setManagedUserAlert({
|
|
|
|
variant: 'resendManagedUserInviteFailed',
|
|
|
|
email: user.email,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsOpened(false)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[setManagedUserAlert, groupId, runResendManagedUserInviteAsync]
|
|
|
|
)
|
|
|
|
|
2023-08-23 14:53:01 -04:00
|
|
|
const handleResendGroupInvite = useCallback(
|
|
|
|
async user => {
|
|
|
|
try {
|
|
|
|
await runResendGroupInviteAsync(
|
|
|
|
postJSON(`/manage/groups/${groupId}/resendInvite/`, {
|
|
|
|
body: {
|
|
|
|
email: user.email,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
setManagedUserAlert({
|
|
|
|
variant: 'resendGroupInviteSuccess',
|
|
|
|
email: user.email,
|
|
|
|
})
|
|
|
|
setIsOpened(false)
|
|
|
|
} catch (err) {
|
|
|
|
if ((err as FetchError)?.response?.status === 429) {
|
|
|
|
setManagedUserAlert({
|
|
|
|
variant: 'resendInviteTooManyRequests',
|
|
|
|
email: user.email,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
setManagedUserAlert({
|
|
|
|
variant: 'resendGroupInviteFailed',
|
|
|
|
email: user.email,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsOpened(false)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[setManagedUserAlert, groupId, runResendGroupInviteAsync]
|
|
|
|
)
|
|
|
|
|
2023-08-22 17:38:37 -04:00
|
|
|
const onResendManagedUserInviteClick = () => {
|
|
|
|
handleResendManagedUserInvite(user)
|
|
|
|
}
|
2023-07-13 04:50:43 -04:00
|
|
|
|
2023-08-23 14:53:01 -04:00
|
|
|
const onResendGroupInviteClick = () => {
|
|
|
|
handleResendGroupInvite(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">
|
2023-08-23 14:53:01 -04:00
|
|
|
{userPending ? (
|
|
|
|
<MenuItemButton onClick={onResendGroupInviteClick}>
|
|
|
|
{t('resend_group_invite')}
|
|
|
|
{isResendingGroupInvite ? (
|
|
|
|
<Icon type="spinner" spin style={{ marginLeft: '5px' }} />
|
|
|
|
) : null}
|
|
|
|
</MenuItemButton>
|
|
|
|
) : null}
|
2023-08-22 17:38:37 -04:00
|
|
|
{userNotManaged ? (
|
|
|
|
<MenuItemButton onClick={onResendManagedUserInviteClick}>
|
|
|
|
{t('resend_managed_user_invite')}
|
|
|
|
{isResendingManagedUserInvite ? (
|
|
|
|
<Icon type="spinner" spin style={{ marginLeft: '5px' }} />
|
|
|
|
) : null}
|
|
|
|
</MenuItemButton>
|
|
|
|
) : null}
|
2023-08-24 06:29:37 -04:00
|
|
|
{user.enrollment?.managedBy ? (
|
2023-07-13 04:50:43 -04:00
|
|
|
<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>
|
|
|
|
)
|
|
|
|
}
|