balls
This commit is contained in:
@@ -16,6 +16,7 @@ import { ProductModal } from "@/components/modals/product-modal";
|
||||
import ProductTable from "@/components/tables/product-table";
|
||||
import { Category } from "@/models/categories";
|
||||
import ImportProductsModal from "@/components/modals/import-products-modal";
|
||||
import { toast } from "sonner";
|
||||
|
||||
export default function ProductsPage() {
|
||||
const router = useRouter();
|
||||
@@ -106,51 +107,76 @@ export default function ProductsPage() {
|
||||
|
||||
|
||||
const handleSaveProduct = async (data: Product, file?: File | null) => {
|
||||
console.log("handleSaveProduct:", data, file);
|
||||
|
||||
const adjustedPricing = data.pricing.map((tier) => ({
|
||||
minQuantity: tier.minQuantity,
|
||||
pricePerUnit:
|
||||
typeof tier.pricePerUnit === "string"
|
||||
? parseFloat(tier.pricePerUnit)
|
||||
: tier.pricePerUnit,
|
||||
}));
|
||||
|
||||
const productToSave: Product = {
|
||||
...data,
|
||||
pricing: adjustedPricing,
|
||||
};
|
||||
|
||||
const authToken = document.cookie
|
||||
.split("; ")
|
||||
.find((row) => row.startsWith("Authorization="))
|
||||
?.split("=")[1];
|
||||
|
||||
if (!authToken) {
|
||||
router.push("/login");
|
||||
return;
|
||||
}
|
||||
|
||||
setLoading(true);
|
||||
|
||||
try {
|
||||
const authToken = document.cookie.split("Authorization=")[1];
|
||||
const apiUrl = editing
|
||||
? `${process.env.NEXT_PUBLIC_API_URL}/products/${data._id}`
|
||||
: `${process.env.NEXT_PUBLIC_API_URL}/products`;
|
||||
|
||||
const url = editing
|
||||
? `/api/products/${data._id}`
|
||||
: "/api/products";
|
||||
|
||||
// Save product data
|
||||
const savedProduct = await saveProductData(
|
||||
apiUrl,
|
||||
productToSave,
|
||||
url,
|
||||
{
|
||||
name: data.name,
|
||||
description: data.description,
|
||||
unitType: data.unitType,
|
||||
category: data.category,
|
||||
pricing: data.pricing,
|
||||
stockTracking: data.stockTracking,
|
||||
currentStock: data.currentStock,
|
||||
lowStockThreshold: data.lowStockThreshold
|
||||
},
|
||||
authToken,
|
||||
editing ? "PUT" : "POST"
|
||||
);
|
||||
|
||||
|
||||
if (file) {
|
||||
await saveProductImage(`${process.env.NEXT_PUBLIC_API_URL}/products/${savedProduct._id}/image`, file, authToken);
|
||||
await saveProductImage(
|
||||
`/api/products/${savedProduct._id}/image`,
|
||||
file,
|
||||
authToken
|
||||
);
|
||||
}
|
||||
|
||||
setProducts((prevProducts) => {
|
||||
if (editing) {
|
||||
return prevProducts.map((product) =>
|
||||
product._id === savedProduct._id ? savedProduct : product
|
||||
);
|
||||
} else {
|
||||
return [...prevProducts, savedProduct];
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// If editing and stock values were updated, update stock in the dedicated endpoint
|
||||
if (editing && data.stockTracking !== undefined) {
|
||||
await saveProductData(
|
||||
`/api/stock/${data._id}`,
|
||||
{
|
||||
stockTracking: data.stockTracking,
|
||||
currentStock: data.currentStock || 0,
|
||||
lowStockThreshold: data.lowStockThreshold || 10
|
||||
},
|
||||
authToken,
|
||||
"PUT"
|
||||
);
|
||||
}
|
||||
|
||||
// Refresh products list
|
||||
const fetchedProducts = await fetchProductData("/api/products", authToken);
|
||||
setProducts(fetchedProducts);
|
||||
|
||||
setModalOpen(false);
|
||||
setLoading(false);
|
||||
|
||||
toast.success(
|
||||
editing ? "Product updated successfully" : "Product added successfully"
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error saving product:", error);
|
||||
console.error(error);
|
||||
setLoading(false);
|
||||
toast.error("Failed to save product");
|
||||
}
|
||||
};
|
||||
|
||||
@@ -194,11 +220,14 @@ export default function ProductsPage() {
|
||||
description: "",
|
||||
unitType: "pcs",
|
||||
category: "",
|
||||
stockTracking: true,
|
||||
currentStock: 0,
|
||||
lowStockThreshold: 10,
|
||||
pricing: [{ minQuantity: 1, pricePerUnit: 0 }],
|
||||
image: null,
|
||||
});
|
||||
setEditing(false);
|
||||
setAddProductOpen(true);
|
||||
setModalOpen(true);
|
||||
};
|
||||
|
||||
// Get category name by ID
|
||||
|
||||
Reference in New Issue
Block a user