import { QueryUserGroupForHolder } from '@/services/data/tuples/query-user-group-types' ; import { Space } from '@/services/data/tuples/space-types' ; import { listUserGroupsForHolder } from '@/services/data/tuples/user-group' ; import { UserGroupId } from '@/services/data/tuples/user-group-types' ; import { TupleItemPicker } from '@/widgets/tuple-workbench/tuple-item-picker' ; import React from 'react' ; const hasUserGroup = ( space ) => ! ! space . userGroupIds && space . userGroupIds . length > 0 ; const getUserGroupIds = ( space ) : Array < UserGroupId > => space . userGroupIds ; const findNameFromUserGroups = ( userGroupId , userGroups ) : string => { return userGroups . find ( userGroup => userGroup . userGroupId == userGroupId ) ! . name ; } ; const removeUserGroup = ( space ) => ( userGroupOrId ) => { let userGroupId ; if ( typeof userGroupOrId === 'string' ) { userGroupId = userGroupOrId ; } else { userGroupId = userGroupOrId . userGroupId ; } const index = space . userGroupIds . findIndex ( id => id == userGroupId ) ; if ( index !== - 1 ) { space . userGroupIds . splice ( index , 1 ) ; } } ; const addUserGroup = ( space ) => ( userGroup ) => { const { userGroupId } = userGroup ; const index = space . userGroupIds . findIndex ( id => id == userGroupId ) ; if ( index === - 1 ) { space . userGroupIds . push ( userGroupId ) ; } } ; const getIdOfUserGroup = ( userGroup ) => userGroup . userGroupId ; const getNameOfUserGroup = ( userGroup ) => userGroup . name ; const isUserGroupPicked = ( space ) => ( userGroup ) => space . userGroupIds . some ( userGroupId => userGroupId == userGroup . userGroupId ) ; export const UserGroupPicker = ( props ) => { const { label , space , codes } = props ; return < TupleItemPicker actionLabel = { label } holder = { space } codes = { codes } isHolding = { hasUserGroup } getHoldIds = { getUserGroupIds } getNameOfHold = { findNameFromUserGroups } listCandidates = { listUserGroupsForHolder } getIdOfCandidate = { getIdOfUserGroup } getNameOfCandidate = { getNameOfUserGroup } isCandidateHold = { isUserGroupPicked ( space ) } removeHold = { removeUserGroup ( space ) } addHold = { addUserGroup ( space ) } / > ; } ;