This commit is contained in:
g
2025-02-08 00:31:53 +00:00
parent e75af20cff
commit 30fb2aaaab
3 changed files with 32 additions and 36 deletions

View File

@@ -27,8 +27,8 @@ export default function ProductsPage() {
description: "", description: "",
unitType: "pcs", unitType: "pcs",
category: "", category: "",
tieredPricing: [{ minQuantity: 1, pricePerUnit: 0 }], pricing: [{ minQuantity: 1, pricePerUnit: 0 }],
image: null, image: null
}); });
// Fetch products and categories // Fetch products and categories
@@ -61,7 +61,7 @@ export default function ProductsPage() {
// Ensure all products have tieredPricing // Ensure all products have tieredPricing
const processedProducts = fetchedProducts.map((product: Product) => ({ const processedProducts = fetchedProducts.map((product: Product) => ({
...product, ...product,
tieredPricing: product.tieredPricing || [{ minQuantity: 1, pricePerUnit: 0 }], pricing: product.pricing || [{ minQuantity: 1, pricePerUnit: 0 }],
})); }));
setProducts(processedProducts); setProducts(processedProducts);
@@ -85,26 +85,26 @@ export default function ProductsPage() {
e: ChangeEvent<HTMLInputElement>, e: ChangeEvent<HTMLInputElement>,
index: number index: number
) => { ) => {
const updatedPricing = [...productData.tieredPricing]; const updatedPricing = [...productData.pricing];
const name = e.target.name as "minQuantity" | "pricePerUnit"; const name = e.target.name as "minQuantity" | "pricePerUnit";
updatedPricing[index][name] = e.target.valueAsNumber || 0; updatedPricing[index][name] = e.target.valueAsNumber || 0;
setProductData({ ...productData, tieredPricing: updatedPricing }); setProductData({ ...productData, pricing: updatedPricing });
}; };
// Save product data after modal form submission // Save product data after modal form submission
const handleSaveProduct = async (data: Product) => { const handleSaveProduct = async (data: Product) => {
const adjustedPricing = data.tieredPricing.map((tier) => ({ const adjustedPricing = data.pricing.map((tier) => ({
minQuantity: tier.minQuantity, minQuantity: tier.minQuantity,
pricePerUnit: pricePerUnit:
typeof tier.pricePerUnit === "string" typeof tier.pricePerUnit === "string"
? parseFloat(tier.pricePerUnit) // Convert string to number ? parseFloat(tier.pricePerUnit)
: tier.pricePerUnit, : tier.pricePerUnit,
})); }));
const productToSave = { const productToSave: Product = {
...data, ...data,
pricing: adjustedPricing, pricing: adjustedPricing,
imageBase64: imagePreview || "", image: data.image ?? "", // ✅ Prevents undefined error
}; };
try { try {
@@ -130,7 +130,7 @@ export default function ProductsPage() {
} }
}); });
setModalOpen(false); // Close modal after saving setModalOpen(false);
} catch (error) { } catch (error) {
console.error("Error saving product:", error); console.error("Error saving product:", error);
} }
@@ -158,8 +158,8 @@ export default function ProductsPage() {
const handleEditProduct = (product: Product) => { const handleEditProduct = (product: Product) => {
setProductData({ setProductData({
...product, ...product,
tieredPricing: product.tieredPricing pricing: product.pricing
? product.tieredPricing.map(tier => ({ ? product.pricing.map(tier => ({
minQuantity: tier.minQuantity, minQuantity: tier.minQuantity,
pricePerUnit: tier.pricePerUnit pricePerUnit: tier.pricePerUnit
})) }))
@@ -176,7 +176,7 @@ export default function ProductsPage() {
description: "", description: "",
unitType: "pcs", unitType: "pcs",
category: "", category: "",
tieredPricing: [{ minQuantity: 1, pricePerUnit: 0 }], pricing: [{ minQuantity: 1, pricePerUnit: 0 }],
image: null, image: null,
}); });
setEditing(false); setEditing(false);

View File

@@ -18,9 +18,9 @@ import {
SelectTrigger, SelectTrigger,
SelectValue, SelectValue,
} from "@/components/ui/select"; } from "@/components/ui/select";
import { Product } from "@/models/products";
import { Trash, PlusCircle } from "lucide-react"; import { Trash, PlusCircle } from "lucide-react";
import { toast } from "sonner"; import { toast } from "sonner";
import { Product } from "@/models/products";
interface Category { interface Category {
_id: string; _id: string;
@@ -39,7 +39,7 @@ interface ProductData {
unitType: string; unitType: string;
category: string; category: string;
pricing: PricingTier[]; pricing: PricingTier[];
image: string | File | null; image?: string | File | null | undefined;
} }
interface ProductModalProps { interface ProductModalProps {
@@ -47,11 +47,10 @@ interface ProductModalProps {
onClose: () => void; onClose: () => void;
onSave: (productData: ProductData) => void; onSave: (productData: ProductData) => void;
productData: ProductData; productData: ProductData;
categories: Category[]; categories: any[];
editing: boolean; editing: boolean;
handleChange: ( handleChange: (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement> handleTieredPricingChange: (e: ChangeEvent<HTMLInputElement>, index: number) => void; // ✅ Added this
) => void;
setProductData: React.Dispatch<React.SetStateAction<ProductData>>; setProductData: React.Dispatch<React.SetStateAction<ProductData>>;
} }
@@ -72,15 +71,6 @@ export const ProductModal = ({
height: 200, height: 200,
}); });
useEffect(() => {
if (productData?.pricing) {
setProductData((prev) => ({
...prev,
tieredPricing: productData.pricing,
}));
}
}, [productData.pricing]);
useEffect(() => { useEffect(() => {
if (productData.image && typeof productData.image === "string") { if (productData.image && typeof productData.image === "string") {
setImagePreview(productData.image); setImagePreview(productData.image);
@@ -137,13 +127,19 @@ export const ProductModal = ({
updatedPricing[index] = { updatedPricing[index] = {
...updatedPricing[index], ...updatedPricing[index],
[name]: isNaN(valueAsNumber) ? 0 : valueAsNumber, // Ensure valid numbers [name]: isNaN(valueAsNumber) ? 0 : valueAsNumber,
}; };
setProductData((prev) => ({ setProductData((prev) => ({
...prev, ...prev,
tieredPricing: updatedPricing, tieredPricing: updatedPricing,
image: prev.image ?? null,
})); }));
const convertToProductData = (product: Product): ProductData => ({
...product,
image: product.image ?? null, // ✅ Ensures the type is correct
});
}; };
return ( return (

View File

@@ -4,9 +4,9 @@ export interface Product {
description: string; description: string;
unitType: string; unitType: string;
category: string; category: string;
tieredPricing: Array<{ pricing: Array<{
minQuantity: number; minQuantity: number;
pricePerUnit: number; pricePerUnit: number;
}>; }>;
image?: string | File | null; image?: string | File | null | undefined;
} }