2023-10-27 09:11:34 -04:00
|
|
|
import React, { useCallback } from 'react'
|
2023-08-30 11:20:57 -04:00
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { useGroupMembersContext } from '../../context/group-members-context'
|
|
|
|
|
2023-10-27 09:11:34 -04:00
|
|
|
export default function SelectAllCheckbox() {
|
2023-08-30 11:20:57 -04:00
|
|
|
const { t } = useTranslation()
|
|
|
|
|
|
|
|
const { selectedUsers, users, selectAllNonManagedUsers, unselectAllUsers } =
|
|
|
|
useGroupMembersContext()
|
|
|
|
|
|
|
|
const handleSelectAllNonManagedClick = useCallback(
|
|
|
|
(e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
if (e.target.checked) {
|
|
|
|
selectAllNonManagedUsers()
|
|
|
|
} else {
|
|
|
|
unselectAllUsers()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[selectAllNonManagedUsers, unselectAllUsers]
|
|
|
|
)
|
|
|
|
|
|
|
|
// Pending: user.enrollment will be `undefined`
|
|
|
|
// Not managed: user.enrollment will be an empty object
|
|
|
|
const nonManagedUsers = users.filter(user => !user.enrollment?.managedBy)
|
|
|
|
|
|
|
|
if (nonManagedUsers.length === 0) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-10-27 09:11:34 -04:00
|
|
|
<td className="cell-checkbox">
|
2023-08-30 11:20:57 -04:00
|
|
|
<label htmlFor="select-all" className="sr-only">
|
|
|
|
{t('select_all')}
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
className="select-all"
|
|
|
|
id="select-all"
|
|
|
|
type="checkbox"
|
2024-05-06 03:44:35 -04:00
|
|
|
autoComplete="off"
|
2023-08-30 11:20:57 -04:00
|
|
|
onChange={handleSelectAllNonManagedClick}
|
|
|
|
checked={selectedUsers.length === nonManagedUsers.length}
|
|
|
|
/>
|
|
|
|
</td>
|
|
|
|
)
|
|
|
|
}
|