import Image from '@/core/components/elements/Image/Image' import ImageNext from 'next/image' import Link from '@/core/components/elements/Link/Link' import DesktopView from '@/core/components/views/DesktopView' import currencyFormat from '@/core/utils/currencyFormat' import { HeartIcon } from '@heroicons/react/24/outline' import { useCallback, useEffect, useRef, useState } from 'react' import LazyLoad from 'react-lazy-load' import ProductSimilar from '../ProductSimilar' import { toast } from 'react-hot-toast' import { updateItemCart } from '@/core/utils/cart' import { useRouter } from 'next/router' import { createSlug } from '@/core/utils/slug' import BottomPopup from '@/core/components/elements/Popup/BottomPopup' import ProductCard from '../ProductCard' import productSimilarApi from '../../api/productSimilarApi' import whatsappUrl from '@/core/utils/whatsappUrl' import PromotionType from '@/lib/promotinProgram/components/PromotionType' import useAuth from '@/core/hooks/useAuth' const ProductDesktop = ({ products, wishlist, toggleWishlist }) => { const router = useRouter() const auth = useAuth() const { slug } = router.query const [quantityActive, setQuantity] = useState(null) const [lowestPrice, setLowestPrice] = useState(null) const [product, setProducts] = useState(products) const [addCartAlert, setAddCartAlert] = useState(false) const [promotionType, setPromotionType] = useState(false) const [promotionActiveId, setPromotionActiveId] = useState(null) const [selectVariantPromoActive, setSelectVariantPromoActive] = useState(null) const getLowestPrice = useCallback(() => { const prices = product.variants.map((variant) => variant.price) const lowest = prices.reduce((lowest, price) => { return price.priceDiscount < lowest.priceDiscount ? price : lowest }, prices[0]) return lowest }, [product]) useEffect(() => { const lowest = getLowestPrice() setLowestPrice(lowest) }, [getLowestPrice]) const [informationTab, setInformationTab] = useState(informationTabOptions[0].value) const variantQuantityRefs = useRef([]) const setVariantQuantityRef = (variantId) => (element) => { if (element) { let variantIndex = product.variants.findIndex((varian) => varian.id == variantId) product.variants[variantIndex].quantity = element.value } variantQuantityRefs.current[variantId] = element } const validQuantity = (quantity) => { let isValid = true if (!quantity || quantity < 1 || isNaN(parseInt(quantity))) { toast.error('Jumlah barang minimal 1') isValid = false } return isValid } const handleAddToCart = (variantId) => { if (!auth) { router.push(`/login?next=/shop/product/${slug}`) return } const quantity = variantQuantityRefs.current[variantId].value if (!validQuantity(quantity)) return if(product.variants.length > 1){ let variantIndex = product.variants.findIndex((varian) => varian.id == variantId) updateItemCart({ productId: variantId, quantity, programLineId: product.variants[variantIndex].programActive, selected: true }) }else{ updateItemCart({ productId: variantId, quantity, programLineId: promotionActiveId, selected: true }) } setAddCartAlert(true) } const handleBuy = (variant) => { const quantity = variantQuantityRefs.current[variant].value if (!validQuantity(quantity)) return router.push(`/shop/checkout?productId=${variant}&quantity=${quantity}`) } const variantSectionRef = useRef(null) const goToVariantSection = () => { if (variantSectionRef.current) { const position = variantSectionRef.current.getBoundingClientRect() window.scrollTo({ top: position.top - 120 + window.pageYOffset, behavior: 'smooth' }) } } const handlePromoClick = (variantId) => { setSelectVariantPromoActive(variantId) setPromotionType(true) } const productSimilarQuery = [ product?.name, `fq=-product_id_i:${product.id}`, `fq=-manufacture_id_i:${product.manufacture?.id || 0}` ].join('&') const [productSimilarInBrand, setProductSimilarInBrand] = useState(null) useEffect(() => { const loadProductSimilarInBrand = async () => { const productSimilarQuery = [product?.name, `fq=-product_id_i:${product.id}`].join('&') const dataProductSimilar = await productSimilarApi({ query: productSimilarQuery }) setProductSimilarInBrand(dataProductSimilar.products) } if (!productSimilarInBrand) loadProductSimilarInBrand() }, [product, productSimilarInBrand]) return (
{product.name}

{product?.name}

Nomor SKU
SKU-{product.id}
Part Number
{product.code || '-'}
Manufacture
{product.manufacture?.name ? ( {product.manufacture?.name} ) : (
-
)}
Berat Barang
{product?.weight > 0 && {product?.weight} KG} {product?.weight == 0 && ( Tanya Berat )}
{product.variants.length <= 1 && (
)}
Informasi Produk
{informationTabOptions.map((option) => ( setInformationTab(option.value)} > {option.label} ))}
Belum ada informasi.
{product.variants.length > 1 && product.lowestPrice.priceDiscount > 0 && (
Harga mulai dari:
)} {lowestPrice?.discountPercentage > 0 && (
{lowestPrice?.discountPercentage}%
{currencyFormat(lowestPrice?.price)}
)}

{lowestPrice?.priceDiscount > 0 ? ( currencyFormat(lowestPrice?.priceDiscount) ) : ( Hubungi kami untuk dapatkan harga terbaik,  klik disini )}

{product.variants.length > 1 ? ( ) : (
)}
Produk Serupa
{productSimilarInBrand && productSimilarInBrand?.map((product) => (
))}
{product.variants.length > 1 && (
Varian Produk
{product.variants.map((variant, index) => ( ))}
Part Number Varian Harga Jumlah Action
{variant.code} {variant.attributes.join(', ') || '-'} {variant.price.discountPercentage > 0 && variant.price.priceDiscount > 0 && ( <>
{currencyFormat(variant.price.price)}
{' '} )}
{variant.price.priceDiscount > 0 ? ( currencyFormat(variant.price.priceDiscount) ) : ( Call for price )}
{/* */}
{/* {variant.programActive ? ( handlePromoClick(variant.id)} className='cursor-pointer' > ) : ( variant.hasProgram ? (
handlePromoClick(variant.id)} className='cursor-pointer' >
):(
) )} */}
)}
Kamu Mungkin Juga Suka
setPromotionType(false)} >
setAddCartAlert(false)} >
{product.name}
{product.name}
Lihat Keranjang
Kamu Mungkin Juga Suka
) } const informationTabOptions = [ { value: 'description', label: 'Deskripsi' }, { value: 'information', label: 'Info Penting' } ] const TabButton = ({ children, active, ...props }) => { const activeClassName = active ? 'text-danger-500 underline underline-offset-4' : 'text-gray_r-12/80' return ( ) } const TabContent = ({ children, active, className = '', ...props }) => (
{children}
) export default ProductDesktop