diff --git a/apps/admin/src/pages/ProductEditPage.tsx b/apps/admin/src/pages/ProductEditPage.tsx index 4ff32c0..b9532cd 100644 --- a/apps/admin/src/pages/ProductEditPage.tsx +++ b/apps/admin/src/pages/ProductEditPage.tsx @@ -124,7 +124,7 @@ export default function ProductEditPage() { const [name, setName] = useState>({}) const [description, setDescription] = useState>({}) const [details, setDetails] = useState>({}) - const [options, setOptions] = useState<{name: Record, values: Record[]}[]>([]) + const [options, setOptions] = useState<{name: Record, values: (Record & { imageUrl?: string })[]}[]>([]) const [variants, setVariants] = useState[]>([]) const [images, setImages] = useState([]) const [titleGraphicUrl, setTitleGraphicUrl] = useState('') @@ -254,7 +254,7 @@ export default function ProductEditPage() { setVariants(updated) } - const regenerateVariants = (newOptions: {name: Record, values: Record[]}[]) => { + const regenerateVariants = (newOptions: {name: Record, values: (Record & { imageUrl?: string })[]}[]) => { setVariants(prevVariants => { const validOptions = newOptions.filter(o => o.name && (o.name['th'] || o.name['en']) && o.values.length > 0) if (validOptions.length === 0) { @@ -277,7 +277,15 @@ export default function ProductEditPage() { const option3 = combo[2] || '' const existing = prevVariants.find(v => v.option1 === option1 && v.option2 === option2 && v.option3 === option3) - return existing || { option1, option2, option3, price: 0, stock: 0, sku: '', barcode: '' } + const opt1Val = newOptions[0]?.values.find(v => (v['th'] || v['en'] || Object.values(v)[0]) === option1) + const swatchUrl = opt1Val?.imageUrl || null + if (existing) { + return { + ...existing, + imageUrl: existing.imageUrl || swatchUrl || null, + } + } + return { option1, option2, option3, price: 0, stock: 0, sku: '', barcode: '', imageUrl: swatchUrl } }) }) } @@ -1058,6 +1066,76 @@ export default function ProductEditPage() { className="w-full px-4 py-2 bg-night-950 border border-white/10 focus:border-cyan-glow/30 rounded-xl outline-none text-sm text-white transition-all shadow-inner" placeholder={`Value ${vIdx + 1}`} /> + {val.imageUrl ? ( +
+ Swatch +
+ + +
+
+ ) : ( + + )}