next-enterprise/package/components/organisms/Checkout/ControlPanel.tsx

94 lines
2.8 KiB
TypeScript
Raw Normal View History

"use client";
import Grid2 from '@mui/material/Grid2';
2024-10-01 13:45:34 -05:00
import { useAtom } from 'jotai';
import React, { useState } from 'react';
import { storeNumberAtom, storePosAtom } from '@atoms/states/StoreAtom';
import { AddProductsOutputInterface } from '@interfaces/OutputInterfaces/Sale/AddProductsOutputInterfae';
import { CustomerInterface } from '@interfaces/ResponseInterfaces/Customer/CustomerResponseInterface';
import { AddProductsResponseInterface, ItemObject } from '@interfaces/ResponseInterfaces/Sale/AddProductsResponseInterface';
import { StartSaleResponseInterface } from '@interfaces/ResponseInterfaces/Sale/StartSaleResponseInterface';
import ActionButtons from '@molecules/ActionButtons';
import ProductSearch from '@molecules/ProductSearch';
import SaleSummary from '@molecules/SaleSummary';
2024-10-01 13:45:34 -05:00
import AddProducts from "@package/config/api/Sale/Transaction/AddProducts/page";
interface ControlPanelProps {
onVoidOrder: () => void;
onVoidLast: () => void;
onResume: () => void;
2024-10-01 13:45:34 -05:00
transaction: StartSaleResponseInterface;
client: CustomerInterface;
onProductAdded: () => void;
}
const ControlPanel: React.FC<ControlPanelProps> = ({
onVoidOrder,
onVoidLast,
onResume,
2024-10-01 13:45:34 -05:00
transaction,
client,
onProductAdded,
}) => {
2024-10-01 13:45:34 -05:00
const [addedProducts, setAddedProducts] = useState<ItemObject>({} as ItemObject);
const [loading, setLoading] = useState(false);
const [storeNumber] = useAtom(storeNumberAtom);
const [storePos] = useAtom(storePosAtom);
const handleSearchSKU = async (sku: string) => {
setLoading(true);
try {
const addProductRequest: AddProductsOutputInterface = {
idTransaction: transaction.object?.id,
storeNumber: storeNumber,
idSubsidiary: client.idSubsidiary,
clientNit: client.dni,
idUser: client.idUser,
localDate: new Date(),
pos: storePos,
products: [
{
sku: sku,
value: 1,
},
],
};
const productResponse = await AddProducts(addProductRequest) as AddProductsResponseInterface;
if (productResponse && productResponse.correct) {
const newProduct = productResponse.object;
setAddedProducts(newProduct)
onProductAdded();
} else {
console.error("Error adding product: ", productResponse?.message);
}
} catch (error) {
console.error("Error during AddProducts API call", error);
} finally {
setLoading(false);
}
};
return (
<Grid2 container spacing={4}>
<Grid2 size={{xs:12}}>
<ActionButtons onVoidOrder={onVoidOrder} onVoidLast={onVoidLast} onResume={onResume} />
</Grid2>
<Grid2 size={{xs:12}}>
2024-10-01 13:45:34 -05:00
<ProductSearch onSearch={handleSearchSKU} />
</Grid2>
<Grid2 size={{xs:12}}>
2024-10-01 13:45:34 -05:00
<SaleSummary product={addedProducts} />
</Grid2>
</Grid2>
);
};
export default ControlPanel;