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 = ({ stockItemUuid, error, intiallvalue, onValueChange }) => { const { t } = useTranslation(); const { isLoading, error: stockItemError, item } = useStockItem(stockItemUuid); const initialSelectedItem = useMemo( () => (item?.packagingUnits ?? []).find((u) => u.uuid === intiallvalue) ?? item?.packagingUnits?.[0], [item?.packagingUnits, intiallvalue], ); useEffect(() => { if (initialSelectedItem) { onValueChange?.(initialSelectedItem?.uuid); } }, [initialSelectedItem, onValueChange]); if (isLoading) return ; if (stockItemError) return ( ); return ( 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;