All files / src/stock-operations/stock-operations-forms/input-components quantity-uom-selector.component.tsx

100% Statements 16/16
92.85% Branches 13/14
100% Functions 6/6
100% Lines 14/14

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                          4x 8x 8x 8x 14x     8x 8x 7x       8x   7x 7x               5x               26x       1x                  
import React, { useEffect, useMemo } from 'react';
import { ComboBox, InlineNotification, SkeletonText } from '@carbon/react';
import { useTranslation } from 'react-i18next';
import { type StockItemPackagingUOMDTO } from '../../../core/api/types/stockItem/StockItemPackagingUOM';
import { useStockItem } from '../../../stock-items/stock-items.resource';
 
interface QtyUomSelectorProps {
  stockItemUuid: string;
  intiallvalue?: string;
  onValueChange?: (value: string) => void;
  error?: string;
}
 
const QtyUomSelector: React.FC<QtyUomSelectorProps> = ({ stockItemUuid, error, intiallvalue, onValueChange }) => {
  const { t } = useTranslation();
  const { isLoading, error: stockItemError, item } = useStockItem(stockItemUuid);
  const initialSelectedItem = useMemo<StockItemPackagingUOMDTO | null>(
    () => (item?.packagingUnits ?? []).find((u) => u.uuid === intiallvalue) ?? item?.packagingUnits?.[0],
    [item?.packagingUnits, intiallvalue],
  );
  useEffect(() => {
    if (initialSelectedItem) {
      onValueChange?.(initialSelectedItem?.uuid);
    }
  }, [initialSelectedItem, onValueChange]);
 
  if (isLoading) return <SkeletonText />;
 
  if (stockItemError)
    return (
      <InlineNotification
        kind="error"
        title={t('packagingUomError', 'Error loading Stock item')}
        subtitle={stockItemError?.message}
      />
    );
 
  return (
    <ComboBox
      id={'stockItemPackagingUOMUuid'}
      initialSelectedItem={initialSelectedItem}
      invalid={!!error}
      invalidText={error}
      items={item?.packagingUnits ?? []}
      itemToString={(s: StockItemPackagingUOMDTO) =>
        s?.packagingUomName ? `${s?.packagingUomName} - ${s?.factor} ` : ''
      }
      name={'stockItemPackagingUOMUuid'}
      onChange={(data: { selectedItem?: StockItemPackagingUOMDTO }) => {
        onValueChange?.(data.selectedItem?.uuid);
      }}
      placeholder={t('filterPlaceholder', 'Filter...')}
      titleText={t('quantityUom', 'Quantity unit of measurement')}
    />
  );
};
 
export default QtyUomSelector;