import { Button, Skeleton } from '@chakra-ui/react'; import { HeartIcon } from '@heroicons/react/24/outline'; import { useRouter } from 'next/router'; import { useEffect, useState } from 'react'; import { toast } from 'react-hot-toast'; import LazyLoad from 'react-lazy-load'; import ImageNext from 'next/image'; import odooApi from '@/core/api/odooApi'; 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 BottomPopup from '@/core/components/elements/Popup/BottomPopup'; import MobileView from '@/core/components/views/MobileView'; import { updateItemCart } from '@/core/utils/cart'; import currencyFormat from '@/core/utils/currencyFormat'; import { gtagAddToCart } from '@/core/utils/googleTag'; import { createSlug } from '@/core/utils/slug'; import whatsappUrl from '@/core/utils/whatsappUrl'; import { getAuth } from '~/libs/auth'; import SimilarBottom from '~/modules/product-detail/components/SimilarBottom'; import ProductSimilar from '../ProductSimilar'; const ProductMobileVariant = ({ product, wishlist, toggleWishlist }) => { const router = useRouter(); const { slug } = router.query; const { srsltid } = router.query; let auth = getAuth(); const [quantity, setQuantity] = useState('1'); const [selectedVariant, setSelectedVariant] = useState(product.id); const [quantityInput, setQuantityInput] = useState(String(1)); const [qtyPickUp, setQtyPickUp] = useState(0); const [informationTab, setInformationTab] = useState( informationTabOptions[0].value ); const [addCartAlert, setAddCartAlert] = useState(false); const [isLoadingSLA, setIsLoadingSLA] = useState(true); const getLowestPrice = () => { const lowest = product.lowestPrice; return lowest; }; const [activeVariant, setActiveVariant] = useState({ id: null, code: product.code, name: product.name, price: getLowestPrice(), stock: product.stockTotal, weight: product.weight, isFlashSale: product.isFlashSale, }); useEffect(() => { if (selectedVariant) { setActiveVariant({ id: product.id, code: product.code, name: product.name, price: product.price, stock: product.stockTotal, weight: product.weight, isFlashSale: product.isFlashSale, }); } }, [selectedVariant, product]); const validAction = (q) => { if (!selectedVariant) { toast.error('Pilih varian terlebih dahulu'); return false; } if (!Number.isInteger(q) || q < 1) { toast.error('Jumlah barang minimal 1'); return false; } return true; }; const getQty = () => Math.max(1, toInt(quantityInput)); const handleClickCart = async () => { const q = getQty(); if (!auth) { router.push(`/login?next=/shop/product/${slug}?srsltid=${srsltid}`); return; } if (!validAction(q)) return; gtagAddToCart(activeVariant, q); updateItemCart({ productId: product.id, quantity: q, programLineId: null, selected: true, source: null, }); setAddCartAlert(true); }; const handleClickBuy = async () => { const q = getQty(); let isLoggedIn = typeof auth === 'object'; if (!isLoggedIn) { const currentUrl = encodeURIComponent(router.asPath); await router.push(`/login?next=${currentUrl}`); await new Promise((resolve) => { const t = setInterval(() => { const newAuth = getAuth(); if (typeof newAuth === 'object') { auth = newAuth; clearInterval(t); resolve(null); } }, 500); }); } if (!validAction(q)) return; updateItemCart({ productId: product.id, quantity: q, programLineId: null, selected: true, source: 'buy', }); router.push(`/shop/checkout?source=buy`); }; const toInt = (v) => { const n = parseInt(String(v ?? '').trim(), 10); return Number.isFinite(n) ? n : 0; }; const validQuantity = (q) => { if (!Number.isInteger(q) || q < 1) { toast.error('Jumlah barang minimal 1'); return false; } return true; }; const handleButton = (variant) => { const quantity = Math.max(1, toInt(quantityInput)); // clamp min 1 if (!validQuantity(quantity)) return; updateItemCart({ productId: variant, quantity, programLineId: null, selected: true, source: 'buy', }); router.push('/shop/quotation?source=buy'); }; const productSimilarQuery = [ product?.name, `fq=-product_id_i:${product.id}`, `fq=-manufacture_id_i:${product.manufacture?.id || 0}`, ].join('&'); useEffect(() => { const fetchData = async () => { const dataSLA = await odooApi( 'GET', `/api/v1/product_variant/${product.id}/stock` ); product.sla = dataSLA; setIsLoadingSLA(false); }; fetchData(); }, [product]); const [fakeStock] = useState(() => { // inisialisasi sekali doang pas pertama kali komponen dibuat return Math.floor(Math.random() * 100) + 1; }); const getImageVariant = (img) => { if (!img || img.trim() === '') return '/images/noimage.jpeg'; return `${img}?variant=True`; }; return (
{!product.price.price > 0 && (
Produk tidak tersedia
)}
{/* ===== BAR BAWAH ===== */}
{/* HARGA & PPN */} {activeVariant.isFlashSale && activeVariant?.price?.discountPercentage > 0 ? ( <>
{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 )}
) : null}
)}
{product?.price?.price > 0 && (
{/* Stock : {activeVariant?.stock ?? 0} */} Stock : {fakeStock}{' '}
{qtyPickUp > 0 && (
* {qtyPickUp} barang bisa di pickup
)}
)}
{product?.price?.price > 0 && (
setQuantityInput(e.target.value)} className='h-10 w-16 text-center text-lg outline-none border-x [appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none' />
)}
{/* TOMBOL AKSI */}
{/* Tombol Dokumen */} {/* Container untuk tombol aksi utama */}
{product.name}
{product.manufacture?.name ? ( {product.manufacture?.name} ) : (
-
)}

{activeVariant?.name}

Informasi Produk

{informationTabOptions.map((option) => ( setInformationTab(option.value)} > {option.label} ))}
{(!product?.price?.price || product?.price?.price <= 0) && ( {isLoadingSLA ? ( ) : product?.sla?.slaDate != '-' ? ( ) : ( '-' )} )} SKU-{product?.id} {activeVariant?.code || '-'} {product?.price?.price > 0 && ( {activeVariant?.stock > 0 && (
Ready Stock
{activeVariant?.stock > 5 ? '> 5' : '< 5'}
)} {activeVariant?.stock == 0 && ( Tanya Stok )}
)} {activeVariant?.weight > -1 && ( {activeVariant?.weight} KG )} {activeVariant?.weight == -1 && ( 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 ProductMobileVariant;