Files
hoanghapc_nextJs/src/components/shared/ProductItem.tsx

158 lines
6.7 KiB
TypeScript
Raw Normal View History

2026-01-15 17:30:04 +07:00
'use client';
import Link from "next/link";
2026-01-30 17:09:41 +07:00
import { formatPrice,formatTextList } from "@/lib/utils";
2026-01-16 17:04:10 +07:00
import { useProductItem } from "@/hooks/useProductItem"
import { useCart } from '@/hooks/useCart';
2026-01-27 17:02:26 +07:00
import { useEffect, useState } from "react";
2026-01-30 17:09:41 +07:00
import parse from 'html-react-parser';
2026-01-15 17:30:04 +07:00
export default function ProductItem({item}:any){
2026-01-27 17:02:26 +07:00
const [mounted, setMounted] = useState(false);
useEffect(() => {
setMounted(true);
}, []);
2026-01-16 17:04:10 +07:00
const product = useProductItem(item);
const { addToCart, isInCart } = useCart();
2026-01-27 17:02:26 +07:00
if (!mounted || !product) return null;
2026-01-16 17:04:10 +07:00
const {
productId,
productUrl,
productImage,
productName,
quantity,
price,
marketPrice,
discount,
displayOffer,
warranty,
displaySummary
} = product;
const checkIncart = isInCart(productId);
const priceView = price > 0 ? formatPrice(price) + 'đ' : 'Liên hệ';
2026-01-15 17:30:04 +07:00
return (
2026-01-16 17:04:10 +07:00
<div className="p-item js-p-item">
<Link href={productUrl} className="p-img">
2026-01-15 17:30:04 +07:00
<img
2026-01-16 17:04:10 +07:00
src={productImage}
alt={productName}
width={250} height={250}
2026-01-15 17:30:04 +07:00
/>
2026-01-16 17:04:10 +07:00
</Link>
2026-01-15 17:30:04 +07:00
<div className="p-text">
<div className="p-price-group">
2026-01-16 17:04:10 +07:00
{discount > 0 &&
<>
<del>{formatPrice(marketPrice)} đ</del>
<span className="p-discount">-{discount}%</span>
</>
}
<p className="p-price"> {priceView} </p>
2026-01-15 17:30:04 +07:00
</div>
2026-01-16 17:04:10 +07:00
<Link href={productUrl} className="p-name">
<h3>{productName}</h3>
2026-01-15 17:30:04 +07:00
</Link>
<div className="p-btn-group flex items-center justify-between text-16 font-500 leading-[23px]">
<div>
2026-01-16 17:04:10 +07:00
{quantity > 0 ?
(
<p className="m-0 text-[#00AD4F] flex items-center gap-1">
<i className="bx bx-check-circle text-18 w-[18px]"></i>
<span> Sẵn hàng </span>
</p>
) : (
<p className="m-0 red flex items-center gap-1">
<i className="bx bxs-phone text-18 w-[18px]"></i>
<span> Liên hệ </span>
</p>
)
}
2026-01-15 17:30:04 +07:00
2026-01-16 17:04:10 +07:00
{displayOffer &&
<p className="m-0 text-[#E16B10] flex items-center gap-1">
<i className="bx bx-gift text-18 w-[18px]"></i>
<span> Quà tặng </span>
</p>
}
</div>
2026-01-15 17:30:04 +07:00
<button className={`p-btn bx bg-btn text-white rounded-full w-9 h-9 text-20 ${checkIncart ? 'bx-check' : 'bx-plus' }`}
type="button" aria-label="Mua"
2026-01-16 17:04:10 +07:00
style={{ background: `${checkIncart ? '#ccc' : ''}` }}
disabled={checkIncart}
onClick={() => addToCart(productId)}
2026-01-15 17:30:04 +07:00
></button>
</div>
</div>
<div className="p-tooltip hidden">
<div className="bg-white rounded-[20px] overflow-hidden border-2 border-[#EAECF0] shadow-[0px_6px_8px_-2px_#10182814]">
<p className="tooltip-name px-5 py-4 text-white font-600 text-16 leading-[21px] bg-[linear-gradient(180.3deg,#259AFF_-18.56%,_#114CDD_100.92%)] m-0">
2026-01-16 17:04:10 +07:00
{productName}
2026-01-15 17:30:04 +07:00
</p>
<div className="p-4 tooltip-content">
<div className="last:border-0 last:p-0 last:m-0 text-16 border-b border-[#DEE4EC] mb-4 pb-4">
<p className="m-0 flex items-center flex-wrap gap-1">
<b className="font-600"> Giá bán: </b>
2026-01-16 17:04:10 +07:00
<b className="font-600 text-[#FF4E2A] text-18"> {priceView} </b>
2026-01-15 17:30:04 +07:00
2026-01-16 17:04:10 +07:00
{discount > 0 &&
<>
<del className="text-[#A0A5AC] font-300"> {formatPrice(marketPrice)} đ </del>
<span className="bg-[#FA354A] text-white text-11 px-[6px] leading-[18px] rounded-[20px]">
-{discount}%
</span>
</>
}
2026-01-15 17:30:04 +07:00
</p>
2026-01-16 17:04:10 +07:00
{warranty &&
<p className="m-0">
<b className="font-600"> Bảo hành: </b>
<span> {warranty} </span>
</p>
}
</div>
{displaySummary &&
<div className="last:mb-0 mb-6 px-1">
<p className="text-16 font-600 flex items-center leading-6 mb-2">
<i className="icons icon-screen"></i>
<span> Thông số sản phẩm </span>
</p>
2026-01-15 17:30:04 +07:00
2026-01-16 17:04:10 +07:00
<div className="tooltip-spec">
2026-01-30 17:09:41 +07:00
{parse(formatTextList(displaySummary, 5))}
2026-01-16 17:04:10 +07:00
</div>
2026-01-15 17:30:04 +07:00
</div>
2026-01-16 17:04:10 +07:00
}
2026-01-15 17:30:04 +07:00
2026-01-16 17:04:10 +07:00
{ displayOffer &&
<div className="rounded-[12px] bg-[linear-gradient(182.15deg,#FFA480_-18.44%,#EB0C23_60.76%)] p-1 pt-2">
<p className="px-2 text-16 font-600 flex items-center leading-5 mb-2 text-white">
<i className="icons icon-gift"></i>
<span> Khuyến mại hấp dẫn </span>
</p>
2026-01-15 17:30:04 +07:00
2026-01-16 17:04:10 +07:00
<div className="tooltip-offer rounded-[8px] bg-[#FEF2F2] px-2 py-4">
2026-01-30 17:09:41 +07:00
{parse(formatTextList(displayOffer, 5))}
2026-01-16 17:04:10 +07:00
</div>
2026-01-15 17:30:04 +07:00
</div>
2026-01-16 17:04:10 +07:00
}
2026-01-15 17:30:04 +07:00
</div>
</div>
</div>
</div>
)
}