import { useEffect, useRef, useState } from 'react'; import ImageNext from 'next/image'; import { LazyLoadComponent } from 'react-lazy-load-image-component'; import { Box, Skeleton, Tooltip } from '@chakra-ui/react'; import { HeartIcon } from '@heroicons/react/24/outline'; import { Info } from 'lucide-react'; import LazyLoad from 'react-lazy-load'; import { toast } from 'react-hot-toast'; import { useRouter } from 'next/router'; import Image from '@/core/components/elements/Image/Image'; import Link from '@/core/components/elements/Link/Link'; import DesktopView from '@/core/components/views/DesktopView'; import BottomPopup from '@/core/components/elements/Popup/BottomPopup'; import CountDown2 from '@/core/components/elements/CountDown/CountDown2'; import currencyFormat from '@/core/utils/currencyFormat'; import { updateItemCart } from '@/core/utils/cart'; import { createSlug } from '@/core/utils/slug'; import whatsappUrl from '@/core/utils/whatsappUrl'; import { sellingProductFormat } from '@/core/utils/formatValue'; import odooApi from '@/core/api/odooApi'; import useAuth from '@/core/hooks/useAuth'; import { useProductCartContext } from '@/contexts/ProductCartContext'; import PromotionType from '@/lib/promotinProgram/components/PromotionType'; import ProductSimilar from '../ProductSimilar'; import ProductCard from '../ProductCard'; import productSimilarApi from '../../api/productSimilarApi'; import ColumnsSLA from './ColumnsSLA'; import Breadcrumb from './Breadcrumb'; import ProductPromoSection from '~/modules/product-promo/components/Section'; 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(); useEffect(() => { setLowestPrice({ price: product?.lowestPrice }); }, [product]); 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 = { productId: variantId, quantity, selected: true, source: source === 'buy' ? 'buy' : null, }; if (product.variants.length > 1) { let variantIndex = product.variants.findIndex( (varian) => varian.id == variantId ); dataUpdate['programLineId'] = product.variants[variantIndex].programActive; } else { dataUpdate['programLineId'] = promotionActiveId; } updateItemCart(dataUpdate); }; const redirectToLogin = (action, variantId, quantity) => { const nextURL = `/shop/product/${slug}?action=${action}&variantId=${variantId}&qty=${quantity}`; router.push(`/login?next=${encodeURIComponent(nextURL)}`); return true; }; const handleAddToCart = (variantId) => { const quantity = variantQuantityRefs.current[variantId].value; if (!validQuantity(quantity)) return; if (!auth) { return redirectToLogin('add_to_cart', variantId, quantity); } 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; if (!auth) { return redirectToLogin('buy', variant, quantity); } 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 && lowestPrice?.price.discountPercentage > 0 && (
{Math.floor(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 && (
)}
{product.variants.length > 1 && product.lowestPrice.priceDiscount > 0 && (
Harga mulai dari:
)} {/* {lowestPrice?.discountPercentage > 0 && (
{lowestPrice?.discountPercentage}%
Include PPN {currencyFormat(lowestPrice?.price * process.env.NEXT_PUBLIC_PPN)}
{product.flashSale.remainingTime > 0 && (
{product?.flashSale?.tag != 'false' || product?.flashSale?.tag ? product?.flashSale?.tag : 'FLASH SALE'}
)}
)} */} {product?.qtySold > 0 && (
{sellingProductFormat(product?.qtySold) + ' Terjual'}
)} {product?.flashSale?.id && lowestPrice?.price.discountPercentage > 0 ? ( <>
{Math.floor(lowestPrice?.price?.discountPercentage)}%
{currencyFormat(lowestPrice?.price?.price)}
{currencyFormat(lowestPrice?.price?.priceDiscount)}
Termasuk PPN:{' '} {currencyFormat( lowestPrice?.price.priceDiscount * process.env.NEXT_PUBLIC_PPN )}
) : (

{lowestPrice?.price.price > 0 ? ( <> {currencyFormat(lowestPrice?.price.price)}
Termasuk PPN:{' '} {currencyFormat( lowestPrice?.price.price * process.env.NEXT_PUBLIC_PPN )}
) : ( Hubungi kami untuk dapatkan harga terbaik,  klik disini )}

)} {product.variants.length > 1 ? ( ) : (
)}
Produk Serupa
{productSimilarInBrand && productSimilarInBrand?.map((product) => (
))}
Informasi Produk
{informationTabOptions.map((option) => ( setInformationTab(option.value)} > {option.label} ))}
Belum ada informasi.
{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.isFlashsale && variant?.price?.discountPercentage > 0 ? ( <>
{Math.floor(variant?.price?.discountPercentage)} %
{currencyFormat(variant?.price?.price)}
{' '}
{currencyFormat(variant?.price?.priceDiscount)}
{' '}
Inc. PPN:{' '} {currencyFormat( variant.price.priceDiscount * process.env.NEXT_PUBLIC_PPN )}
) : (
{variant?.price?.price > 0 ? ( <>
{currencyFormat(variant?.price?.price)}
{' '}
Inc. PPN:{' '} {currencyFormat( variant?.price?.price * process.env.NEXT_PUBLIC_PPN )}
) : ( Call for Inquiry )}
)} {/* {variant.price.discountPercentage > 0 && variant.price.priceDiscount > 0 && ( <>
{currencyFormat(variant.price.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;