diff --git a/ipi-survey-platform/src/pages/Admin/configuration/EditCompanyProfile.jsx b/ipi-survey-platform/src/pages/Admin/configuration/EditCompanyProfile.jsx index c0c9b34..32b1439 100644 --- a/ipi-survey-platform/src/pages/Admin/configuration/EditCompanyProfile.jsx +++ b/ipi-survey-platform/src/pages/Admin/configuration/EditCompanyProfile.jsx @@ -410,31 +410,35 @@ const EditCompanyProfile = () => { // Handle adding a product const handleAddProduct = (product) => { - setSelectedProducts(prev => { - // Check if product is already selected - const isAlreadySelected = prev.some(p => - (p.value && p.value === product.value) || - (p.id && p.id === product.id) || - (p.product_id && p.product_id === product.product_id) - ); - - if (isAlreadySelected) { - return prev; // Don't add duplicate - } - - // Count current newly added products (excluding original products) - const currentNewlyAdded = prev.filter(p => newlyAddedProductIds.has(p.value || p.id || p.product_id)); - - // Allow up to 4 newly added products - if (currentNewlyAdded.length >= 4) { - alert('Maximum of 4 replacement products can be added.'); - return prev; - } - - return [...prev, { ...product, isExisting: false }]; - }); + // Check if product is already selected + const isAlreadySelected = selectedProducts.some(p => + (p.value && p.value === product.value) || + (p.id && p.id === product.id) || + (p.product_id && p.product_id === product.product_id) + ); - // Remove from available products + if (isAlreadySelected) { + // Check if the product is in selectedProducts but unchecked + const productId = product.value || product.id || product.product_id; + if (!checkedProducts.has(productId)) { + alert('This product is already in your selected products but is unchecked. Please check the box next to the product in the "Selected Products" panel to include it.'); + } + return; // Don't add duplicate, don't modify available products + } + + // Count current newly added products (excluding original products) + const currentNewlyAdded = selectedProducts.filter(p => newlyAddedProductIds.has(p.value || p.id || p.product_id)); + + // Allow up to 4 newly added products + if (currentNewlyAdded.length >= 4) { + alert('Maximum of 4 replacement products can be added.'); + return; // Don't add product, don't modify available products + } + + // Only proceed with adding the product if we pass all checks + setSelectedProducts(prev => [...prev, { ...product, isExisting: false }]); + + // Remove from available products (only if product was actually added) setAvailableProducts(prev => prev.filter(p => !(p.value && p.value === product.value) &&