From 72ea64df1553d70a1c3f50011ba0820b70002567 Mon Sep 17 00:00:00 2001 From: g Date: Sun, 9 Feb 2025 02:15:26 +0000 Subject: [PATCH] weewoo --- components/forms/pricing-tiers.tsx | 13 +++++++----- components/modals/product-modal.tsx | 33 +++++++++++++++++++---------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/components/forms/pricing-tiers.tsx b/components/forms/pricing-tiers.tsx index e1a7946..b7d4d83 100644 --- a/components/forms/pricing-tiers.tsx +++ b/components/forms/pricing-tiers.tsx @@ -6,7 +6,10 @@ import { Trash, PlusCircle } from "lucide-react"; interface PricingTiersProps { pricing: any[]; - handleTierChange: (e: React.ChangeEvent, index: number) => void; + handleTierChange: ( + e: React.ChangeEvent, + index: number + ) => void; handleRemoveTier: (index: number) => void; handleAddTier: () => void; } @@ -26,17 +29,17 @@ export const PricingTiers = ({ handleTierChange(e, index)} className="h-8 text-sm px-2 flex-1" /> + handleTierChange(e, index)} className="h-8 text-sm px-2 flex-1" /> @@ -64,4 +67,4 @@ export const PricingTiers = ({ Add Tier -); \ No newline at end of file +); diff --git a/components/modals/product-modal.tsx b/components/modals/product-modal.tsx index 16fff29..e99a853 100644 --- a/components/modals/product-modal.tsx +++ b/components/modals/product-modal.tsx @@ -35,8 +35,6 @@ export const ProductModal: React.FC = ({ categories, editing, handleChange, - handleTieredPricingChange, - handleAddTier, handleRemoveTier, setProductData, }) => { @@ -59,9 +57,7 @@ export const ProductModal: React.FC = ({ setLocalCategories(categories); }, [categories]); - /** - * 2) When user selects a file, store it and create an objectURL for preview - */ + const handleImageChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) { @@ -72,12 +68,10 @@ export const ProductModal: React.FC = ({ } // For preview - console.log(file) const objectUrl = URL.createObjectURL(file); setSelectedFile(file); setImagePreview(objectUrl); - // Optionally, load the image to calculate dimensions const image = new Image(); image.onload = () => { const aspectRatio = image.naturalWidth / image.naturalHeight; @@ -88,9 +82,13 @@ export const ProductModal: React.FC = ({ image.src = objectUrl; }; - /** - * 3) On 'Save', call the parent onSave(productData, selectedFile) - */ + const handleAddTier = () => { + setProductData((prev) => ({ + ...prev, + pricing: [...prev.pricing, { minQuantity: 0, pricePerUnit: 0 }], + })); + }; + const handleSave = async () => { if (!productData.category) { toast.error("Please select or add a category"); @@ -107,6 +105,19 @@ export const ProductModal: React.FC = ({ setLocalCategories((prev) => [...prev, newCategory]); }; + const handleTierChange = (e: React.ChangeEvent, index: number) => { + const { name, value } = e.target; + + setProductData((prev) => ({ + ...prev, + pricing: prev.pricing.map((tier, i) => + i === index + ? { ...tier, [name]: value === "" ? 0 : Number(value) } + : tier + ), + })); + }; + return ( @@ -133,7 +144,7 @@ export const ProductModal: React.FC = ({ />