Update product-modal.tsx

This commit is contained in:
NotII
2025-02-28 12:18:10 +00:00
parent fd31b461ce
commit db99cda0b5

View File

@@ -50,15 +50,25 @@ export const ProductModal: React.FC<ProductModalProps> = ({
// 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<HTMLInputElement>) => {
const file = e.target.files?.[0];