213 lines
8.6 KiB
TypeScript
213 lines
8.6 KiB
TypeScript
'use client';
|
|
|
|
import { useState } from 'react';
|
|
import { Check, ShoppingCart, ShieldCheck, FileText } from 'lucide-react';
|
|
import BlurHashImage from '@/components/ui/BlurHashImage';
|
|
import { ProductResponse, ProductVariantResponse } from '@/services/api/catalogService';
|
|
import { useCartStore } from '@/store/cartStore';
|
|
import { formatCurrency, getImageUrl } from '@/lib/utils';
|
|
|
|
interface QuickComparisonTableProps {
|
|
product: ProductResponse;
|
|
}
|
|
|
|
function getVariantTitle(variant: ProductVariantResponse, fallbackIndex: number): string {
|
|
const validAttrs = (variant.attributes || []).filter((a) => {
|
|
const key = (a.attribute_name || a.attribute_code || '').toLowerCase().trim();
|
|
return a.attribute_value && !['media_key', 'parent_media_key'].includes(key);
|
|
});
|
|
|
|
if (validAttrs.length > 0) {
|
|
return validAttrs
|
|
.map((a) => {
|
|
const name = a.attribute_name ? `${a.attribute_name}: ` : '';
|
|
return `${name}${a.attribute_value}`;
|
|
})
|
|
.join(' | ');
|
|
}
|
|
|
|
return variant.sku || `Variant ${fallbackIndex + 1}`;
|
|
}
|
|
|
|
function getVariantImage(product: ProductResponse, variant: ProductVariantResponse): string {
|
|
const img = variant.images?.[0]?.image_url || product.images?.[0]?.image_url;
|
|
return getImageUrl(img);
|
|
}
|
|
|
|
export function QuickComparisonTable({ product }: QuickComparisonTableProps) {
|
|
const addToCart = useCartStore((state) => state.addToCart);
|
|
const [addedVariantId, setAddedVariantId] = useState<string | null>(null);
|
|
|
|
const variants = product?.variants || [];
|
|
// Strictly only show when the product has multiple variants (> 1)
|
|
if (variants.length <= 1) {
|
|
return null;
|
|
}
|
|
|
|
const handleAdd = async (variant: ProductVariantResponse) => {
|
|
await addToCart(product, variant, 1);
|
|
setAddedVariantId(variant.variant_id);
|
|
setTimeout(() => setAddedVariantId(null), 1800);
|
|
};
|
|
|
|
const labelCls =
|
|
'sticky left-0 z-10 w-[160px] min-w-[160px] px-5 py-4 text-[13px] font-semibold text-gray-700 text-left align-middle bg-[#f3f4f6] border-b border-gray-200';
|
|
const cellCls = 'px-5 py-4 min-w-[200px] align-middle text-center border-b border-gray-200';
|
|
|
|
return (
|
|
<section className="mb-12">
|
|
<div className="flex items-center gap-2 mb-4">
|
|
<h3 className="text-[20px] font-bold text-gray-800 tracking-tight">Variant Comparison</h3>
|
|
<span className="text-[12px] font-medium text-gray-500 bg-gray-100 px-2.5 py-0.5 rounded-full">
|
|
{variants.length} Options
|
|
</span>
|
|
</div>
|
|
|
|
<div className="overflow-x-auto rounded-lg border border-gray-200 shadow-sm bg-white">
|
|
<table className="w-full border-collapse min-w-[700px]">
|
|
{/* Variant Header Row */}
|
|
<thead>
|
|
<tr className="bg-white">
|
|
<th className={`${labelCls} bg-white text-gray-400 font-normal`}>Option</th>
|
|
{variants.map((v, idx) => {
|
|
const img = getVariantImage(product, v);
|
|
const title = getVariantTitle(v, idx);
|
|
return (
|
|
<th key={v.variant_id} className={`${cellCls} font-normal bg-white`}>
|
|
<div className="w-[100px] h-[100px] mx-auto bg-gray-50 border border-gray-200 rounded-lg p-2 flex items-center justify-center mb-3">
|
|
<BlurHashImage
|
|
src={img}
|
|
alt={title}
|
|
width={90}
|
|
height={90}
|
|
className="max-h-full max-w-full object-contain"
|
|
/>
|
|
</div>
|
|
<div className="text-[13px] font-semibold text-gray-800 leading-snug line-clamp-2 px-1 min-h-[36px]">
|
|
{title}
|
|
</div>
|
|
</th>
|
|
);
|
|
})}
|
|
</tr>
|
|
</thead>
|
|
|
|
<tbody>
|
|
{/* 1. Name / Specifications Row */}
|
|
<tr className="bg-[#fcfcfd]">
|
|
<td className={labelCls}>Name / Specification</td>
|
|
{variants.map((v, idx) => (
|
|
<td key={v.variant_id} className={`${cellCls} text-[13px] font-medium text-gray-800`}>
|
|
{getVariantTitle(v, idx)}
|
|
</td>
|
|
))}
|
|
</tr>
|
|
|
|
{/* 2. Price Row */}
|
|
<tr className="bg-white">
|
|
<td className={labelCls}>Price</td>
|
|
{variants.map((v) => {
|
|
const price = Number(v.price) || 0;
|
|
const comparePrice = v.compare_price ? Number(v.compare_price) : 0;
|
|
const hasDiscount = comparePrice > price;
|
|
const discountPercent = hasDiscount
|
|
? Math.round(((comparePrice - price) / comparePrice) * 100)
|
|
: 0;
|
|
|
|
return (
|
|
<td key={v.variant_id} className={cellCls}>
|
|
<div className="flex items-center justify-center gap-2 flex-wrap">
|
|
<span className="text-[16px] font-bold text-gray-900">
|
|
{formatCurrency(price)}
|
|
</span>
|
|
{hasDiscount && (
|
|
<>
|
|
<span className="text-[13px] text-gray-400 line-through">
|
|
{formatCurrency(comparePrice)}
|
|
</span>
|
|
<span className="text-[11px] font-semibold text-emerald-700 bg-emerald-50 border border-emerald-200 px-1.5 py-0.5 rounded">
|
|
{discountPercent}% OFF
|
|
</span>
|
|
</>
|
|
)}
|
|
</div>
|
|
</td>
|
|
);
|
|
})}
|
|
</tr>
|
|
|
|
{/* 3. Warranty Type Row */}
|
|
<tr className="bg-[#fcfcfd]">
|
|
<td className={labelCls}>
|
|
<div className="flex items-center gap-1.5">
|
|
<ShieldCheck size={15} className="text-primary flex-shrink-0" />
|
|
<span>Warranty Type</span>
|
|
</div>
|
|
</td>
|
|
{variants.map((v) => (
|
|
<td key={v.variant_id} className={`${cellCls} text-[13px] text-gray-700 font-medium`}>
|
|
{product.warranty_type || 'Standard Warranty'}
|
|
</td>
|
|
))}
|
|
</tr>
|
|
|
|
{/* 4. Summary Row */}
|
|
<tr className="bg-white">
|
|
<td className={labelCls}>
|
|
<div className="flex items-center gap-1.5">
|
|
<FileText size={15} className="text-primary flex-shrink-0" />
|
|
<span>Summary</span>
|
|
</div>
|
|
</td>
|
|
{variants.map((v) => (
|
|
<td key={v.variant_id} className={`${cellCls} text-[13px] text-gray-600 leading-relaxed max-w-[280px]`}>
|
|
{product.warranty_summary || '100% Quality Tested & Brand Verified'}
|
|
</td>
|
|
))}
|
|
</tr>
|
|
|
|
{/* Action Row */}
|
|
<tr className="bg-[#fcfcfd]">
|
|
<td className={labelCls}>Action</td>
|
|
{variants.map((v) => {
|
|
const justAdded = addedVariantId === v.variant_id;
|
|
const isOutOfStock = v.status === 'inactive' || (v.available_stock !== undefined && v.available_stock <= 0);
|
|
|
|
return (
|
|
<td key={v.variant_id} className={cellCls}>
|
|
<button
|
|
type="button"
|
|
disabled={isOutOfStock}
|
|
onClick={() => void handleAdd(v)}
|
|
className={`inline-flex items-center justify-center gap-2 min-w-[130px] text-[13px] font-semibold px-4 py-2.5 rounded-md transition-all cursor-pointer shadow-sm ${
|
|
isOutOfStock
|
|
? 'bg-gray-200 text-gray-400 cursor-not-allowed'
|
|
: justAdded
|
|
? 'bg-emerald-600 text-white'
|
|
: 'bg-primary hover:bg-primary-hover text-white hover:shadow'
|
|
}`}
|
|
>
|
|
{isOutOfStock ? (
|
|
'Out of Stock'
|
|
) : justAdded ? (
|
|
<>
|
|
<Check size={14} className="stroke-[2.5]" />
|
|
<span>Added</span>
|
|
</>
|
|
) : (
|
|
<>
|
|
<ShoppingCart size={14} />
|
|
<span>Add To Cart</span>
|
|
</>
|
|
)}
|
|
</button>
|
|
</td>
|
|
);
|
|
})}
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</section>
|
|
);
|
|
}
|