diff --git a/src/components/pages/production/recording/form/RecordingForm.tsx b/src/components/pages/production/recording/form/RecordingForm.tsx index a59952ec..90e705f6 100644 --- a/src/components/pages/production/recording/form/RecordingForm.tsx +++ b/src/components/pages/production/recording/form/RecordingForm.tsx @@ -1277,6 +1277,18 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { }; } + // In migration mode (edit), the form field `product_warehouse_id` must hold + // the master product ID (what the dropdown options use as `value`), not the + // PW entity ID returned by the API. The API response has the product ID + // nested at stock.product_warehouse.product_id. + if (isMigrationMode && type === 'edit' && initialValues?.stocks?.length) { + baseValues.stocks = initialValues.stocks.map((stock) => ({ + product_warehouse_id: + stock.product_warehouse?.product_id ?? stock.product_warehouse_id, + qty: stock.usage_amount ?? '', + })); + } + if (!recordingRestriction.canEditStock) { baseValues.stocks = []; } @@ -1297,6 +1309,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { selectedKandang, recordingRestriction.canEditStock, recordingRestriction.canEditDepletion, + isMigrationMode, ]); const formik = useFormik< @@ -1408,6 +1421,20 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => { }, }); + // When migration mode activates after formik has initialized (SWR timing race), + // push the corrected stock values (product_id, not PW entity id) into the form. + useEffect(() => { + if (type !== 'edit' || !isMigrationMode || !initialValues?.stocks?.length) + return; + const migrationStocks = initialValues.stocks.map((stock) => ({ + product_warehouse_id: + stock.product_warehouse?.product_id ?? stock.product_warehouse_id, + qty: stock.usage_amount ?? '', + })); + formik.setFieldValue('stocks', migrationStocks); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [isMigrationMode]); // intentionally shallow — only run when migration mode flips + // ===== HELPER FUNCTIONS ===== const { setFieldValue } = formik;