Skip to content

Commit

Permalink
app: fix-new-matched-aliases-population (#233)
Browse files Browse the repository at this point in the history
* populate after isSuccess

* extend redux by newMatched

---------

Co-authored-by: Michal Struck <[email protected]>
  • Loading branch information
felix-lipski and michalstruck authored Jul 1, 2024
1 parent dd09ee9 commit 8ec8114
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 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>;
};
6 changes: 6 additions & 0 deletions native/db/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ export type ProcessInvoiceResponse = {
quantity: number;
};
};
unmatched: {
[name: string]: {
price_per_unit: number;
quantity: number;
};
};
unmatchedAliases: string[];
} | null;

Expand Down
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
36 changes: 34 additions & 2 deletions native/screens/IdentifyAliasesScreen/Invoice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { IDListCardAddProduct } from "../../components/IDListCardAddProduct";
import { IndexBadge } from "../../components/IndexBadge";
import { useSnackbar } from "../../components/Snackbar/hooks";
import { Typography } from "../../components/Typography";
import { useListProductRecords } from "../../db";
import { useCreateProductNameAlias } from "../../db/hooks/useCreateProductNameAlias";
import { useListExistingProducts } from "../../db/hooks/useListProducts";
import { IdentifyAliasesScreenNavigationProp } from "../../navigation/types";
Expand Down Expand Up @@ -68,14 +69,24 @@ export const IdentifyAliasesScreenInvoice = () => {
const { openBottomSheet, closeBottomSheet } = useBottomSheet();
const { showInfo } = useSnackbar();
const { data: products } = useListExistingProducts();
const { mutate, isSuccess } = useCreateProductNameAlias();
const {
mutate,
isSuccess,
data: resolvedAliases,
} = useCreateProductNameAlias();

const dispatch = useAppDispatch();
const inventoryId = useAppSelector(documentScannerSelector.selectInventoryId);
const aliases = useAppSelector(
documentScannerSelector.selectInvoiceUnmatchedAliases
);

const processedInvoice = useAppSelector(
documentScannerSelector.selectProcessedInvoice
);

const { data: productRecords } = useListProductRecords(inventoryId as number);

const { setValue, handleSubmit, watch, getValues } = useForm<AliasForm>({
defaultValues: async () =>
!!products
Expand All @@ -92,16 +103,37 @@ export const IdentifyAliasesScreenInvoice = () => {
const usedAliases = watch("usedAliases");
useEffect(() => {
if (isSuccess) {
if (processedInvoice) {
let newMatched: typeof processedInvoice.form = [];

for (const name in processedInvoice.unmatched) {
const { price_per_unit, quantity } = processedInvoice.unmatched[name];
const alias = resolvedAliases?.find((alias) => alias.alias === name);
if (!alias || !alias.product_id) continue;
const { product_id } = alias;

const record = productRecords?.find(
(r) => r.product_id === product_id
);
if (!record || !record.id) continue;

newMatched[record.id] = { price_per_unit, quantity, product_id };
}

dispatch(documentScannerAction.SET_NEW_MATCHED({ newMatched }));
}
dispatch(documentScannerAction.RESET_PROCESSED_INVOICE());
navigation.goBack();
}
}, [isSuccess]);

const handleSavePress = () => {
handleSubmit(
(data) => {
// New alisases are inserted into the db here
mutate(data);
dispatch(documentScannerAction.PHOTO_RESET_DATA());
dispatch(documentScannerAction.RESET_PROCESSED_INVOICE());
// dispatch(documentScannerAction.RESET_PROCESSED_INVOICE());
dispatch(documentScannerAction.PHOTO_RETAKE());
},
(_errors) => {
Expand Down

0 comments on commit 8ec8114

Please sign in to comment.