managed-users-select-user-checkbox.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { useTranslation } from 'react-i18next'
  2. import type { User } from '../../../../../../types/group-management/user'
  3. import { useGroupMembersContext } from '../../context/group-members-context'
  4. import { useCallback } from 'react'
  5. type ManagedUsersSelectUserCheckboxProps = {
  6. user: User
  7. }
  8. export default function ManagedUsersSelectUserCheckbox({
  9. user,
  10. }: ManagedUsersSelectUserCheckboxProps) {
  11. const { t } = useTranslation()
  12. const { users, selectedUsers, selectUser, unselectUser } =
  13. useGroupMembersContext()
  14. const handleSelectUser = useCallback(
  15. (event, user) => {
  16. if (event.target.checked) {
  17. selectUser(user)
  18. } else {
  19. unselectUser(user)
  20. }
  21. },
  22. [selectUser, unselectUser]
  23. )
  24. // Pending: user.enrollment will be `undefined`
  25. // Non managed: user.enrollment will be an empty object
  26. const nonManagedUsers = users.filter(user => !user.enrollment?.managedBy)
  27. // Hide the entire `td` (entire column) if no more users available to be click
  28. // because all users are currently managed
  29. if (nonManagedUsers.length === 0) {
  30. return null
  31. }
  32. const selected = selectedUsers.includes(user)
  33. return (
  34. <td className="cell-checkbox">
  35. {/* the next check will hide the `checkbox` but still show the `td` */}
  36. {user.enrollment?.managedBy ? null : (
  37. <>
  38. <label htmlFor={`select-user-${user.email}`} className="sr-only">
  39. {t('select_user')}
  40. </label>
  41. <input
  42. className="select-item"
  43. id={`select-user-${user.email}`}
  44. type="checkbox"
  45. checked={selected}
  46. onChange={e => handleSelectUser(e, user)}
  47. />
  48. </>
  49. )}
  50. </td>
  51. )
  52. }