fixed image duping
This commit is contained in:
parent
119e9adfec
commit
56bb9b3b4a
1 changed files with 35 additions and 6 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue