diff --git a/native/components/StockFormContext/DeliveryFormContextProvider.tsx b/native/components/StockFormContext/DeliveryFormContextProvider.tsx index a4d90344..76145775 100644 --- a/native/components/StockFormContext/DeliveryFormContextProvider.tsx +++ b/native/components/StockFormContext/DeliveryFormContextProvider.tsx @@ -20,6 +20,7 @@ export const DeliveryFormContextProvider = ({ const processedInvoice = useAppSelector( documentScannerSelector.selectProcessedInvoice ); + const newMatched = useAppSelector(documentScannerSelector.selectNewMatched); const methods = useForm({ defaultValues: { product_records: {}, recipe_records: {} }, }); @@ -49,5 +50,27 @@ export const DeliveryFormContextProvider = ({ } }, [processedInvoice]); + useEffect(() => { + const valuesForForm = newMatched; + + for (const record_id in valuesForForm) { + if (record_id in dirtyFields) continue; + methods.setValue( + `product_records.${record_id}.quantity`, + valuesForForm[record_id].quantity, + { + shouldDirty: true, + } + ); + methods.setValue( + `product_records.${record_id}.price_per_unit`, + valuesForForm[record_id].price_per_unit, + { + shouldDirty: true, + } + ); + } + }, [newMatched]); + return {children}; }; diff --git a/native/redux/documentScannerSlice.ts b/native/redux/documentScannerSlice.ts index 3b7d627b..e6351ec2 100644 --- a/native/redux/documentScannerSlice.ts +++ b/native/redux/documentScannerSlice.ts @@ -12,6 +12,13 @@ interface DocumentScannerSlice { isCameraReady: boolean | null; photo: CameraCapturedPicture | undefined | null; processedInvoice: ProcessInvoiceResponse; + newMatched: { + [recordId: number]: { + product_id: number; + price_per_unit: number; + quantity: number; + }; + }; processedSalesRaport: ProcessSalesRaportResponse; inventory_id: number | null; } @@ -22,6 +29,7 @@ const initialState: DocumentScannerSlice = { isCameraReady: null, photo: null, processedInvoice: null, + newMatched: {}, processedSalesRaport: null, inventory_id: null, } as DocumentScannerSlice; @@ -62,6 +70,14 @@ export const documentScannerSlice = createSlice({ processedInvoice: DocumentScannerSlice["processedInvoice"]; }> ) => ({ ...state, processedInvoice: payload.processedInvoice }), + SET_NEW_MATCHED: ( + state, + { + payload, + }: PayloadAction<{ + newMatched: DocumentScannerSlice["newMatched"]; + }> + ) => ({ ...state, newMatched: payload.newMatched }), SET_PROCESSED_SALES_RAPORT: ( state, { @@ -91,6 +107,7 @@ export const documentScannerSlice = createSlice({ selectisTakingPhoto: (state) => state.isTakingPhoto, selectPhoto: (state) => state.photo, selectInventoryId: (state) => state.inventory_id, + selectNewMatched: (state) => state.newMatched, selectProcessedInvoice: (state) => state.processedInvoice, selectProcessedSalesRaport: (state) => state.processedSalesRaport, selectInvoiceUnmatchedAliases: (state) => diff --git a/native/screens/IdentifyAliasesScreen/Invoice.tsx b/native/screens/IdentifyAliasesScreen/Invoice.tsx index 7d8cae54..ea9631b8 100644 --- a/native/screens/IdentifyAliasesScreen/Invoice.tsx +++ b/native/screens/IdentifyAliasesScreen/Invoice.tsx @@ -104,10 +104,6 @@ export const IdentifyAliasesScreenInvoice = () => { useEffect(() => { if (isSuccess) { if (processedInvoice) { - // console.log("suc resolvedAliases", resolvedAliases); - // console.log("suc unmatched records", processedInvoice?.unmatched); - // console.log("suc matched records", processedInvoice?.form); - let newMatched: typeof processedInvoice.form = []; for (const name in processedInvoice.unmatched) { @@ -124,18 +120,9 @@ export const IdentifyAliasesScreenInvoice = () => { newMatched[record.id] = { price_per_unit, quantity, product_id }; } - // console.log("newM", newMatched); - - dispatch( - documentScannerAction.SET_PROCESSED_INVOICE({ - processedInvoice: { - ...processedInvoice, - form: { ...processedInvoice.form, ...newMatched }, - }, - }) - ); + dispatch(documentScannerAction.SET_NEW_MATCHED({ newMatched })); } - // dispatch(documentScannerAction.RESET_PROCESSED_INVOICE()); + dispatch(documentScannerAction.RESET_PROCESSED_INVOICE()); navigation.goBack(); } }, [isSuccess]);