All files / app/src/features/import/ui/accounting-object-import accounting-object-import.tsx

45.67% Statements 37/81
50% Branches 2/4
40% Functions 2/5
45.67% Lines 37/81

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 1001x                   1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x                                     6x 6x 6x 6x 6x 6x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   6x 6x                                 6x                                   6x 6x   6x  
import { FC } from 'react'
import { FormBlockWrapper } from '@/shared/ui'
import { Controller, useFormContext } from 'react-hook-form'
import { Select, SelectSearch } from '@tmk/ui-kit'
import { normalizeSelectOptions, normalizeSelectOptionsFromEnum } from '@/shared/helpers'
import { AccountingObjectTypeForImport, AccountingTypeForImport } from '@/entities/accounting-object'
import { useTranslate } from '@/shared/lib'
import { useRouter } from 'next/router'
import { useWarehouse, useWarehousesCollection } from '@/entities/warehouse'
 
export const AccountingObjectImport: FC = () => {
  const { t } = useTranslate(['accounting-object'])
  const { control, watch } = useFormContext()
  const router = useRouter()
  const warehouseId = router.query.warehouse
  const isObjectTypeSelected = watch('accountingObjectType')
  const { data: warehouses } = useWarehousesCollection({
    enabled: !!warehouseId,
  })
  const { data, isIdle, isLoading } = useWarehouse(warehouseId as string, {
    enabled: !!warehouseId,
  })
  const getOptions = () => {
    const warehouseOptions = normalizeSelectOptions(warehouses?.['hydra:member'], {
      labelKey: 'name',
      valueKey: '@id',
    })
 
    const isCurrentWarehouseIncluded = warehouses?.['hydra:member']?.some(
      warehouse => warehouse?.['@id'] === data?.['@id']
    )
 
    if (!isCurrentWarehouseIncluded && data) {
      warehouseOptions.push({
        label: data.name,
        id: data['@id'],
      })
    }
 
    return warehouseOptions
  }
  return (
    <div className='bg-white w-full px-5 py-4 mt-2.5'>
      <FormBlockWrapper childrenClassName='grid grid-cols-12 gap-x-2.5 items-center'>
        <Controller
          name='accountingObjectType'
          control={control}
          render={({ field, fieldState: { error } }) => (
            <Select
              label={t('Accounting_type')}
              options={normalizeSelectOptionsFromEnum(AccountingObjectTypeForImport, t)}
              className='col-span-3'
              t={t}
              inputProps={{
                error,
              }}
              {...field}
            />
          )}
        />
        {isObjectTypeSelected && (
          <Controller
            render={({ field, fieldState: { error } }) => (
              <Select
                label={t('Object_type') + ' *'}
                options={normalizeSelectOptionsFromEnum(AccountingTypeForImport, t)}
                className='col-span-4'
                t={t}
                inputProps={{
                  error,
                }}
                {...field}
              />
            )}
            name='accountingType'
          />
        )}
        {warehouseId && (
          <Controller
            name='warehouse'
            control={control}
            render={({ field }) => (
              <SelectSearch
                label={t('Warehouse')}
                {...field}
                className='col-span-3'
                options={getOptions()}
                value={warehouseId}
                disabled={!!data}
                t={t}
                isLoading={isIdle || isLoading}
              />
            )}
          />
        )}
      </FormBlockWrapper>
    </div>
  )
}