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 | 4x 5x 5x 5x 5x 5x 5x 5x 5x 14x 9x 5x 5x 5x 9x 9x 5x 8x 5x 91x 91x 91x 91x 91x 5x 91x 91x 91x 91x 5x 1x 5x 1x 4x | import React, { useCallback, useEffect, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { ComboBox, SelectSkeleton } from '@carbon/react';
import { type StockBatchWithUoM } from '../../../core/api/types/stockItem/StockBatchDTO';
import { formatForDatePicker } from '../../../constants';
import { useStockItemBatchInformationHook } from '../../../stock-items/add-stock-item/batch-information/batch-information.resource';
import { useStockItemBatchNumbers } from '../hooks/useStockItemBatchNumbers';
interface BatchNoSelectorProps {
stockItemUuid: string;
initialValue?: string;
onValueChange?: (value: string) => void;
error?: string;
}
const BatchNoSelector: React.FC<BatchNoSelectorProps> = ({ stockItemUuid, error, initialValue, onValueChange }) => {
const { isLoading, stockItemBatchNos } = useStockItemBatchNumbers(stockItemUuid);
const { t } = useTranslation();
const { items, setStockItemUuid, isLoading: isLoadingBatchinfo } = useStockItemBatchInformationHook();
useEffect(() => {
setStockItemUuid(stockItemUuid);
}, [stockItemUuid, setStockItemUuid]);
const stockItemBatchesInfo = useMemo(() => {
Iif (!stockItemBatchNos) return [];
return stockItemBatchNos.map((item) => {
const matchingBatch = items?.find((batch) => batch.batchNumber === item.batchNo);
return matchingBatch
? ({
...item,
...matchingBatch,
quantity: String(matchingBatch.quantity),
} as StockBatchWithUoM)
: (item as StockBatchWithUoM);
});
}, [stockItemBatchNos, items]);
const filteredBatches = useMemo(() => {
Iif (!stockItemBatchesInfo) return [];
return stockItemBatchesInfo.filter((batch) => {
const quantity = typeof batch.quantity === 'string' ? parseFloat(batch.quantity) : batch.quantity;
return !isNaN(quantity) && quantity > 0;
});
}, [stockItemBatchesInfo]);
const initialSelectedItem = useMemo(
() => filteredBatches.find((batch) => batch.uuid === initialValue) ?? null,
[filteredBatches, initialValue],
);
const formatQuantityDisplay = useCallback(
(batch: StockBatchWithUoM): string => {
Iif (batch.quantity === undefined) return t('unknown', 'Unknown');
const quantity = typeof batch.quantity === 'string' ? parseFloat(batch.quantity) : batch.quantity;
Iif (isNaN(quantity)) return t('unknown', 'Unknown');
const baseQuantity = quantity.toString();
Eif (!batch.quantityUoM) return baseQuantity;
const withUnit = `${baseQuantity} ${batch.quantityUoM}`;
if (!batch.quantityFactor) return withUnit;
const factor = parseFloat(batch.quantityFactor);
return !isNaN(factor) && factor > 1 ? `${withUnit} (${factor} units each)` : withUnit;
},
[t],
);
const itemToString = useCallback(
(batch: StockBatchWithUoM | null): string => {
Iif (!batch?.batchNo) return '';
const quantityDisplay = formatQuantityDisplay(batch);
const expiryDate = batch.expiration ? formatForDatePicker(batch.expiration) : t('noExpiry', 'No expiry');
return `${batch.batchNo} | Qty: ${quantityDisplay} | Expiry: ${expiryDate}`;
},
[formatQuantityDisplay, t],
);
const handleChange = useCallback(
(data: { selectedItem?: StockBatchWithUoM | null }) => {
onValueChange?.(data.selectedItem?.uuid ?? '');
},
[onValueChange],
);
if (isLoading || isLoadingBatchinfo) {
return <SelectSkeleton role="progressbar" />;
}
return (
<ComboBox
id="stockBatchUuid"
invalid={!!error}
invalidText={error}
items={filteredBatches}
itemToString={itemToString}
name="stockBatchUuid"
onChange={handleChange}
placeholder={t('filterPlaceholder', 'Filter...')}
selectedItem={initialSelectedItem}
style={{ flexGrow: 1 }}
titleText={t('batchNo', 'Batch number')}
/>
);
};
export default BatchNoSelector;
|