import { QuerySpaceForHolder } from '@/services/data/tuples/query-space-types' ; import { listSpacesForHolder } from '@/services/data/tuples/space' ; import { SpaceId } from '@/services/data/tuples/space-types' ; import { UserGroup } from '@/services/data/tuples/user-group-types' ; import { TupleItemPicker } from '@/widgets/tuple-workbench/tuple-item-picker' ; import React from 'react' ; const hasSpace = ( userGroup ) => ! ! userGroup . spaceIds && userGroup . spaceIds . length > 0 ; const getSpaceIds = ( userGroup ) : Array < SpaceId > => userGroup . spaceIds ; const findNameFromSpaces = ( spaceId , spaces ) : string => { return spaces . find ( space => space . spaceId == spaceId ) ! . name ; } ; const removeSpace = ( userGroup ) => ( spaceOrId ) => { let spaceId ; if ( typeof spaceOrId === 'string' ) { spaceId = spaceOrId ; } else { spaceId = spaceOrId . spaceId ; } const index = userGroup . spaceIds . findIndex ( id => id == spaceId ) ; if ( index !== - 1 ) { userGroup . spaceIds . splice ( index , 1 ) ; } } ; const addSpace = ( userGroup ) => ( space ) => { const { spaceId } = space ; const index = userGroup . spaceIds . findIndex ( id => id == spaceId ) ; if ( index === - 1 ) { userGroup . spaceIds . push ( spaceId ) ; } } ; const getIdOfSpace = ( space ) => space . spaceId ; const getNameOfSpace = ( space ) => space . name ; const isSpacePicked = ( userGroup ) => ( space ) => userGroup . spaceIds . some ( spaceId => spaceId == space . spaceId ) ; export const SpacePicker = ( props ) => { const { label , userGroup , codes } = props ; return < TupleItemPicker actionLabel = { label } holder = { userGroup } codes = { codes } isHolding = { hasSpace } getHoldIds = { getSpaceIds } getNameOfHold = { findNameFromSpaces } listCandidates = { listSpacesForHolder } getIdOfCandidate = { getIdOfSpace } getNameOfCandidate = { getNameOfSpace } isCandidateHold = { isSpacePicked ( userGroup ) } removeHold = { removeSpace ( userGroup ) } addHold = { addSpace ( userGroup ) } / > ; } ;