blob: 54490c26dbcb33512de1e642dfe27c09a7a6c8e0 (
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
57
58
59
60
61
62
63
64
|
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])
if (isVariant == true) {
return (
<>
<ProductDesktopVariant
product={product}
wishlist={wishlist}
toggleWishlist={toggleWishlist}
/>
<ProductMobileVariant
product={product}
wishlist={wishlist}
toggleWishlist={toggleWishlist}
/>
</>
)
} else {
return (
<>
<ProductMobile product={product} wishlist={wishlist} toggleWishlist={toggleWishlist} />
<ProductDesktop products={product} wishlist={wishlist} toggleWishlist={toggleWishlist} />
</>
)
}
}
export default Product
|