All files / app/src/entities/responsibility-person/ui/responsibility-person-block responsibility-person-block.tsx

92.3% Statements 60/65
66.66% Branches 4/6
50% Functions 3/6
92.3% Lines 60/65

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 931x                                           1x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x   2x 2x 2x   2x           2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   2x 2x 2x 2x 2x 2x   2x  
import { FormBlockWrapper } from '@/shared/ui'
import { Controller, useFormContext } from 'react-hook-form'
import { getNestedFieldName, useTranslate } from '@/shared/lib'
import { Button, Select } from '@tmk/ui-kit'
import { normalizeSelectOptions } from '@/shared/helpers'
import { useUsersCollection } from '@/entities/user'
import { SelectWithQuery } from '@/shared/lib'
import TrashIcon from '@/shared/assets/icons/common/TrashIcon.svg'
import { FCWithClassName } from '@/shared/@types'
import cn from 'classnames'
import { checkIsUniqueResponsibilityPerson } from '@/entities/responsibility-person'
import { filterPublicRoles, usePermissionsByRoles } from '@/features/rolevik'
import { useUpdateAtom } from 'jotai/utils'
import { deletedWarehouseResponsibilityPersonIdsAtom } from '@/entities/warehouse'
 
export interface ResponsibilityPersonBlockProps {
  parentName?: string
  isHidden?: boolean
  isLoading?: boolean
  remove?: () => void
}
 
export const ResponsibilityPersonBlock: FCWithClassName<ResponsibilityPersonBlockProps> = ({
  parentName,
  isHidden,
  remove,
  className,
}) => {
  const { control, watch, getValues } = useFormContext()
  const { t } = useTranslate(['accounting-object', 'common'])
  const { data, isLoading } = usePermissionsByRoles()
  const setDeletedResponsibilityPersonsIds = useUpdateAtom(deletedWarehouseResponsibilityPersonIdsAtom)
 
  if (isHidden) return null
 
  const name = parentName?.slice(0, -3) as string
  const persons = watch(name)
  const isHaveIRI = getValues(getNestedFieldName(parentName, '@id'))
 
  const onDelete = () => {
    if (isHaveIRI) {
      setDeletedResponsibilityPersonsIds(prev => [...prev, getValues(getNestedFieldName(parentName, 'id'))])
    }
    remove?.()
  }
  return (
    <FormBlockWrapper className={cn('!bg-gray-secondary', className)} childrenClassName='flex items-center gap-x-2.5'>
      <div className='grid grid-cols-2 gap-x-2.5 w-full'>
        <Controller
          name={getNestedFieldName(parentName, 'role')}
          control={control}
          render={({ field, fieldState: { error } }) => (
            <Select
              label={t('common:Role')}
              className='col-span-1'
              options={normalizeSelectOptions(filterPublicRoles(data?.['hydra:member']), {
                labelKey: entity => entity?.name || t(`common:${entity?.role}`),
                valueKey: 'role',
              })}
              t={t}
              isSaved={!!isHaveIRI}
              isLoading={isLoading}
              inputProps={{
                error,
              }}
              {...field}
            />
          )}
        />
        <Controller
          name={getNestedFieldName(parentName, 'user')}
          control={control}
          render={({ field, fieldState: { error } }) => (
            <SelectWithQuery
              label={t('common:FIO')}
              useQuery={useUsersCollection}
              labelKey='displayName'
              error={error}
              isSaved={!!isHaveIRI}
              validate={value => checkIsUniqueResponsibilityPerson(value, persons)}
              className='col-span-1'
              {...field}
            />
          )}
        />
      </div>
      <Button variant='icon' onClick={onDelete}>
        <TrashIcon className='cursor-pointer fill-blue-dark mt-5' />
      </Button>
    </FormBlockWrapper>
  )
}