summaryrefslogtreecommitdiff
path: root/src/lib/cart/components/Cartheader.jsx
blob: 19f79bc9808ba1da0450139126c1485a5e028148 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { useCallback, useEffect, useMemo, useState } from 'react'
import { getCartApi } from '../api/CartApi'
import useAuth from '@/core/hooks/useAuth'
import { useRouter } from 'next/router'
import odooApi from '@/core/api/odooApi'
import { useProductCartContext } from '@/contexts/ProductCartContext'

const { ShoppingCartIcon, PhotoIcon } = require('@heroicons/react/24/outline')
const { default: Link } = require('next/link')

const Cardheader = (cartCount) => {
  const router = useRouter()
  const [subTotal, setSubTotal] = useState(null)
  const [buttonLoading, SetButtonTerapkan] = useState(false)
  const itemLoading = [1, 2, 3]
  const auth = useAuth()
  const [countCart, setCountCart] = useState(null)
  const { productCart, setRefreshCart, setProductCart, refreshCart, isLoading, setIsloading } =
    useProductCartContext()

  const [isHovered, setIsHovered] = useState(false)

  const products = useMemo(() => {
    return productCart?.products || []
  }, [productCart])

  const handleMouseEnter = () => {
    setIsHovered(true)
    getCart()
  }

  const handleMouseLeave = () => {
    setIsHovered(false)
  }

  const getCart = () => {
    if (!productCart && auth) {
      refreshCartf()
    }
  }
  const refreshCartf = useCallback(async () => {
    setIsloading(true)
    let cart = await getCartApi()
    setProductCart(cart)
    setCountCart(cart.productTotal)
    setIsloading(false)
  }, [setProductCart, setIsloading])

  useEffect(() => {
    if (!products) return

    let calculateTotalPriceBeforeTax = 0
    let calculateTotalTaxAmount = 0
    let calculateTotalDiscountAmount = 0
    for (const product of products) {
      if (product.quantity == '') continue

      let priceBeforeTax = product.price.price / 1.11
      calculateTotalPriceBeforeTax += priceBeforeTax * product.quantity
      calculateTotalTaxAmount += (product.price.price - priceBeforeTax) * product.quantity
      calculateTotalDiscountAmount +=
        (product.price.price - product.price.priceDiscount) * product.quantity
    }
    let subTotal =
      calculateTotalPriceBeforeTax - calculateTotalDiscountAmount + calculateTotalTaxAmount
    setSubTotal(subTotal)
  }, [products])

  useEffect(() => {
    if (refreshCart) {
      refreshCartf()
    }
    setRefreshCart(false)
  }, [refreshCart, refreshCartf, setRefreshCart])

  useEffect(() => {
    setCountCart(cartCount.cartCount)
  }, [cartCount])

  const handleCheckout = async () => {
    SetButtonTerapkan(true)
    let checkoutAll = await odooApi('POST', `/api/v1/user/${auth.id}/cart/select-all`)
    router.push('/shop/checkout')
  }

  return (
    <div className='relative group'>
      <div>
        <Link
          href='/shop/cart'
          target='_blank'
          rel='noreferrer'
          className='flex items-center gap-x-2 !text-gray_r-12/80'
          onMouseEnter={handleMouseEnter}
          onMouseLeave={handleMouseLeave}
        >
          <div className={`relative ${countCart > 0 && 'mr-2'}`}>
            <ShoppingCartIcon className='w-7' />
            {countCart > 0 && (
              <span className='absolute -top-2 -right-2 badge-solid-red rounded-full w-5 h-5 flex items-center justify-center'>
                {countCart}
              </span>
            )}
          </div>
          <span>
            Keranjang
            <br />
            Belanja
          </span>
        </Link>
      </div>
    </div>
  )
}

export default Cardheader