Cleanup
This commit is contained in:
@@ -28,7 +28,7 @@ export default function ProductsPage() {
|
||||
unitType: "pcs",
|
||||
category: "",
|
||||
pricing: [{ minQuantity: 1, pricePerUnit: 0 }],
|
||||
image: null
|
||||
image: null,
|
||||
});
|
||||
|
||||
// Fetch products and categories
|
||||
@@ -55,15 +55,15 @@ export default function ProductsPage() {
|
||||
authToken
|
||||
),
|
||||
]);
|
||||
|
||||
|
||||
console.log("Fetched Products:", fetchedProducts);
|
||||
|
||||
|
||||
// Ensure all products have tieredPricing
|
||||
const processedProducts = fetchedProducts.map((product: Product) => ({
|
||||
...product,
|
||||
pricing: product.pricing || [{ minQuantity: 1, pricePerUnit: 0 }],
|
||||
}));
|
||||
|
||||
|
||||
setProducts(processedProducts);
|
||||
setCategories(fetchedCategories);
|
||||
} catch (error) {
|
||||
@@ -75,6 +75,20 @@ export default function ProductsPage() {
|
||||
fetchDataAsync();
|
||||
}, []);
|
||||
|
||||
const handleAddTier = () => {
|
||||
setProductData((prev) => ({
|
||||
...prev,
|
||||
pricing: [...prev.pricing, { minQuantity: 1, pricePerUnit: 0 }],
|
||||
}));
|
||||
};
|
||||
|
||||
const handleRemoveTier = (index: number) => {
|
||||
setProductData((prev) => ({
|
||||
...prev,
|
||||
pricing: prev.pricing.filter((_, i) => i !== index),
|
||||
}));
|
||||
};
|
||||
|
||||
// Handle input changes
|
||||
const handleChange = (
|
||||
e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
|
||||
@@ -100,26 +114,26 @@ export default function ProductsPage() {
|
||||
? parseFloat(tier.pricePerUnit)
|
||||
: tier.pricePerUnit,
|
||||
}));
|
||||
|
||||
|
||||
const productToSave: Product = {
|
||||
...data,
|
||||
pricing: adjustedPricing,
|
||||
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,7 +143,7 @@ export default function ProductsPage() {
|
||||
return [...prevProducts, savedProduct];
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
setModalOpen(false);
|
||||
} catch (error) {
|
||||
console.error("Error saving product:", error);
|
||||
@@ -159,9 +173,9 @@ export default function ProductsPage() {
|
||||
setProductData({
|
||||
...product,
|
||||
pricing: product.pricing
|
||||
? product.pricing.map(tier => ({
|
||||
? product.pricing.map((tier) => ({
|
||||
minQuantity: tier.minQuantity,
|
||||
pricePerUnit: tier.pricePerUnit
|
||||
pricePerUnit: tier.pricePerUnit,
|
||||
}))
|
||||
: [{ minQuantity: 1, pricePerUnit: 0 }], // Fallback if undefined
|
||||
});
|
||||
@@ -219,6 +233,8 @@ export default function ProductsPage() {
|
||||
editing={editing}
|
||||
handleChange={handleChange}
|
||||
handleTieredPricingChange={handleTieredPricingChange}
|
||||
handleAddTier={handleAddTier} // ✅ Ensure this is passed
|
||||
handleRemoveTier={handleRemoveTier} // ✅ Ensure this is passed
|
||||
setProductData={setProductData}
|
||||
/>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user