diff --git a/frontend/src/pages/Admin/ProductEditPage.jsx b/frontend/src/pages/Admin/ProductEditPage.jsx index f23f853..4e2a27e 100644 --- a/frontend/src/pages/Admin/ProductEditPage.jsx +++ b/frontend/src/pages/Admin/ProductEditPage.jsx @@ -192,9 +192,23 @@ const ProductEditPage = () => { setFormData(prev => ({ ...prev, tags: newTags })); }; + const deduplicateImages = (images) => { + const uniquePaths = new Set(); + return images.filter(img => { + const path = img.path || img; + if (uniquePaths.has(path)) { + return false; + } + uniquePaths.add(path); + return true; + }); + }; + // Handle images change + const handleImagesChange = (newImages) => { - setFormData(prev => ({ ...prev, images: newImages })); + const uniqueImages = deduplicateImages(newImages); + setFormData(prev => ({ ...prev, images: uniqueImages })); }; // Handle notification checkbox change @@ -348,6 +362,25 @@ const ProductEditPage = () => { // Load product data when available useEffect(() => { if (product && !isNewProduct) { + // Process images to avoid duplication + let processedImages = []; + if (product.images && Array.isArray(product.images)) { + // Create a Set to track unique paths + const uniquePaths = new Set(); + processedImages = product.images + .filter(img => { + const path = img.path || ''; + if (!path || uniquePaths.has(path)) return false; + uniquePaths.add(path); + return true; + }) + .map((img, index) => ({ + path: img.path || img, + isPrimary: img.isPrimary || img.is_primary, + displayOrder: img.displayOrder || img.display_order || index + })); + } + setFormData({ name: product.name || '', description: product.description || '', @@ -363,11 +396,7 @@ const ProductEditPage = () => { materialType: product.material_type || '', color: product.color || '', tags: product.tags || [], - images: product.images ? product.images.map(img => ({ - path: img.path || img, - isPrimary: img.isPrimary || img.is_primary, - displayOrder: img.displayOrder || img.display_order || 0 - })) : [] + images: processedImages }); // Load notification settings if available