All files / app/src/features/measure/ui/row-rack-filter row-rack-filter.tsx

59.82% Statements 70/117
38.46% Branches 5/13
57.14% Functions 4/7
59.82% Lines 70/117

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 1x                                           1x   1x 1x 1x 3x   3x 2x     2x 2x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x                         2x 2x 2x 2x 2x 2x   3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                       2x   3x 3x   3x  
import { FC, useEffect, useState } from 'react'
import { MultiSelect, MultiSelectOption } from '@/shared/ui'
import { useRacksByWarehouseCollection } from '@/entities/rack'
import { getIdFromIRI, normalizeSelectOptions } from '@/shared/helpers'
import { queryFetchRowsByRackId } from '@/entities/row'
import { useTranslate } from '@/shared/lib'
import { Controller, useFormContext } from 'react-hook-form'
import { Nullable, SelectOption } from '@/shared/@types'
import { useUpdateAtom } from 'jotai/utils'
import { MeasureFilterAtomType, measureFiltersAtom } from '@/features/measure'
 
export const RowRackFilter: FC = () => {
  const { t } = useTranslate(['accounting-object', 'common'])
  const [isRowsLoading, setIsRowsLoading] = useState(false)
  const [rackSearchValue, setRackSearchValue] = useState('')
  const [rowSearchValue, setRowSearchValue] = useState('')
  const [racks, setRacks] = useState<Nullable<MultiSelectOption[]>>([])
  const [rows, setRows] = useState<Nullable<SelectOption[]>>([])
  const { watch, control } = useFormContext()
  const warehouse = watch('warehouse')
  const setFilterAtom = useUpdateAtom(measureFiltersAtom)
 
  const rackValue = watch('rack') as MultiSelectOption[]
  const rowValue = watch('row') as MultiSelectOption[]
  const { data, isLoading } = useRacksByWarehouseCollection(getIdFromIRI(warehouse) as string, {
    enabled: !!warehouse,
    filters: {
      name: rackSearchValue,
    },
  })
 
  useEffect(() => {
    async function fetchRows() {
      setIsRowsLoading(true)
      const promises =
        racks?.map(async rack => {
          return queryFetchRowsByRackId(getIdFromIRI(rack.id) as string)({
            params: {
              itemsPerPage: 400,
            },
          })()
        }) || []
      const res = await Promise.all(promises)
      setRows(
        res.flatMap(
          (row, rowIndex) =>
            row?.['hydra:member'].map(row => ({
              id: row.id,
              label: `${racks?.[rowIndex]?.label || ''} / ${row?.orderNumber || ''}`,
            })) || []
        )
      )
      setIsRowsLoading(false)
    }
    if (racks?.length) {
      fetchRows()
    } else {
      setRows([])
    }
  }, [racks])
 
  const getRowsOptions = () => {
    if (rowSearchValue) {
      return rows?.filter(row => row.label.toLowerCase().includes(rowSearchValue.toLowerCase())) || []
    }
    return rows || []
  }
  return (
    <>
      <Controller
        name='rack'
        control={control}
        render={({ field, fieldState: { error } }) => (
          <MultiSelect
            label={t('Rack')}
            inputProps={{
              error,
            }}
            disabled={!data?.['hydra:member']?.length || !warehouse}
            onChangeWithOptions={value => {
              setRacks(value || [])
              field.onChange(value)
              setFilterAtom(
                prev =>
                  ({
                    ...prev,
                    rack: {
                      value,
                    },
                  } as MeasureFilterAtomType)
              )
            }}
            isLoading={isLoading}
            options={normalizeSelectOptions(data?.['hydra:member'])}
            onCustomSearch={setRackSearchValue}
            {...field}
            value={rackValue?.map(({ id }) => id) || []}
          />
        )}
      />
      <Controller
        name='row'
        control={control}
        render={({ field, fieldState: { error } }) => (
          <MultiSelect
            label={t('Row')}
            inputProps={{
              error,
            }}
            disabled={!rackValue}
            options={getRowsOptions()}
            isLoading={isRowsLoading}
            {...field}
            value={rowValue?.map(({ id }) => id) || []}
            onCustomSearch={setRowSearchValue}
            onChangeWithOptions={value => {
              field.onChange(value)
              setFilterAtom(
                prev =>
                  ({
                    ...prev,
                    row: {
                      value,
                    },
                  } as MeasureFilterAtomType)
              )
            }}
          />
        )}
      />
    </>
  )
}