203 lines
7.8 KiB
TypeScript
203 lines
7.8 KiB
TypeScript
"use client";
|
|
import React, { useState } from "react";
|
|
import Link from "next/link";
|
|
import { ShoppingCart, Check, ExternalLink, ShieldCheck } from "lucide-react";
|
|
import SafeImage from "./SafeImage";
|
|
import { useCartStore } from "../lib/store/cartStore";
|
|
|
|
export interface ArticleProductCardProps {
|
|
product: {
|
|
id: string;
|
|
slug?: string;
|
|
nameFa: string;
|
|
nameEn?: string;
|
|
description?: string;
|
|
category?: { name: string; slug?: string } | string;
|
|
categorySlug?: string;
|
|
imageUrl?: string;
|
|
images?: string[];
|
|
priceValue?: number;
|
|
priceDisplay?: string;
|
|
prices?: { price: number; volume?: string }[];
|
|
stock?: number;
|
|
stockStatus?: string;
|
|
isOriginal?: boolean;
|
|
madeInGermany?: boolean;
|
|
rating?: number;
|
|
};
|
|
compact?: boolean;
|
|
}
|
|
|
|
export default function ArticleProductCard({ product, compact = false }: ArticleProductCardProps) {
|
|
const [isAdded, setIsAdded] = useState(false);
|
|
const addItem = useCartStore((state) => state.addItem);
|
|
|
|
if (!product || !product.nameFa) return null;
|
|
|
|
const productUrl = `/shop/${encodeURIComponent(product.slug || product.id)}`;
|
|
const imageSrc =
|
|
product.imageUrl ||
|
|
(Array.isArray(product.images) && product.images[0]) ||
|
|
"/assets/images/canina-product-placeholder.png";
|
|
|
|
const categoryName =
|
|
typeof product.category === "string"
|
|
? product.category
|
|
: product.category?.name || product.categorySlug || "مکمل تخصصی کنینا";
|
|
|
|
// Price calculation
|
|
const rawPrice =
|
|
product.priceValue ||
|
|
(Array.isArray(product.prices) && product.prices[0]?.price) ||
|
|
0;
|
|
|
|
const formattedPrice =
|
|
product.priceDisplay ||
|
|
(rawPrice > 0 ? `${Number(rawPrice).toLocaleString("fa-IR")} تومان` : "تماس بگیرید");
|
|
|
|
const isOutOfStock =
|
|
product.stockStatus === "OUT_OF_STOCK" ||
|
|
product.stockStatus === "DISCONTINUED" ||
|
|
(typeof product.stock === "number" && product.stock <= 0);
|
|
|
|
const handleAddToCart = (e: React.MouseEvent) => {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
if (isOutOfStock) return;
|
|
|
|
// Adapt to CartStore Product shape
|
|
const storeProduct: any = {
|
|
id: product.id,
|
|
slug: product.slug || product.id,
|
|
nameFa: product.nameFa,
|
|
nameEn: product.nameEn || "",
|
|
category: typeof product.category === "string" ? { name: product.category, slug: "" } : product.category,
|
|
priceValue: rawPrice,
|
|
images: [imageSrc],
|
|
stock: product.stock ?? 99,
|
|
stockStatus: product.stockStatus || "IN_STOCK",
|
|
};
|
|
|
|
addItem(storeProduct, 1);
|
|
setIsAdded(true);
|
|
setTimeout(() => {
|
|
setIsAdded(false);
|
|
}, 2500);
|
|
};
|
|
|
|
const buttonAriaLabel = isOutOfStock
|
|
? "ناموجود"
|
|
: isAdded
|
|
? "به سبد اضافه شد"
|
|
: "افزودن به سبد خرید";
|
|
|
|
return (
|
|
<div
|
|
data-testid="article-product-card"
|
|
className="my-6 sm:my-8 rounded-3xl bg-gradient-to-br from-white via-medical-gray-50/60 to-canina-blue/5 border-2 border-canina-blue/20 hover:border-canina-blue/40 shadow-lg hover:shadow-xl transition-all duration-300 overflow-hidden font-vazir text-right group not-prose"
|
|
dir="rtl"
|
|
>
|
|
<div className="p-4 sm:p-6 flex flex-col sm:flex-row items-center gap-4 sm:gap-6">
|
|
{/* Product Image Thumbnail */}
|
|
<Link
|
|
href={productUrl}
|
|
className="relative w-28 h-28 sm:w-32 sm:h-32 shrink-0 bg-white rounded-2xl p-2 border border-medical-gray-200/80 shadow-inner flex items-center justify-center overflow-hidden group-hover:scale-105 transition-transform duration-300"
|
|
aria-label={product.nameFa}
|
|
>
|
|
<SafeImage
|
|
src={imageSrc}
|
|
alt={product.nameFa}
|
|
className="w-full h-full"
|
|
imgClassName="w-full h-full object-contain"
|
|
/>
|
|
{/* Made In Germany Floating Pill on Image */}
|
|
<div className="absolute top-1.5 right-1.5 bg-black/80 backdrop-blur-xs text-white text-[9px] font-black px-1.5 py-0.5 rounded-md flex items-center gap-0.5 shadow-xs border border-white/20">
|
|
<span className="w-1.5 h-1.5 rounded-full bg-red-500 animate-pulse" />
|
|
<span>اصل آلمان</span>
|
|
</div>
|
|
</Link>
|
|
|
|
{/* Product Details */}
|
|
<div className="flex-1 min-w-0 w-full flex flex-col justify-between h-full space-y-2">
|
|
<div>
|
|
<div className="flex items-center justify-between gap-2 mb-1">
|
|
<span className="text-[11px] font-bold text-canina-blue bg-canina-blue/10 px-2.5 py-0.5 rounded-full">
|
|
{categoryName}
|
|
</span>
|
|
<div className="flex items-center gap-1 text-[11px] font-bold text-emerald-700 bg-emerald-50 px-2 py-0.5 rounded-md border border-emerald-200/60">
|
|
<ShieldCheck className="w-3.5 h-3.5 text-emerald-600" />
|
|
<span>ضمانت ۱۰۰٪ اصالت آلمان</span>
|
|
</div>
|
|
</div>
|
|
|
|
<Link href={productUrl} className="block group-hover:text-canina-blue transition-colors">
|
|
<h4 className="text-base sm:text-lg font-black text-medical-gray-900 leading-snug truncate">
|
|
{product.nameFa}
|
|
</h4>
|
|
{product.nameEn && (
|
|
<span className="text-xs font-mono font-medium text-medical-gray-400 block truncate mt-0.5 dir-ltr text-right">
|
|
{product.nameEn}
|
|
</span>
|
|
)}
|
|
</Link>
|
|
|
|
{product.description && (
|
|
<p className="text-xs text-medical-gray-600 line-clamp-2 mt-1 leading-relaxed">
|
|
{product.description}
|
|
</p>
|
|
)}
|
|
</div>
|
|
|
|
{/* Price & Action Button Row */}
|
|
<div className="flex flex-wrap items-center justify-between gap-3 pt-3 border-t border-medical-gray-200/60">
|
|
<div>
|
|
<span className="text-[10px] text-medical-gray-400 block font-bold">قیمت مصرفکننده:</span>
|
|
<span className="text-sm sm:text-base font-black text-canina-blue font-mono">
|
|
{formattedPrice}
|
|
</span>
|
|
</div>
|
|
|
|
<div className="flex items-center gap-2">
|
|
<Link
|
|
href={productUrl}
|
|
className="px-3.5 py-2 bg-white hover:bg-medical-gray-100 text-medical-gray-700 text-xs font-bold rounded-xl border border-medical-gray-200 transition-colors flex items-center gap-1.5 shadow-2xs"
|
|
>
|
|
<span>مشاهده مشخصات</span>
|
|
<ExternalLink className="w-3.5 h-3.5" />
|
|
</Link>
|
|
|
|
<button
|
|
type="button"
|
|
onClick={handleAddToCart}
|
|
disabled={isOutOfStock}
|
|
aria-label={buttonAriaLabel}
|
|
className={`px-4 py-2 text-xs font-black rounded-xl transition-all duration-300 flex items-center gap-1.5 shadow-md cursor-pointer ${
|
|
isOutOfStock
|
|
? "bg-medical-gray-200 text-medical-gray-400 cursor-not-allowed"
|
|
: isAdded
|
|
? "bg-emerald-600 text-white hover:bg-emerald-700"
|
|
: "bg-canina-blue hover:bg-canina-blue/90 text-white active:scale-95"
|
|
}`}
|
|
>
|
|
{isAdded ? (
|
|
<>
|
|
<Check className="w-4 h-4 animate-in zoom-in-50" />
|
|
<span>به سبد اضافه شد</span>
|
|
</>
|
|
) : isOutOfStock ? (
|
|
<span>ناموجود</span>
|
|
) : (
|
|
<>
|
|
<ShoppingCart className="w-4 h-4" />
|
|
<span>خرید مستقیم</span>
|
|
</>
|
|
)}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|