All files / app/src/entities/accounting-object/ui/operation-shipment-destination-block operation-shipment-destination-block.tsx

93.93% Statements 31/33
37.5% Branches 3/8
66.66% Functions 2/3
93.93% Lines 31/33

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 521x                         1x 3x 3x 3x 3x 3x   3x 3x 3x   3x 3x 3x 3x 3x     3x 3x   3x 3x 2x   3x 3x 3x 3x 3x 3x 3x 2x   3x 3x   3x  
import { FC } from 'react'
import { Controller, useFormContext } from 'react-hook-form'
import { useTranslate } from '@/shared/lib'
import { getIdFromIRI, Input } from '@tmk/ui-kit'
import { OperationShipment, useAccountingObjectById } from '@/entities/accounting-object'
import { WareHouseContract, useWarehouseContractById } from '@/entities/warehouse-contract'
import { FormBlockWrapper } from '@/shared/ui'
import { BaseInfoPassport } from '@/features/passport'
 
export interface OperationShipmentDestinationBlockProps {
  accountingObjectId: string
  draftOperationData?: OperationShipment
}
export const OperationShipmentDestinationBlock: FC<OperationShipmentDestinationBlockProps> = ({
  accountingObjectId,
  draftOperationData,
}) => {
  const { t } = useTranslate(['accounting-object', 'common'])
  const { control, setValue } = useFormContext()
 
  const { data: accountingObject } = useAccountingObjectById(getIdFromIRI(accountingObjectId as string) as string, {
    enabled: !draftOperationData,
  })
 
  const { data: warehouseContract, isFetching } = useWarehouseContractById(
    accountingObject?.warehouseContract?.id as string,
    {
      enabled: !!accountingObject?.warehouseContract?.id && !draftOperationData,
      onSuccess: data => {
        setValue('client', data?.contract?.client?.['@id'] as string)
      },
    }
  )
 
  const clientValue =
    warehouseContract?.contract?.client?.name ||
    (draftOperationData?.warehouseContract as WareHouseContract)?.contract?.client?.name
 
  return (
    <FormBlockWrapper childrenClassName='grid grid-cols-2 gap-x-2.5' className='!bg-gray-secondary'>
      <BaseInfoPassport title={t('Client')} value={clientValue} className='bg-gray-tertiary' isLoading={isFetching} />
      <Controller
        name='orderClientId'
        control={control}
        render={({ field, fieldState: { error } }) => (
          <Input error={error} label={t('Customers order number') + ' *'} className='col-span-1' {...field} />
        )}
      />
    </FormBlockWrapper>
  )
}