feat(FE-137): enhance stock product selection in RecordingForm with initial values support

This commit is contained in:
rstubryan
2025-10-24 20:37:11 +07:00
parent e322e0d078
commit 81003eac63
@@ -552,9 +552,10 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
// Memoized unified products for stock selection
const unifiedStockProducts = useMemo(() => {
if (!isResponseSuccess(stockProducts)) return [];
const options: OptionType[] = [];
// Add products from API stockProducts
if (isResponseSuccess(stockProducts)) {
stockProducts.data.forEach((product) => {
const warehouse = product.warehouse;
const stockText = product.quantity.toLocaleString('id-ID');
@@ -578,9 +579,40 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
});
}
});
}
// Add existing stock products from initialValues (for detail/edit mode)
if (initialValues && 'stocks' in initialValues && initialValues.stocks && type !== 'add') {
const initialValuesWithStocks = initialValues as Recording & {
stocks?: Array<{
product_warehouse_id: number;
usage_amount: number;
notes: string;
product_warehouse?: {
id: number;
product_id: number;
product_name: string;
warehouse_id: number;
warehouse_name: string;
};
}>;
};
initialValuesWithStocks.stocks?.forEach((stock) => {
if (stock.product_warehouse && stock.product_warehouse.product_name) {
const existingOption = options.find(opt => opt.value === stock.product_warehouse_id);
if (!existingOption) {
options.push({
value: stock.product_warehouse_id,
label: `${stock.product_warehouse.product_name} - ${stock.product_warehouse.warehouse_name}`
});
}
}
});
}
return options;
}, [stockProducts, getProjectFlockLocation()]);
}, [stockProducts, getProjectFlockLocation(), initialValues, type]);
// Handle stock usage amount change - simplified following MovementForm pattern