From 8b89cc99ca43e039dc1d85e9a58e3d52af7a556c Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Sat, 29 Nov 2025 15:13:15 +0330 Subject: [PATCH] variant --- .../product/components/BaseInformation.tsx | 246 +++++++++++++----- src/components/CategoryModal.tsx | 4 +- src/share/components/Menu.tsx | 2 +- src/types/product.types.ts | 24 +- 4 files changed, 203 insertions(+), 73 deletions(-) diff --git a/src/app/product/components/BaseInformation.tsx b/src/app/product/components/BaseInformation.tsx index 72d2c90..aae2a53 100644 --- a/src/app/product/components/BaseInformation.tsx +++ b/src/app/product/components/BaseInformation.tsx @@ -75,77 +75,193 @@ const BaseInformation: FC = ({ product, stats }) => { {/* نمایش variant ها */} -
- {/* متراژ */} -
- {product.variants.map((variant) => { - if (variant.meterage) { - const isSelected = variantId === variant._id - return ( - - ) - } - return null - })} -
+ {product.variants.length > 0 && ( +
+ {/* بررسی وجود فیلدهای خاص در variant ها */} + {(() => { + const hasMeterage = product.variants.some(v => v.meterage) + const hasColor = product.variants.some(v => v.color) + const hasSize = product.variants.some(v => v.size) + const hasThemeValue = product.variants.some(v => v.themeValue) - {/* رنگ ها */} -
- {product.variants.map((variant) => { - if (variant.color) { - const isSelected = variantId === variant._id + // اگر هیچ فیلد خاصی وجود نداشت، نمایش عمومی + if (!hasMeterage && !hasColor && !hasSize && !hasThemeValue) { return ( - +
+ {product.variants.map((variant, index) => { + const isSelected = variantId === variant._id + return ( + + ) + })} +
) } - return null - })} -
- {/* سایزها */} -
- {product.variants.map((variant) => { - if (variant.size) { - const isSelected = variantId === variant._id - return ( - - ) - } - return null - })} + // نمایش فیلدهای خاص + return ( + <> + {/* متراژ */} + {hasMeterage && ( +
+ {product.variants.map((variant) => { + if (variant.meterage) { + const isSelected = variantId === variant._id + return ( + + ) + } + return null + })} +
+ )} + + {/* رنگ ها */} + {hasColor && ( +
+ {product.variants.map((variant) => { + if (variant.color) { + const isSelected = variantId === variant._id + return ( + + ) + } + return null + })} +
+ )} + + {/* سایزها */} + {hasSize && ( +
+ {product.variants.map((variant) => { + if (variant.size) { + const isSelected = variantId === variant._id + return ( + + ) + } + return null + })} +
+ )} + + {/* themeValue ها - نمایش داینامیک */} + {hasThemeValue && (() => { + // گروه‌بندی variant‌ها بر اساس theme + const themeGroups = new Map() + + product.variants.forEach(variant => { + if (variant.themeValue) { + const theme = variant.themeValue.theme + if (!themeGroups.has(theme)) { + themeGroups.set(theme, []) + } + themeGroups.get(theme)!.push(variant) + } + }) + + // تابع برای بررسی hex بودن value + const isHexColor = (value: string | number): boolean => { + if (typeof value !== 'string') return false + const hexPattern = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/ + return hexPattern.test(value) + } + + return Array.from(themeGroups.entries()).map(([theme, variants]) => ( +
+ {variants.map((variant) => { + if (variant.themeValue) { + const isSelected = variantId === variant._id + const value = typeof variant.themeValue.value === 'string' + ? variant.themeValue.value + : String(variant.themeValue.value) + const isHex = isHexColor(value) + + // اگر hex بود، دایره رنگی نمایش بده + if (isHex) { + return ( + + ) + } + + // اگر hex نبود، name را نمایش بده + return ( + + ) + } + return null + })} +
+ )) + })()} + + ) + })()}
-
+ )} { product.specifications.length > 0 ? diff --git a/src/components/CategoryModal.tsx b/src/components/CategoryModal.tsx index 6e3d662..d86a635 100644 --- a/src/components/CategoryModal.tsx +++ b/src/components/CategoryModal.tsx @@ -67,7 +67,7 @@ const CategoryModal: FC = ({ isOpen, onClose }) => { onClick={() => handleMainCategoryClick(category._id)} >
- {category.icon ? ( + {category.icon && category.icon.trim() !== '' && (category.icon.startsWith('http://') || category.icon.startsWith('https://') || category.icon.startsWith('/')) ? ( {category.title_fa} = ({ isOpen, onClose }) => { onClick={() => handleCategoryNavigate(item)} >
- {item.icon ? ( + {item.icon && item.icon.trim() !== '' && (item.icon.startsWith('http://') || item.icon.startsWith('https://') || item.icon.startsWith('/')) ? ( {item.title_fa} { onClick={() => handleCategoryClick(category)} >
- {category.icon && ( + {category.icon && category.icon.trim() !== '' && (category.icon.startsWith('http://') || category.icon.startsWith('https://') || category.icon.startsWith('/')) && (