All files / app/src/entities/nomenclature/ui/nomenclature-block nomenclature-block.tsx

90.9% Statements 120/132
100% Branches 8/8
87.5% Functions 7/8
90.9% Lines 120/132

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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 1661x                                             1x 2x 2x 2x   2x     2x                         2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x   2x 2x   2x  
import { FC } from 'react'
import { FormBlockWrapper } from '@/shared/ui'
import { AccountingType, AccountObjectType } from '@/entities/accounting-object'
import { normalizeSelectOptionsFromEnum } from '@/shared/helpers'
import { getNestedFieldName, SelectWithQuery, useTranslate } from '@/shared/lib'
import { Controller, useFormContext } from 'react-hook-form'
import {
  useNomenclatureConnectionsCollection,
  useNomenclatureSidesByDiameterCollection,
  useNomenclatureDiametersByConnectionCollection,
  useNomenclatureStrengthGroupsCollection,
} from '@/entities/nomenclature'
import { Button, Select } from '@tmk/ui-kit'
import TrashIcon from '@/shared/assets/icons/common/TrashIcon.svg'
import { PERMISSION_MANAGER_WAREHOUSE_NOMENCLATURE, RoleProvider } from '@/features/rolevik'
import { useUpdateAtom } from 'jotai/utils'
import { deletedNomenclatureIdsAtom } from '@/entities/warehouse'
export interface NomenclatureBlockProps {
  parentName?: string
  remove?: () => void
  contractId?: string
}
 
export const NomenclatureBlock: FC<NomenclatureBlockProps> = ({ parentName, remove, contractId, ...rest }) => {
  const { t } = useTranslate(['accounting-object'])
  const { control, watch, getValues } = useFormContext()
  const setDeletedNomenclatureIds = useUpdateAtom(deletedNomenclatureIdsAtom)
 
  const accountObjectTypeName = getNestedFieldName(parentName, 'accountingObjectType')
  // Когда вырезали AccountObjectType.CUSTOM  здесь не убрали, поэтому поставил пока false
  //  const isCustom = watch(accountObjectTypeName) === AccountObjectType.CUSTOM
  const onDelete = () => {
    const isHaveIRI = getValues(getNestedFieldName(parentName, '@id'))
    if (isHaveIRI) {
      setDeletedNomenclatureIds(prev => [
        ...prev,
        {
          id: getValues(getNestedFieldName(parentName, 'id')),
          contractId,
        },
      ])
    }
    remove?.()
  }
  const isCustom = false
  return (
    <FormBlockWrapper className='!bg-gray-secondary' childrenClassName='flex flex-col gap-y-2.5' {...rest}>
      <>
        <div className='grid grid-cols-4 gap-x-2.5 items-center'>
          <Controller
            name={accountObjectTypeName}
            control={control}
            render={({ field, fieldState: { error } }) => (
              <Select
                label={t('Accounting_type')}
                options={normalizeSelectOptionsFromEnum(AccountObjectType, t)}
                className='col-span-1'
                t={t}
                inputProps={{
                  error,
                }}
                {...field}
              />
            )}
          />
          <Controller
            render={({ field, fieldState: { error } }) => (
              <Select
                label={t('Object_type')}
                options={normalizeSelectOptionsFromEnum(AccountingType, t)}
                className='col-span-1'
                t={t}
                inputProps={{
                  error,
                }}
                {...field}
              />
            )}
            name={getNestedFieldName(parentName, 'accountingType')}
          />
          <div className='col-span-2 mt-5 w-full flex justify-end'>
            <RoleProvider availablePermissions={[PERMISSION_MANAGER_WAREHOUSE_NOMENCLATURE]}>
              <Button variant='icon' childrenClassName='flex items-center gap-x-2' onClick={onDelete}>
                <TrashIcon className='fill-blue-dark' />
                <span className='text-main-semibold text-blue-dark'>{t('common:Delete')}</span>
              </Button>
            </RoleProvider>
          </div>
        </div>
        {!isCustom && (
          <div className='grid grid-cols-12 gap-2.5'>
            <Controller
              name={getNestedFieldName(parentName, 'connection')}
              control={control}
              render={({ field, fieldState: { error } }) => (
                <SelectWithQuery
                  label={t('Connection')}
                  useQuery={useNomenclatureConnectionsCollection}
                  error={error}
                  className='col-span-3'
                  {...field}
                />
              )}
            />
            <Controller
              name={getNestedFieldName(parentName, 'diameter')}
              control={control}
              render={({ field, fieldState: { error } }) => (
                <SelectWithQuery
                  label={t('Diameter')}
                  subType='number'
                  dependentEntityType='id'
                  dependentEntities={{
                    connection: watch(getNestedFieldName(parentName, 'connection')),
                  }}
                  useQuery={useNomenclatureDiametersByConnectionCollection}
                  labelKey='value'
                  error={error}
                  className='col-span-3'
                  {...field}
                />
              )}
            />
            <Controller
              name={getNestedFieldName(parentName, 'side')}
              control={control}
              render={({ field, fieldState: { error } }) => (
                <SelectWithQuery
                  label={t('Pipe_wall')}
                  subType='number'
                  dependentEntityType='id'
                  dependentEntities={{
                    diameter: watch(getNestedFieldName(parentName, 'diameter')),
                  }}
                  useQuery={useNomenclatureSidesByDiameterCollection}
                  labelKey='value'
                  error={error}
                  className='col-span-3'
                  {...field}
                />
              )}
            />
            <Controller
              name={getNestedFieldName(parentName, 'strengthGroup')}
              control={control}
              render={({ field, fieldState: { error } }) => (
                <SelectWithQuery
                  label={t('Strength_Group')}
                  dependentEntityType='filter'
                  dependentEntities={{
                    connection: watch(getNestedFieldName(parentName, 'connection')),
                  }}
                  useQuery={useNomenclatureStrengthGroupsCollection}
                  error={error}
                  className='col-span-3'
                  {...field}
                />
              )}
            />
          </div>
        )}
      </>
    </FormBlockWrapper>
  )
}