All files / app/src/features/measure/ui/measure-base-filters measure-base-filters.tsx

1.58% Statements 2/126
100% Branches 0/0
0% Functions 0/1
1.58% Lines 2/126

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                                                                                                                                                                                                                                                                                  
import { FC, useEffect, useState } from 'react'
import { SelectWithQuery, useTranslate } from '@/shared/lib'
import { Select } from '@tmk/ui-kit'
import { Controller, useFormContext } from 'react-hook-form'
import { normalizeSelectOptionsFromEnum } from '@/shared/helpers'
import {
  accountingObjectClientAtom,
  accountingObjectContractAtom,
  accountingObjectWarehouseAtom,
  AccountObjectType,
} from '@/entities/accounting-object'
import { FormBlockWrapper } from '@/shared/ui'
import { useClientsCollection } from '@/entities/client'
import dayjs from 'dayjs'
import { DEFAULT_DATE_FORMAT } from '@/shared/config'
import { useContracts } from '@/entities/contract'
import { useWarehousesCollection } from '@/entities/warehouse'
import { useBatchesCollection } from '@/entities/batch'
import {
  canRedirectWithoutConfirmFromMeasureAtom,
  measureFiltersAtom,
  RowRackFilter,
  setMeasureFilterValue,
} from '@/features/measure'
import { useAtom, useSetAtom } from 'jotai'
import { BaseFilter } from '@/shared/@types'
import { useUpdateAtom } from 'jotai/utils'
 
export const MeasureBaseFilters: FC = () => {
  const { t } = useTranslate(['accounting-object', 'common'])
  const { control, watch } = useFormContext()
  const setClient = useSetAtom(accountingObjectClientAtom)
  const setContract = useSetAtom(accountingObjectContractAtom)
  const setWarehouse = useSetAtom(accountingObjectWarehouseAtom)
  const setFilterAtom = useUpdateAtom(measureFiltersAtom)
  const isObjectTypeSelected = watch('accountingObjectType')
  const [canRedirectWithoutConfirmFromMeasure, setCanRedirectWithoutConfirmFromMeasure] = useAtom(
    canRedirectWithoutConfirmFromMeasureAtom
  )
 
  const [isFirstRender, setIsFirstRender] = useState(true)
 
  useEffect(() => {
    const subscription = watch(() => {
      if (isFirstRender) setIsFirstRender(false)
      else setCanRedirectWithoutConfirmFromMeasure(false)
    })
 
    return () => subscription.unsubscribe()
  }, [watch, canRedirectWithoutConfirmFromMeasure, isFirstRender, setCanRedirectWithoutConfirmFromMeasure])
 
  return (
    <div className='bg-white px-5 py-4 mt-2.5'>
      <FormBlockWrapper childrenClassName='grid grid-cols-4 gap-2.5'>
        <Controller
          name='accountingObjectType'
          control={control}
          render={({ field, fieldState: { error } }) => (
            <Select
              label={t('Accounting_type') + ' *'}
              options={normalizeSelectOptionsFromEnum(AccountObjectType, t)}
              className='col-span-1'
              t={t}
              inputProps={{
                error,
              }}
              {...field}
            />
          )}
        />
        {isObjectTypeSelected && (
          <>
            <Controller
              name='client'
              control={control}
              render={({ field, fieldState: { error } }) => (
                <SelectWithQuery
                  label={t('Client')}
                  useQuery={useClientsCollection}
                  error={error}
                  className='col-span-1'
                  onChangeWithOptions={value => {
                    setClient(value as BaseFilter)
                    setMeasureFilterValue(setFilterAtom, 'client', value)
                  }}
                  {...field}
                  isApplySingleOption
                  disableIfEmptyDependentEntities={false}
                  disableOnEmptyOptions={false}
                  loadingOnMount={false}
                />
              )}
            />
            <Controller
              name='contract'
              control={control}
              render={({ field, fieldState: { error } }) => (
                <SelectWithQuery
                  label={t('Contract') + ' *'}
                  useQuery={useContracts}
                  error={error}
                  transformLabel={contract =>
                    `${contract?.number}  ${t('common:from')} ${dayjs(contract?.date).format(DEFAULT_DATE_FORMAT)}`
                  }
                  onChangeWithOptions={value => {
                    setContract(value as BaseFilter)
                    setMeasureFilterValue(setFilterAtom, 'contract', value)
                  }}
                  disableIfEmptyDependentEntities={false}
                  disableOnEmptyOptions={false}
                  isApplySingleOption
                  loadingOnMount={false}
                  {...field}
                />
              )}
            />
            <Controller
              name='warehouse'
              control={control}
              render={({ field, fieldState: { error } }) => (
                <SelectWithQuery
                  label={t('Warehouse')}
                  useQuery={useWarehousesCollection}
                  error={error}
                  onChangeWithOptions={value => {
                    setWarehouse(value as BaseFilter)
                    setMeasureFilterValue(setFilterAtom, 'warehouse', value)
                  }}
                  disableIfEmptyDependentEntities={false}
                  disableOnEmptyOptions={false}
                  isApplySingleOption
                  loadingOnMount={false}
                  {...field}
                />
              )}
            />
            <Controller
              name='batch'
              control={control}
              render={({ field, fieldState: { error } }) => (
                <SelectWithQuery
                  label={t('Lot')}
                  useQuery={useBatchesCollection}
                  error={error}
                  dependentEntityType='filter'
                  dependentEntities={{
                    'warehouseContract.contract': watch('contract'),
                  }}
                  labelKey='number'
                  disableIfEmptyDependentEntities={false}
                  disableOnEmptyOptions={false}
                  {...field}
                  onChangeWithOptions={value => {
                    setMeasureFilterValue(setFilterAtom, 'batch', value)
                  }}
                />
              )}
            />
            <RowRackFilter />
          </>
        )}
      </FormBlockWrapper>
    </div>
  )
}