Skip to content

Commit

Permalink
extend redux by newMatched
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-lipski committed Jul 1, 2024
1 parent 062d077 commit c2c43cc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
23 changes: 23 additions & 0 deletions native/components/StockFormContext/DeliveryFormContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const DeliveryFormContextProvider = ({
const processedInvoice = useAppSelector(
documentScannerSelector.selectProcessedInvoice
);
const newMatched = useAppSelector(documentScannerSelector.selectNewMatched);
const methods = useForm<StockForm>({
defaultValues: { product_records: {}, recipe_records: {} },
});
Expand Down Expand Up @@ -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 <FormProvider {...methods}>{children}</FormProvider>;
};
17 changes: 17 additions & 0 deletions native/redux/documentScannerSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -22,6 +29,7 @@ const initialState: DocumentScannerSlice = {
isCameraReady: null,
photo: null,
processedInvoice: null,
newMatched: {},
processedSalesRaport: null,
inventory_id: null,
} as DocumentScannerSlice;
Expand Down Expand Up @@ -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,
{
Expand Down Expand Up @@ -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) =>
Expand Down
17 changes: 2 additions & 15 deletions native/screens/IdentifyAliasesScreen/Invoice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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]);
Expand Down

0 comments on commit c2c43cc

Please sign in to comment.