summaryrefslogtreecommitdiff
path: root/src/lib/product/components/Product/Product.jsx
blob: 9521cbe4d6539e92095532bd1eb1cfba0ba6db80 (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
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'

const Product = ({ product }) => {
  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()
  }

  return (
    <>
      <ProductMobile product={product} wishlist={wishlist} toggleWishlist={toggleWishlist} />
      <ProductDesktop product={product} wishlist={wishlist} toggleWishlist={toggleWishlist} />
    </>
  )
}

export default Product