2023-02-06 07:30:57 -05:00
|
|
|
import { useCallback, useState } from 'react'
|
|
|
|
import { User } from '../../../../../types/group-management/user'
|
|
|
|
|
|
|
|
export default function useUserSelection(initialUsers: User[]) {
|
|
|
|
const [users, setUsers] = useState<User[]>(initialUsers)
|
|
|
|
const [selectedUsers, setSelectedUsers] = useState<User[]>([])
|
|
|
|
|
|
|
|
const selectAllUsers = () => setSelectedUsers(users)
|
|
|
|
const unselectAllUsers = () => setSelectedUsers([])
|
|
|
|
|
2023-08-30 11:20:57 -04:00
|
|
|
const selectAllNonManagedUsers = useCallback(() => {
|
|
|
|
// Pending: user.enrollment will be `undefined`
|
|
|
|
// Not managed: user.enrollment will be an empty object
|
|
|
|
const nonManagedUsers = users.filter(user => !user.enrollment?.managedBy)
|
|
|
|
|
|
|
|
setSelectedUsers(nonManagedUsers)
|
|
|
|
}, [users])
|
|
|
|
|
2023-02-06 07:30:57 -05:00
|
|
|
const selectUser = useCallback((user: User) => {
|
|
|
|
setSelectedUsers(users => [...users, user])
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const unselectUser = useCallback((user: User) => {
|
|
|
|
setSelectedUsers(users => users.filter(u => u.email !== user.email))
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return {
|
|
|
|
users,
|
|
|
|
setUsers,
|
|
|
|
selectedUsers,
|
|
|
|
selectUser,
|
|
|
|
unselectUser,
|
|
|
|
selectAllUsers,
|
|
|
|
unselectAllUsers,
|
2023-08-30 11:20:57 -04:00
|
|
|
selectAllNonManagedUsers,
|
2023-02-06 07:30:57 -05:00
|
|
|
}
|
|
|
|
}
|