import Image from '@/core/components/elements/Image/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 odooApi from '@/core/api/odooApi' import PromotionType from '@/lib/promotinProgram/components/PromotionType' import useAuth from '@/core/hooks/useAuth' import ImageNext from 'next/image' import CountDown2 from '@/core/components/elements/CountDown/CountDown2' import { LazyLoadComponent } from 'react-lazy-load-image-component' import ColumnsSLA from './ColumnsSLA' import { useProductCartContext } from '@/contexts/ProductCartContext' import { Box, Skeleton, Tooltip } from '@chakra-ui/react' import { Info } from 'lucide-react' 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 [isLoadingSLA, setIsLoadingSLA] = useState(true) const [promotionType, setPromotionType] = useState(false) const [promotionActiveId, setPromotionActiveId] = useState(null) const [selectVariantPromoActive, setSelectVariantPromoActive] = useState(null) const [backgorundFlashSale, setBackgorundFlashSale] = useState(null) const { setRefreshCart, refreshCart } = useProductCartContext() 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]) useEffect(() => { const getBackgound = async () => { const get = await odooApi('GET', '/api/v1/banner?type=flash-sale-background-banner') setBackgorundFlashSale(get[0].image) } getBackgound() }, []) 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 updateCart = (variantId, quantity, source) => { let dataUpdate if (product.variants.length > 1) { let variantIndex = product.variants.findIndex((varian) => varian.id == variantId) dataUpdate = { productId: variantId, quantity, programLineId: product.variants[variantIndex].programActive, selected: true, source: source === 'buy' ? 'buy' : null } } else { dataUpdate = { productId: variantId, quantity, programLineId: promotionActiveId, selected: true, source: source === 'buy' ? 'buy' : null } } updateItemCart(dataUpdate) } const handleAddToCart = (variantId) => { if (!auth) { router.push(`/login?next=/shop/product/${slug}`) return } const quantity = variantQuantityRefs.current[variantId].value if (!validQuantity(quantity)) return let source = 'cart' updateCart(variantId, quantity, source) setRefreshCart(true) setAddCartAlert(true) } const handleQuantityChange = (variantId) => (event) => { const { value } = event.target const variantIndex = product.variants.findIndex((variant) => variant.id === variantId) if (variantIndex !== -1) { product.variants[variantIndex].quantity = parseInt(value, 10) // Pastikan untuk mengubah ke tipe number jika diperlukan // Lakukan sesuatu jika nilai quantity diubah } } const handleBuy = (variant) => { const quantity = variantQuantityRefs.current[variant].value if (!validQuantity(quantity)) return let source = 'buy' updateCart(variant, quantity, source) router.push(`/shop/checkout?source=buy`) } 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 source = 'right' const dataProductSimilar = await productSimilarApi({ query: productSimilarQuery, source }) setProductSimilarInBrand(dataProductSimilar.products) } if (!productSimilarInBrand) loadProductSimilarInBrand() }, [product, productSimilarInBrand]) useEffect(() => { const fetchData = async () => { const promises = product.variants.map(async (variant) => { const dataSLA = await odooApi('GET', `/api/v1/product_variant/${variant.id}/stock`) return { ...variant, sla: dataSLA } }) const variantData = await Promise.all(promises) product.variants = variantData setIsLoadingSLA(false) } if (product.variantTotal == 1) fetchData() }, [product]) return (
{product?.flashSale?.remainingTime > 0 && (
{product.lowestPrice.discountPercentage}%
{product?.flashSale?.tag != 'false' || product?.flashSale?.tag ? product?.flashSale?.tag : 'FLASH SALE'}
)} {product.name}

Keterangan : Gambar atau foto berperan sebagai ilustrasi produk. Kadang tidak sesuai dengan kondisi terbaru dengan berbagai perubahan dan perbaikan. Hubungi tim sales kami untuk informasi yang lebih baik perihal gambar di 021-2933 8828.

{product?.name}

Nomor SKU
SKU-{product.id}
Part Number
{product.code || '-'}
Manufacture
{product.manufacture?.name ? ( {product.manufacture?.name} ) : (
-
)}
Persiapan Barang
{product.variants.length > 1 && ( )} {product.variants.length === 1 && ( <> {!product.variants[0]?.sla && } {product.variants[0]?.sla && ( {product.variants[0]?.sla?.slaDate} )} )}
{product.variants.length === 1 && (
Stock
{!product.variants[0]?.sla && } {product?.variants[0].sla?.qty > 0 && ( {product?.variants[0].sla?.qty} )} {product?.variants[0].sla?.qty == 0 && ( Tanya Admin )}
)}
1 ? '' : 'bg-gray_r-4'} `}>
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)}
{product.flashSale.remainingTime > 0 && (
{product?.flashSale?.tag != 'false' || product?.flashSale?.tag ? product?.flashSale?.tag : 'FLASH SALE'}
)}
)}

{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 Stock Persiapan Barang Harga Jumlah Action
{variant.isFlashsale && ( 🗲 )} {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