import { QueryUserForHolder } from '@/services/data/tuples/query-user-types' ; import { listUsersForHolder } from '@/services/data/tuples/user' ; import { UserGroup } from '@/services/data/tuples/user-group-types' ; import { UserId } from '@/services/data/tuples/user-types' ; import { TupleItemPicker } from '@/widgets/tuple-workbench/tuple-item-picker' ; import React from 'react' ; const hasUser = ( userGroup ) => ! ! userGroup . userIds && userGroup . userIds . length > 0 ; const getUserIds = ( userGroup ) : Array < UserId > => userGroup . userIds ; const findNameFromUsers = ( userId , users ) : string => { return users . find ( user => user . userId == userId ) ! . name ; } ; const removeUser = ( userGroup ) => ( userOrId ) => { let userId ; if ( typeof userOrId === 'string' ) { userId = userOrId ; } else { userId = userOrId . userId ; } const index = userGroup . userIds . findIndex ( id => id == userId ) ; if ( index !== - 1 ) { userGroup . userIds . splice ( index , 1 ) ; } } ; const addUser = ( userGroup ) => ( user ) => { const { userId } = user ; const index = userGroup . userIds . findIndex ( id => id == userId ) ; if ( index === - 1 ) { userGroup . userIds . push ( userId ) ; } } ; const getIdOfUser = ( user ) => user . userId ; const getNameOfUser = ( user ) => user . name ; const isUserPicked = ( userGroup ) => ( user ) => userGroup . userIds . some ( userId => userId == user . userId ) ; export const UserPicker = ( props ) => { const { label , userGroup , codes } = props ; return < TupleItemPicker actionLabel = { label } holder = { userGroup } codes = { codes } isHolding = { hasUser } getHoldIds = { getUserIds } getNameOfHold = { findNameFromUsers } listCandidates = { listUsersForHolder } getIdOfCandidate = { getIdOfUser } getNameOfCandidate = { getNameOfUser } isCandidateHold = { isUserPicked ( userGroup ) } removeHold = { removeUser ( userGroup ) } addHold = { addUser ( userGroup ) } / > ; } ;