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 { gtagAddToCart } from '@/core/utils/googleTag'
import odooApi from '@/core/api/odooApi'
import { Skeleton } from '@chakra-ui/react'
const ProductMobileVariant = ({ product, wishlist, toggleWishlist }) => {
const router = useRouter()
const [quantity, setQuantity] = useState('1')
const [selectedVariant, setSelectedVariant] = useState(product.id)
const [informationTab, setInformationTab] = useState(informationTabOptions[0].value)
const [addCartAlert, setAddCartAlert] = useState(false)
const [isLoadingSLA, setIsLoadingSLA] = useState(true)
const getLowestPrice = () => {
const lowest = product.price
return lowest
}
const [activeVariant, setActiveVariant] = useState({
id: null,
code: product.code,
name: product.name,
price: getLowestPrice(),
stock: product.stockTotal,
weight: product.weight
})
useEffect(() => {
if (selectedVariant) {
setActiveVariant({
id: product.id,
code: product.code,
name: product.name,
price: product.price,
stock: product.stock,
weight: product.weight
})
}
}, [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 handleClickCart = () => {
if (!validAction()) return
gtagAddToCart(activeVariant, quantity)
updateItemCart({
productId: variant,
quantity,
programLineId: null,
selected: true,
source: null
})
setAddCartAlert(true)
}
const handleClickBuy = () => {
if (!validAction()) return
updateItemCart({
productId: product.id,
quantity,
programLineId: null,
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('&')
useEffect(() => {
const fetchData = async () => {
const dataSLA = await odooApi('GET', `/api/v1/product_variant/${product.id}/stock`)
product.sla = dataSLA
setIsLoadingSLA(false)
}
fetchData()
}, [product])
return (
{activeVariant?.name}
{activeVariant?.price?.priceDiscount > 0 ? (
<>
{currencyFormat(activeVariant?.price?.priceDiscount * 1.11)}
Informasi Produk
Kamu Mungkin Juga Suka