fixed image duping

This commit is contained in:
2ManyProjects 2025-04-28 10:45:46 -05:00
parent 119e9adfec
commit 56bb9b3b4a

View file

@ -192,9 +192,23 @@ const ProductEditPage = () => {
setFormData(prev => ({ ...prev, tags: newTags })); 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 // Handle images change
const handleImagesChange = (newImages) => { const handleImagesChange = (newImages) => {
setFormData(prev => ({ ...prev, images: newImages })); const uniqueImages = deduplicateImages(newImages);
setFormData(prev => ({ ...prev, images: uniqueImages }));
}; };
// Handle notification checkbox change // Handle notification checkbox change
@ -348,6 +362,25 @@ const ProductEditPage = () => {
// Load product data when available // Load product data when available
useEffect(() => { useEffect(() => {
if (product && !isNewProduct) { 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({ setFormData({
name: product.name || '', name: product.name || '',
description: product.description || '', description: product.description || '',
@ -363,11 +396,7 @@ const ProductEditPage = () => {
materialType: product.material_type || '', materialType: product.material_type || '',
color: product.color || '', color: product.color || '',
tags: product.tags || [], tags: product.tags || [],
images: product.images ? product.images.map(img => ({ images: processedImages
path: img.path || img,
isPrimary: img.isPrimary || img.is_primary,
displayOrder: img.displayOrder || img.display_order || 0
})) : []
}); });
// Load notification settings if available // Load notification settings if available