import Divider from '@/core/components/elements/Divider/Divider'; import Image from '@/core/components/elements/Image/Image'; import Link from '@/core/components/elements/Link/Link'; import currencyFormat from '@/core/utils/currencyFormat'; import { useEffect, useState } from 'react'; import Select from 'react-select'; import ProductSimilar from '../ProductSimilar'; import LazyLoad from 'react-lazy-load'; import { updateItemCart } from '@/core/utils/cart'; import { HeartIcon } from '@heroicons/react/24/outline'; import { useRouter } from 'next/router'; import MobileView from '@/core/components/views/MobileView'; import { toast } from 'react-hot-toast'; import { createSlug } from '@/core/utils/slug'; import BottomPopup from '@/core/components/elements/Popup/BottomPopup'; import whatsappUrl from '@/core/utils/whatsappUrl'; import PromotionType from '@/lib/promotinProgram/components/PromotionType'; import { gtagAddToCart } from '@/core/utils/googleTag'; import odooApi from '@/core/api/odooApi'; import ImageNext from 'next/image'; import CountDown2 from '@/core/components/elements/CountDown/CountDown2'; import Breadcrumb from './Breadcrumb'; import useAuth from '@/core/hooks/useAuth'; import { sellingProductFormat } from '@/core/utils/formatValue'; import ProductPromoSection from '~/modules/product-promo/components/Section'; const ProductMobile = ({ product, wishlist, toggleWishlist }) => { const router = useRouter(); const auth = useAuth(); const { slug } = router.query; const [quantity, setQuantity] = useState('1'); const [selectedVariant, setSelectedVariant] = useState(null); const [informationTab, setInformationTab] = useState( informationTabOptions[0].value ); const [addCartAlert, setAddCartAlert] = useState(false); const [isLoadingSLA, setIsLoadingSLA] = useState(true); const [promotionType, setPromotionType] = useState(false); const [promotionActiveId, setPromotionActiveId] = useState(null); const [backgorundFlashSale, setBackgorundFlashSale] = useState(null); const getLowestPrice = () => { 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; }; useEffect(() => { const getBackgound = async () => { const get = await odooApi( 'GET', '/api/v1/banner?type=flash-sale-background-banner' ); if (get.length > 0) { setBackgorundFlashSale(get[0].image); } }; getBackgound(); }, []); const [activeVariant, setActiveVariant] = useState({ id: null, code: product.code, name: product.name, price: getLowestPrice(), stock: product.stockTotal, weight: product.weight, hasProgram: false, qtySold: product.qtySold, }); const variantOptions = product.variants?.map((variant) => { let label = []; if (variant.isFlashsale) { label.push("🗲"); } if (variant.code) { label.push(`[${variant.code}]`); } if (variant.attributes.length > 0) { label.push(variant.attributes.join(', ')); } else { label.push(product.name); } return { value: variant.id, label: label.join(' '), }; }); useEffect(() => { if (!selectedVariant && variantOptions.length == 1) { const selectedVariant = variantOptions?.find((variant) => variant.is_in_bu) || variantOptions?.[0]; setSelectedVariant(selectedVariant); // setSelectedVariant(variantOptions[0]); } }, [selectedVariant, variantOptions]); useEffect(() => { if (selectedVariant) { const variant = product.variants.find( (variant) => variant.id == selectedVariant.value ); const variantAttributes = variant.attributes.length > 0 ? ' - ' + variant.attributes.join(', ') : ''; const newActiveVariant = { id: variant.id, code: variant.code, name: variant.parent.name + variantAttributes, price: variant.price, stock: variant.stock, weight: variant.weight, hasProgram: variant.hasProgram, isFlashsale: variant.isFlashsale, qtySold: variant.qtySold, }; setActiveVariant(newActiveVariant); const fetchSLA = async () => { const dataSLA = await odooApi( 'GET', `/api/v1/product_variant/${variant.id}/stock` ); setActiveVariant({ ...newActiveVariant, sla: dataSLA }); }; fetchSLA(); } }, [selectedVariant, product]); const validAction = () => { let isValid = true; if (!selectedVariant) { toast.error('Pilih varian terlebih dahulu'); isValid = false; } if (!quantity || quantity < 1 || isNaN(parseInt(quantity))) { toast.error('Jumlah barang minimal 1'); isValid = false; } return isValid; }; const redirectToLogin = (action) => { const nextURL = `/shop/product/${slug}?action=${action}&variantId=${activeVariant.id}&qty=${quantity}`; router.push(`/login?next=${encodeURIComponent(nextURL)}`); return true; }; const handleClickCart = () => { if (!validAction()) return; gtagAddToCart(activeVariant, quantity); if (!auth) { return redirectToLogin('add_to_cart'); } updateItemCart({ productId: activeVariant.id, quantity, programLineId: promotionActiveId, selected: true, }); setAddCartAlert(true); }; const handleClickBuy = () => { if (!validAction()) return; if (!auth) { return redirectToLogin('buy'); } updateItemCart({ productId: activeVariant.id, quantity, programLineId: promotionActiveId, selected: true, source: 'buy', }); router.push(`/shop/checkout?source=buy`); }; const productSimilarQuery = [ product?.name, `fq=-product_id_i:${product.id}`, `fq=-manufacture_id_i:${product.manufacture?.id || 0}`, ].join('&'); return (
{product?.flashSale?.remainingTime > 0 && activeVariant?.price.discountPercentage > 0 && (
{Math.floor(product.lowestPrice.discountPercentage)}%
{product?.flashSale?.tag != 'false' || product?.flashSale?.tag ? product?.flashSale?.tag : 'FLASH SALE'}
)} {product.name}
{product.manufacture?.name ? ( {product.manufacture?.name} ) : (
-
)}

{activeVariant?.name}

{product?.qtySold > 0 && (
{sellingProductFormat(activeVariant?.qtySold) + ' Terjual'}
)} {product.variants.length > 1 && activeVariant.price.priceDiscount > 0 && !selectedVariant && (
Harga mulai dari:{' '}
)} {activeVariant.isFlashsale && activeVariant?.price?.discountPercentage > 0 ? ( <>
{Math.floor(activeVariant?.price?.discountPercentage)}%
{currencyFormat(activeVariant?.price?.price)}
{currencyFormat(activeVariant?.price?.priceDiscount)}
Termasuk PPN:{' '} {currencyFormat( activeVariant?.price.priceDiscount * process.env.NEXT_PUBLIC_PPN )}
) : (

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

)}
setQuantity(e.target.value)} />

Informasi Produk

{informationTabOptions.map((option) => ( setInformationTab(option.value)} > {option.label} ))}
{selectedVariant ? ( activeVariant?.sla?.slaDate != '-' ? ( ) : ( '-' ) ) : ( '-' )} {product?.variantTotal} Varian SKU-{product?.id} {activeVariant?.code || '-'} {activeVariant?.sla?.qty > 0 && (
Ready Stock
{activeVariant?.sla?.qty}
)} {activeVariant?.sla?.qty == 0 && ( Tanya Stok )}
{activeVariant?.weight > 0 && ( {activeVariant?.weight} KG )} {activeVariant?.weight == 0 && ( Tanya Berat )}

Kamu Mungkin Juga Suka

setAddCartAlert(false)} >
{product.name}
{product.name}
Lihat Keranjang
Kamu Mungkin Juga Suka
); }; const informationTabOptions = [ { value: 'specification', label: 'Spesifikasi' }, { 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-11'; return ( ); }; const TabContent = ({ children, active, className, ...props }) => (
{children}
); const SpecificationContent = ({ children, label }) => (
{label} {children}
); export default ProductMobile;