blob: 6e983c2ed3e01e5bf6aa530b016fb076eed50e08 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
import { toast } from 'react-hot-toast'
import useWishlist from '@/lib/wishlist/hooks/useWishlist'
import createOrDeleteWishlistApi from '@/lib/wishlist/api/createOrDeleteWishlistApi'
import ProductDesktop from './ProductDesktop'
import useAuth from '@/core/hooks/useAuth'
import ProductMobile from './ProductMobile'
import { useRouter } from 'next/router'
import { useEffect } from 'react'
import { gtagViewItem } from '@/core/utils/googleTag'
import ProductDesktopVariant from './ProductDesktopVariant'
import ProductMobileVariant from './ProductMobileVariant'
const Product = ({ product, isVariant = false }) => {
const auth = useAuth()
const router = useRouter()
const { wishlist } = useWishlist({ productId: product?.id })
const toggleWishlist = async () => {
if (!auth) {
router.push('/login')
return
}
const data = { product_id: product.id }
await createOrDeleteWishlistApi({ data })
if (wishlist?.data?.productTotal > 0) {
toast.success('Berhasil menghapus dari wishlist')
} else {
toast.success('Berhasil menambahkan ke wishlist')
}
wishlist.refetch()
}
useEffect(() => {
if (isVariant == false) {
gtagViewItem(product.variants)
}
}, [product, isVariant])
return isVariant == true ? (
<>
<ProductDesktopVariant
product={product}
wishlist={wishlist}
toggleWishlist={toggleWishlist}
/>
<ProductMobileVariant product={product} wishlist={wishlist} toggleWishlist={toggleWishlist} />
</>
) : (
<>
<ProductMobile product={product} wishlist={wishlist} toggleWishlist={toggleWishlist} />
<ProductDesktop products={product} wishlist={wishlist} toggleWishlist={toggleWishlist} />
</>
)
}
export default Product
|