All files / app/src/features/measure/ui/measure-calculation-data measure-calculation-data.tsx

64.81% Statements 70/108
75% Branches 3/4
50% Functions 3/6
64.81% Lines 70/108

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 1341x                               1x 3x 3x 3x 3x   3x 3x   3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x   3x 3x 3x 3x 3x                   3x 3x 3x 3x 3x                           3x 3x 3x 3x 3x                 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x   3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   3x 3x                         3x 3x 3x 3x   3x  
import { makeUpLengthLossUnit } from '@/entities/accounting-object'
import { useCurrencies } from '@/entities/currency'
import { LockPriceEnum } from '@/entities/pipe'
import { currentUserPermissionsAtom, PERMISSION_VIEW_PRICE, RoleProvider } from '@/features/rolevik'
import InfoIcon from '@/shared/assets/icons/common/passport.svg'
import { normalizeSelectOptionsFromEnum } from '@/shared/helpers'
import { SelectWithQuery, useTranslate } from '@/shared/lib'
import { FormBlockWrapper, Tooltip } from '@/shared/ui'
import { Input, RadioGroup, Select } from '@tmk/ui-kit'
import cn from 'classnames'
import { useAtomValue } from 'jotai'
import { FC } from 'react'
import { Controller, useFormContext } from 'react-hook-form'
export interface MeasureCalculationDataProps {
  isSameMakeUpLoss?: boolean
}
export const MeasureCalculationData: FC<MeasureCalculationDataProps> = ({ isSameMakeUpLoss }) => {
  const { t } = useTranslate(['accounting-object', 'common'])
  const { control, watch } = useFormContext()
  const priceType = watch('priceTypeOverride') as LockPriceEnum
  const currentUserPermissions = useAtomValue(currentUserPermissionsAtom)
 
  const isOverrideMakeUpLoss = watch('overrideMakeUpLengthLoss')
  const isHavePricePermission = currentUserPermissions.some(permission =>
    permission.permissions?.includes(PERMISSION_VIEW_PRICE)
  )
  return (
    <div className='bg-white w-full px-5 py-4 flex flex-col gap-y-2.5'>
      <h2>{t('Data for calculation')}</h2>
      <div className='grid grid-cols-6 items-center gap-x-2.5'>
        <RoleProvider availablePermissions={[PERMISSION_VIEW_PRICE]} emptySpace removeFromDom>
          <FormBlockWrapper
            childrenClassName='grid grid-cols-3'
            className={cn({
              'col-span-3': isHavePricePermission,
              hidden: !isHavePricePermission,
            })}
          >
            <div className='flex items-center gap-x-2.5 col-span-3'>
              <Controller
                name='priceOverride'
                control={control}
                render={({ field, fieldState: { error } }) => (
                  <Input
                    label={t(
                      priceType === LockPriceEnum.TON ? 'Price_per_ton_without_VAT' : 'Price_per_meter_without_VAT'
                    )}
                    error={error}
                    type='number'
                    {...field}
                  />
                )}
              />
              <Controller
                name='overrideCurrency'
                control={control}
                render={({ field, fieldState: { error } }) => (
                  <SelectWithQuery
                    label={t('common:Ed.ed')}
                    useQuery={useCurrencies}
                    labelKey='symbol'
                    valueKey={'@id'}
                    className='max-w-[140px]'
                    isApplySingleOption={true}
                    inputProps={{
                      error,
                    }}
                    {...field}
                  />
                )}
              />
              <Controller
                name='priceTypeOverride'
                control={control}
                render={({ field }) => (
                  <RadioGroup
                    className='col-span-1 flex items-center gap-x-2.5 mt-5 whitespace-nowrap'
                    options={normalizeSelectOptionsFromEnum(LockPriceEnum, t, {
                      translatePrefix: 'per_',
                    })}
                    {...field}
                  />
                )}
              />
            </div>
          </FormBlockWrapper>
        </RoleProvider>
        <FormBlockWrapper childrenClassName='grid grid-cols-3' className='col-span-2'>
          <div className='flex items-center gap-x-2.5 col-span-3'>
            <Controller
              name='overrideMakeUpLengthLoss'
              control={control}
              render={({ field, fieldState: { error } }) => (
                <Input type='number' label={t('Make_up_length_loss')} error={error} {...field} />
              )}
            />
            <Controller
              name='overrideMakeUpLengthLossNotation'
              control={control}
              render={({ field, fieldState: { error } }) => (
                <Select
                  label={t('common:Ed.ed')}
                  options={normalizeSelectOptionsFromEnum(makeUpLengthLossUnit, t, {
                    translatePrefix: 'common:',
                  })}
                  className='max-w-[140px]'
                  t={t}
                  inputProps={{
                    error,
                  }}
                  {...field}
                />
              )}
            />
            {isSameMakeUpLoss && (
              <div className='mt-5'>
                <Tooltip
                  label={t(
                    isOverrideMakeUpLoss
                      ? 'Attention! The entered length loss value during screwing differs from the value for the pipe'
                      : 'One of the objects has a significantly different length loss when screwing up'
                  )}
                >
                  <InfoIcon className='fill-red stroke-red' />
                </Tooltip>
              </div>
            )}
          </div>
        </FormBlockWrapper>
      </div>
    </div>
  )
}