import clsx from 'clsx'; import ImageNext from 'next/image'; import { useRouter } from 'next/router'; import { useMemo, useEffect, useState } from 'react'; import Image from '@/core/components/elements/Image/Image'; import Link from '@/core/components/elements/Link/Link'; import currencyFormat from '@/core/utils/currencyFormat'; import { sellingProductFormat } from '@/core/utils/formatValue'; import { createSlug } from '@/core/utils/slug'; import whatsappUrl from '@/core/utils/whatsappUrl'; import useUtmSource from '~/hooks/useUtmSource'; import useDevice from '@/core/hooks/useDevice'; import { BadgePercent, Tag } from 'lucide-react'; import { TicketIcon } from '@heroicons/react/24/solid'; import DesktopView from '@/core/components/views/DesktopView'; import MobileView from '@/core/components/views/MobileView'; const ProductCard = ({ product, simpleTitle, variant = 'vertical' }) => { const router = useRouter(); const utmSource = useUtmSource(); const [discount, setDiscount] = useState(0); const { isDesktop, isMobile } = useDevice(); let voucherPastiHemat = 0; voucherPastiHemat = product?.newVoucherPastiHemat[0] ? product?.newVoucherPastiHemat[0] : product?.newVoucherPastiHemat; const callForPriceWhatsapp = whatsappUrl('product', { name: product.name, manufacture: product.manufacture?.name, url: createSlug('/shop/product/', product.name, product.id, true), }); const image = useMemo(() => { if (!isDesktop && product.image_mobile) { return product.image_mobile + '?ratio=square'; } else { if (product.image) return product.image + '?ratio=square'; return '/images/noimage.jpeg'; } }, [product.image, product.image_mobile]); const URL = { product: createSlug('/shop/product/', product?.name, product?.id) + `?utm_source=${utmSource}`, manufacture: createSlug( '/shop/brands/', product?.manufacture?.name, product?.manufacture.id ), }; const hitungDiscountVoucher = () => { let countDiscount = 0; if (voucherPastiHemat.discountType === 'percentage') { countDiscount = product?.lowestPrice.priceDiscount * (voucherPastiHemat.discountAmount / 100); if ( voucherPastiHemat.maxDiscount > 0 && countDiscount > voucherPastiHemat.maxDiscount ) { countDiscount = voucherPastiHemat.maxDiscount; } } else { countDiscount = voucherPastiHemat.discountAmount; } setDiscount(countDiscount); }; useEffect(() => { hitungDiscountVoucher(); }, []); if (variant == 'vertical') { return ( <>
{product?.name}
{product?.isSni && ( )}
{product?.isTkdn && ( )}
{(product?.lowestPrice?.discountPercentage ?? 0) > 0 && (
{Math.floor(product.lowestPrice.discountPercentage)}%
)} {(product?.variantTotal > 1 || product?.isInBu) && (
{' '} {product?.variantTotal > 1 && (
{' '} {product.variantTotal} Varian{' '}
)}{' '} {product?.isInBu && ( Pick Up Now )}{' '}
)} {router.pathname != '/' && product?.flashSale?.id > 0 && (
{/*
{Math.floor(product?.lowestPrice.discountPercentage)}%
{product?.flashSale?.tag != 'false' || product?.flashSale?.tag ? product?.flashSale?.tag : 'FLASH SALE'}
*/}
)} {/* {product.variantTotal > 1 && (
{product.variantTotal} Varian
)} */}
{product?.manufacture?.name ? ( {product.manufacture.name} ) : (
-
)}
{/* ⬇️ line clamp desktop dibiarkan seperti aslinya */} {product?.name} {/* ---------- HARGA ---------- */} {product?.flashSale?.id > 0 && product?.lowestPrice.discountPercentage > 0 ? ( // ===== CASE FLASH SALE =====
{product?.lowestPrice.priceDiscount > 0 ? ( currencyFormat(product?.lowestPrice.priceDiscount) ) : ( Call for Inquiry )} {currencyFormat(product.lowestPrice.price)}
{product?.lowestPrice.priceDiscount > 0 && (
Include PPN:  {currencyFormat( Math.round( Number(product.lowestPrice.priceDiscount) * process.env.NEXT_PUBLIC_PPN ) )} {/* {currencyFormat( Math.round( Number(product.lowestPrice.priceDiscount) * process.env.NEXT_PUBLIC_PPN ) )} */}
)}
) : ( // ===== BUKAN FLASH SALE ===== (() => { const basePrice = Number( product?.lowestPrice?.priceDiscount || 0 ); const voucherCut = Number(discount || 0); const finalAfterVoucher = Math.max(basePrice - voucherCut, 0); // CASE 1: ada harga (>0) if (Number(product?.lowestPrice?.price) > 0) { if (voucherCut > 0) { return (
{/* baris harga sekarang + harga coret */}
{/* harga setelah voucher */} {currencyFormat(finalAfterVoucher)} {/* [OPTIONAL] harga sebelum voucher (coret) kalau mau muncul */} {/* {currencyFormat(basePrice)} */}
{/* PPN pakai harga finalAfterVoucher */}
Include PPN:  {currencyFormat( Math.round( finalAfterVoucher * process.env.NEXT_PUBLIC_PPN ) )} {/* {currencyFormat( Math.round( finalAfterVoucher * process.env.NEXT_PUBLIC_PPN ) )} */}
); } // CASE 1b: ga ada voucher return (
{currencyFormat(basePrice)}
Include PPN:  {currencyFormat( Math.round( Number(product.lowestPrice.price) * process.env.NEXT_PUBLIC_PPN ) )} {/* {currencyFormat( Math.round( Number(product.lowestPrice.price) * process.env.NEXT_PUBLIC_PPN ) )} */}
); } // CASE 2: harga 0 → call for inquiry return (
Call for Inquiry
); })() )} {/* ---------- /HARGA ---------- */}
{(product?.stockTotal > 0 || product?.qtySold > 0) && (
{product?.stockTotal > 0 && (
Ready Stock
)} {product?.qtySold > 0 && (
{sellingProductFormat(product?.qtySold)} Terjual
)}
)}
{product?.name}
{/* SNI / TKDN (kanan-atas, tetap) */}
{product?.isSni && ( )}
{product?.isTkdn && ( )}
{/* BADGE DISKON Kanan-ATAS */} {(product?.lowestPrice?.discountPercentage ?? 0) > 0 && (
{Math.floor(product.lowestPrice.discountPercentage)}%
)} {/* BOTTOM-LEFT: Varian + PICK UP NOW */} {(product?.variantTotal > 1 || product?.isInBu) && (
{product?.variantTotal > 1 && (
{product.variantTotal} Varian
)} {product?.isInBu && ( Pick Up Now )}
)}
{router.pathname != '/' && product?.flashSale?.id > 0 && (
)} {/* ⬇️ konten bawah (tidak diubah) */}
{product?.manufacture?.name ? ( {product.manufacture.name} ) : (
-
)}
{product?.name} {/* ---------- HARGA ---------- */} {product?.flashSale?.id > 0 && product?.lowestPrice.discountPercentage > 0 ? ( // ===== CASE FLASH SALE =====
{product?.lowestPrice.priceDiscount > 0 ? ( currencyFormat(product?.lowestPrice.priceDiscount) ) : ( Call for Inquiry )} {currencyFormat(product.lowestPrice.price)}
{product?.lowestPrice.priceDiscount > 0 && (
Include PPN:  {currencyFormat( Math.round( Number(product.lowestPrice.priceDiscount) * process.env.NEXT_PUBLIC_PPN ) )} {/* {currencyFormat( Math.round( Number(product.lowestPrice.priceDiscount) * process.env.NEXT_PUBLIC_PPN ) )} */}
)}
) : ( // ===== BUKAN FLASH SALE ===== (() => { const basePrice = product?.lowestPrice?.priceDiscount || 0; const voucherCut = discount || 0; const finalAfterVoucher = Math.max(basePrice - voucherCut, 0); // CASE 1: ada harga (>0) if (product?.lowestPrice?.price > 0) { if (voucherCut > 0) { return (
{/* baris harga sekarang + harga coret */}
{/* harga setelah voucher */} {currencyFormat(finalAfterVoucher)} {/* harga sebelum voucher, dicoret, truncate kalau kepanjangan */} {/* {currencyFormat(basePrice)} */}
{/* PPN pakai harga finalAfterVoucher */}
Include PPN:  {currencyFormat( finalAfterVoucher * process.env.NEXT_PUBLIC_PPN )} {/* {currencyFormat( finalAfterVoucher * process.env.NEXT_PUBLIC_PPN )} */}
); } // CASE 1b: ga ada voucher return (
{currencyFormat(basePrice)}
Include PPN:  {currencyFormat( product.lowestPrice.price * process.env.NEXT_PUBLIC_PPN )} {/* {currencyFormat( product.lowestPrice.price * process.env.NEXT_PUBLIC_PPN )} */}
); } // CASE 2: harga 0 → call for inquiry return (
Call for Inquiry
); })() )} {/* ---------- /HARGA ---------- */} {(product?.stockTotal > 0 || product?.qtySold > 0) && (
{product?.stockTotal > 0 && (
Ready Stock
)} {product?.qtySold > 0 && (
{sellingProductFormat(product?.qtySold)} Terjual
)}
)}
); } if (variant == 'horizontal') { return (
{product?.name}
{product?.isSni && ( )}
{product?.isTkdn && ( )}
{product.variantTotal > 1 && (
{product.variantTotal} Varian
)}
{product.flashSale.id > 0 && (
{' '} {product?.flashSale?.tag != 'false' || product?.flashSale?.tag ? product?.flashSale?.tag : 'FLASH SALE'}
)} {product?.manufacture?.name ? (
{product.manufacture.name} {/* {product?.is_in_bu && (
Click & Pickup
)} */}
) : (
-
)} {product?.name} {/* ---------- HARGA ---------- */} {product?.flashSale?.id > 0 && product?.lowestPrice.discountPercentage > 0 ? ( // ===== CASE FLASH SALE =====
{product?.lowestPrice.priceDiscount > 0 ? ( currencyFormat(product?.lowestPrice.priceDiscount) ) : ( Call for Inquiry )} {currencyFormat(product.lowestPrice.price)}
) : ( // ===== BUKAN FLASH SALE ===== (() => { const basePrice = product?.lowestPrice?.priceDiscount || 0; const voucherCut = discount || 0; const finalAfterVoucher = Math.max(basePrice - voucherCut, 0); // CASE 1: ada harga (>0) if (product?.lowestPrice?.price > 0) { if (voucherCut > 0) { return (
{/* baris harga sekarang + harga coret */}
{/* harga setelah voucher */} {currencyFormat(finalAfterVoucher)} {/* harga sebelum voucher, dicoret, truncate kalau kepanjangan */} {/* {currencyFormat(basePrice)} */}
{/* PPN pakai harga finalAfterVoucher */}
Include PPN:  {/* {currencyFormat( finalAfterVoucher * process.env.NEXT_PUBLIC_PPN )} */} {currencyFormat( finalAfterVoucher * process.env.NEXT_PUBLIC_PPN )}
); } // CASE 1b: ga ada voucher return (
{currencyFormat(basePrice)}
Include PPN:  {currencyFormat( product.lowestPrice.price * process.env.NEXT_PUBLIC_PPN )}
); } // CASE 2: harga 0 → call for inquiry return (
Call for Inquiry
); })() )} {/* ---------- /HARGA ---------- */}
{product?.stockTotal > 0 && (
Ready Stock
)} {/*
{product?.stockTotal > 5 ? '> 5' : '< 5'}
*/} {product?.qtySold > 0 && (
{sellingProductFormat(product?.qtySold) + ' Terjual'}
)}
); } }; export default ProductCard;