This commit is contained in:
g
2025-02-09 02:15:26 +00:00
parent 7ec17cfd73
commit 72ea64df15
2 changed files with 30 additions and 16 deletions

View File

@@ -6,7 +6,10 @@ import { Trash, PlusCircle } from "lucide-react";
interface PricingTiersProps { interface PricingTiersProps {
pricing: any[]; pricing: any[];
handleTierChange: (e: React.ChangeEvent<HTMLInputElement>, index: number) => void; handleTierChange: (
e: React.ChangeEvent<HTMLInputElement>,
index: number
) => void;
handleRemoveTier: (index: number) => void; handleRemoveTier: (index: number) => void;
handleAddTier: () => void; handleAddTier: () => void;
} }
@@ -26,17 +29,17 @@ export const PricingTiers = ({
<Input <Input
name="minQuantity" name="minQuantity"
type="number" type="number"
min="1"
placeholder="Min Quantity" placeholder="Min Quantity"
value={tier.minQuantity} value={tier.minQuantity === 0 ? "" : tier.minQuantity} // ✅ Show empty string when 0
onChange={(e) => handleTierChange(e, index)} onChange={(e) => handleTierChange(e, index)}
className="h-8 text-sm px-2 flex-1" className="h-8 text-sm px-2 flex-1"
/> />
<Input <Input
name="pricePerUnit" name="pricePerUnit"
type="number" type="number"
placeholder="Price per unit" placeholder="Price per unit"
value={tier.pricePerUnit} value={tier.pricePerUnit === 0 ? "" : tier.pricePerUnit} // ✅ Show empty string when 0
onChange={(e) => handleTierChange(e, index)} onChange={(e) => handleTierChange(e, index)}
className="h-8 text-sm px-2 flex-1" className="h-8 text-sm px-2 flex-1"
/> />
@@ -64,4 +67,4 @@ export const PricingTiers = ({
Add Tier Add Tier
</Button> </Button>
</div> </div>
); );

View File

@@ -35,8 +35,6 @@ export const ProductModal: React.FC<ProductModalProps> = ({
categories, categories,
editing, editing,
handleChange, handleChange,
handleTieredPricingChange,
handleAddTier,
handleRemoveTier, handleRemoveTier,
setProductData, setProductData,
}) => { }) => {
@@ -59,9 +57,7 @@ export const ProductModal: React.FC<ProductModalProps> = ({
setLocalCategories(categories); setLocalCategories(categories);
}, [categories]); }, [categories]);
/**
* 2) When user selects a file, store it and create an objectURL for preview
*/
const handleImageChange = (e: React.ChangeEvent<HTMLInputElement>) => { const handleImageChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0]; const file = e.target.files?.[0];
if (!file) { if (!file) {
@@ -72,12 +68,10 @@ export const ProductModal: React.FC<ProductModalProps> = ({
} }
// For preview // For preview
console.log(file)
const objectUrl = URL.createObjectURL(file); const objectUrl = URL.createObjectURL(file);
setSelectedFile(file); setSelectedFile(file);
setImagePreview(objectUrl); setImagePreview(objectUrl);
// Optionally, load the image to calculate dimensions
const image = new Image(); const image = new Image();
image.onload = () => { image.onload = () => {
const aspectRatio = image.naturalWidth / image.naturalHeight; const aspectRatio = image.naturalWidth / image.naturalHeight;
@@ -88,9 +82,13 @@ export const ProductModal: React.FC<ProductModalProps> = ({
image.src = objectUrl; image.src = objectUrl;
}; };
/** const handleAddTier = () => {
* 3) On 'Save', call the parent onSave(productData, selectedFile) setProductData((prev) => ({
*/ ...prev,
pricing: [...prev.pricing, { minQuantity: 0, pricePerUnit: 0 }],
}));
};
const handleSave = async () => { const handleSave = async () => {
if (!productData.category) { if (!productData.category) {
toast.error("Please select or add a category"); toast.error("Please select or add a category");
@@ -107,6 +105,19 @@ export const ProductModal: React.FC<ProductModalProps> = ({
setLocalCategories((prev) => [...prev, newCategory]); setLocalCategories((prev) => [...prev, newCategory]);
}; };
const handleTierChange = (e: React.ChangeEvent<HTMLInputElement>, 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 ( return (
<Dialog open={open} onOpenChange={onClose}> <Dialog open={open} onOpenChange={onClose}>
<DialogContent className="max-w-6xl"> <DialogContent className="max-w-6xl">
@@ -133,7 +144,7 @@ export const ProductModal: React.FC<ProductModalProps> = ({
/> />
<PricingTiers <PricingTiers
pricing={productData.pricing} pricing={productData.pricing}
handleTierChange={handleTieredPricingChange} handleTierChange={handleTierChange}
handleRemoveTier={handleRemoveTier} handleRemoveTier={handleRemoveTier}
handleAddTier={handleAddTier} handleAddTier={handleAddTier}
/> />