From db99cda0b54a4a4aa56d9f2b8e3adfbb2f4423f3 Mon Sep 17 00:00:00 2001 From: NotII <46204250+NotII@users.noreply.github.com> Date: Fri, 28 Feb 2025 12:18:10 +0000 Subject: [PATCH] Update product-modal.tsx --- components/modals/product-modal.tsx | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/components/modals/product-modal.tsx b/components/modals/product-modal.tsx index 860c7f8..0652c5d 100644 --- a/components/modals/product-modal.tsx +++ b/components/modals/product-modal.tsx @@ -50,15 +50,25 @@ export const ProductModal: React.FC = ({ // If productData.image is a *URL* (string), show it as a default preview useEffect(() => { - if (productData.image && typeof productData.image === "string") { + if (productData.image && typeof productData.image === "string" && productData._id) { setImagePreview(`${process.env.NEXT_PUBLIC_API_URL}/products/${productData._id}/image`); + } else if (productData.image && typeof productData.image === "string") { + // Image exists but no ID, this is probably a new product + setImagePreview(null); } - }, [productData.image]); + }, [productData.image, productData._id]); useEffect(() => { setLocalCategories(categories); }, [categories]); + // Reset image state when modal is closed + useEffect(() => { + if (!open) { + setSelectedFile(null); + setImagePreview(null); + } + }, [open]); const handleImageChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0];