Fix
This commit is contained in:
@@ -27,8 +27,8 @@ export default function ProductsPage() {
|
||||
description: "",
|
||||
unitType: "pcs",
|
||||
category: "",
|
||||
tieredPricing: [{ minQuantity: 1, pricePerUnit: 0 }],
|
||||
image: null,
|
||||
pricing: [{ minQuantity: 1, pricePerUnit: 0 }],
|
||||
image: null
|
||||
});
|
||||
|
||||
// Fetch products and categories
|
||||
@@ -61,7 +61,7 @@ export default function ProductsPage() {
|
||||
// Ensure all products have tieredPricing
|
||||
const processedProducts = fetchedProducts.map((product: Product) => ({
|
||||
...product,
|
||||
tieredPricing: product.tieredPricing || [{ minQuantity: 1, pricePerUnit: 0 }],
|
||||
pricing: product.pricing || [{ minQuantity: 1, pricePerUnit: 0 }],
|
||||
}));
|
||||
|
||||
setProducts(processedProducts);
|
||||
@@ -85,41 +85,41 @@ export default function ProductsPage() {
|
||||
e: ChangeEvent<HTMLInputElement>,
|
||||
index: number
|
||||
) => {
|
||||
const updatedPricing = [...productData.tieredPricing];
|
||||
const updatedPricing = [...productData.pricing];
|
||||
const name = e.target.name as "minQuantity" | "pricePerUnit";
|
||||
updatedPricing[index][name] = e.target.valueAsNumber || 0;
|
||||
setProductData({ ...productData, tieredPricing: updatedPricing });
|
||||
setProductData({ ...productData, pricing: updatedPricing });
|
||||
};
|
||||
|
||||
// Save product data after modal form submission
|
||||
const handleSaveProduct = async (data: Product) => {
|
||||
const adjustedPricing = data.tieredPricing.map((tier) => ({
|
||||
const adjustedPricing = data.pricing.map((tier) => ({
|
||||
minQuantity: tier.minQuantity,
|
||||
pricePerUnit:
|
||||
typeof tier.pricePerUnit === "string"
|
||||
? parseFloat(tier.pricePerUnit) // Convert string to number
|
||||
? parseFloat(tier.pricePerUnit)
|
||||
: tier.pricePerUnit,
|
||||
}));
|
||||
|
||||
const productToSave = {
|
||||
|
||||
const productToSave: Product = {
|
||||
...data,
|
||||
pricing: adjustedPricing,
|
||||
imageBase64: imagePreview || "",
|
||||
image: data.image ?? "", // ✅ Prevents undefined error
|
||||
};
|
||||
|
||||
|
||||
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 savedProduct = await saveProductData(
|
||||
apiUrl,
|
||||
productToSave,
|
||||
authToken,
|
||||
editing ? "PUT" : "POST"
|
||||
);
|
||||
|
||||
|
||||
setProducts((prevProducts) => {
|
||||
if (editing) {
|
||||
return prevProducts.map((product) =>
|
||||
@@ -129,8 +129,8 @@ export default function ProductsPage() {
|
||||
return [...prevProducts, savedProduct];
|
||||
}
|
||||
});
|
||||
|
||||
setModalOpen(false); // Close modal after saving
|
||||
|
||||
setModalOpen(false);
|
||||
} catch (error) {
|
||||
console.error("Error saving product:", error);
|
||||
}
|
||||
@@ -158,8 +158,8 @@ export default function ProductsPage() {
|
||||
const handleEditProduct = (product: Product) => {
|
||||
setProductData({
|
||||
...product,
|
||||
tieredPricing: product.tieredPricing
|
||||
? product.tieredPricing.map(tier => ({
|
||||
pricing: product.pricing
|
||||
? product.pricing.map(tier => ({
|
||||
minQuantity: tier.minQuantity,
|
||||
pricePerUnit: tier.pricePerUnit
|
||||
}))
|
||||
@@ -176,7 +176,7 @@ export default function ProductsPage() {
|
||||
description: "",
|
||||
unitType: "pcs",
|
||||
category: "",
|
||||
tieredPricing: [{ minQuantity: 1, pricePerUnit: 0 }],
|
||||
pricing: [{ minQuantity: 1, pricePerUnit: 0 }],
|
||||
image: null,
|
||||
});
|
||||
setEditing(false);
|
||||
|
||||
Reference in New Issue
Block a user