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

75.55% Statements 34/45
33.33% Branches 1/3
33.33% Functions 1/3
75.55% Lines 34/45

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 641x                             1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x   2x 2x 2x                 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x       2x 2x 2x   2x  
import { FC } from 'react'
import { Button } from '@tmk/ui-kit'
import PlusIcon from '@/shared/assets/icons/common/plus.svg'
import { FormBlockWrapper } from '@/shared/ui'
import { useFieldArray, useFormContext } from 'react-hook-form'
import { getNestedFieldName, useTranslate } from '@/shared/lib'
import { getAppendNomenclatureBody, NomenclatureBlock } from '@/entities/nomenclature'
import { useRouter } from 'next/router'
import { useWarehouseContractNomenclaturesCollection } from '@/entities/warehouse-contract'
import { PERMISSION_MANAGER_WAREHOUSE_NOMENCLATURE, RoleProvider } from '@/features/rolevik'
 
export interface GroupNomenclatureProps {
  parentName?: string
}
 
export const GroupNomenclature: FC<GroupNomenclatureProps> = ({ parentName }) => {
  const { control, getValues, setValue } = useFormContext()
  const router = useRouter()
  const isEdit = router.pathname.includes('edit')
  const { t } = useTranslate(['accounting-object'])
  const warehouseID = router.query.pid as string
  const name = `${parentName}nomenclatures`
  const { fields, append, remove } = useFieldArray({
    control,
    name,
  })
 
  const contractId = getValues(getNestedFieldName(parentName, 'id')) as string
 
  useWarehouseContractNomenclaturesCollection(warehouseID, contractId, {
    enabled: !!contractId && isEdit && !!warehouseID,
    onSuccess: data => {
      if (data['hydra:member'].length === 0) {
        if (fields.length === 0) {
          append(getAppendNomenclatureBody())
        }
      } else {
        return setValue(name, data['hydra:member'])
      }
    },
    key: [contractId, warehouseID, 'nomenclatures-for-edit'],
  })
 
  return (
    <FormBlockWrapper childrenClassName='flex flex-col gap-y-2.5'>
      <div className='flex items-center gap-x-2.5'>
        <h1>{t('Nomenclature')}</h1>
        <RoleProvider availablePermissions={[PERMISSION_MANAGER_WAREHOUSE_NOMENCLATURE]}>
          <Button variant='border-icon' onClick={() => append(getAppendNomenclatureBody())}>
            <PlusIcon className='stroke-currentColor' />
          </Button>
        </RoleProvider>
      </div>
      <div className='flex flex-col gap-2.5'>
        {fields.map((item, index) => (
          <RoleProvider key={item.id} availablePermissions={[PERMISSION_MANAGER_WAREHOUSE_NOMENCLATURE]} emptySpace>
            <NomenclatureBlock contractId={contractId} parentName={`${name}.${index}.`} remove={() => remove(index)} />
          </RoleProvider>
        ))}
      </div>
    </FormBlockWrapper>
  )
}