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'; const ProductCard = ({ product, simpleTitle, variant = 'vertical' }) => { const router = useRouter(); const utmSource = useUtmSource(); const [discount, setDiscount] = useState(0); let voucherPastiHemat = 0; if (product?.voucherPastiHemat ? product?.voucherPastiHemat.length : voucherPastiHemat > 0) { const stringVoucher = product?.voucherPastiHemat[0]; const validJsonString = stringVoucher.replace(/'/g, '"'); voucherPastiHemat = JSON.parse(validJsonString); } const callForPriceWhatsapp = whatsappUrl('product', { name: product.name, manufacture: product.manufacture?.name, url: createSlug('/shop/product/', product.name, product.id, true), }); const image = useMemo(() => { if (product.image) return product.image + '?ratio=square'; return '/images/noimage.jpeg'; }, [product.image]); 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.discount_type === 'percentage') { countDiscount = product?.lowestPrice.priceDiscount * (voucherPastiHemat.discount_amount / 100); if ( voucherPastiHemat.max_discount > 0 && countDiscount > voucherPastiHemat.max_discount ) { countDiscount = voucherPastiHemat.max_discount; } } else { countDiscount = voucherPastiHemat.discount_amount; } setDiscount(countDiscount); }; useEffect(() => { hitungDiscountVoucher(); }, []); if (variant == 'vertical') { return (
{product?.name}
{product?.isSni && ( )}
{product?.isTkdn && ( )}
{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} ) : (
-
)} {product?.name} {product?.flashSale?.id > 0 && product?.lowestPrice.discountPercentage > 0 ? ( <>
{currencyFormat(product.lowestPrice.price)}
{Math.floor(product?.lowestPrice.discountPercentage)}%
{product?.lowestPrice.priceDiscount > 0 ? ( currencyFormat(product?.lowestPrice.priceDiscount) ) : ( Call for Inquiry )}
) : (
{product?.lowestPrice.price > 0 ? ( <> {currencyFormat(product?.lowestPrice.price)}
Inc. PPN:{' '} {currencyFormat( product.lowestPrice.price * process.env.NEXT_PUBLIC_PPN )}
) : ( Call for Inquiry )}
)} {discount > 0 && product?.flashSale?.id < 1 && (
Voucher : {currencyFormat(discount)}
)}
{product?.stockTotal > 0 && (
Ready Stock
)} {/*
{product?.stockTotal > 5 ? '> 5' : '< 5'}
*/} {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?.name} {product?.flashSale?.id > 0 && product?.lowestPrice?.discountPercentage > 0 ? ( <> {product?.lowestPrice.discountPercentage > 0 && (
{Math.floor(product?.lowestPrice?.discountPercentage)}%
{currencyFormat(product?.lowestPrice?.price)}
)}
{product?.lowestPrice?.priceDiscount > 0 ? ( currencyFormat(product?.lowestPrice?.priceDiscount) ) : ( Call for Inquiry )}
) : (
{product?.lowestPrice.price > 0 ? ( <> {currencyFormat(product?.lowestPrice.price)}
Inc. PPN:{' '} {currencyFormat( product.lowestPrice.price * process.env.NEXT_PUBLIC_PPN )}
) : ( Call for Inquiry )}
)} {discount > 0 && product?.flashSale?.id < 1 && (
Voucher : {currencyFormat(discount)}
)}
{product?.stockTotal > 0 && (
Ready Stock
)} {/*
{product?.stockTotal > 5 ? '> 5' : '< 5'}
*/} {product?.qtySold > 0 && (
{sellingProductFormat(product?.qtySold) + ' Terjual'}
)}
); } }; export default ProductCard;