From 4f285349adec93a040e41d1d8c0c1c7b57749397 Mon Sep 17 00:00:00 2001 From: "HATEC\\SPVDEV001" Date: Thu, 21 Sep 2023 14:56:35 +0700 Subject: add context for list product popup cart --- src/lib/cart/components/Cartheader.jsx | 309 +++++++++++++++++++++++---------- 1 file changed, 219 insertions(+), 90 deletions(-) (limited to 'src/lib/cart') diff --git a/src/lib/cart/components/Cartheader.jsx b/src/lib/cart/components/Cartheader.jsx index 1760360d..3fc959d4 100644 --- a/src/lib/cart/components/Cartheader.jsx +++ b/src/lib/cart/components/Cartheader.jsx @@ -1,49 +1,128 @@ -import { useEffect, useState } from 'react' +import { useCallback, useEffect, useMemo, useState } from 'react' import { getCartApi } from '../api/CartApi' import currencyFormat from '@/core/utils/currencyFormat' import Image from '@/core/components/elements/Image/Image' import { createSlug } from '@/core/utils/slug' +import useAuth from '@/core/hooks/useAuth' +import { useRouter } from 'next/router' +import odooApi from '@/core/api/odooApi' +import { useProductCartContext } from '@/contexts/ProductCartContext' -const { ShoppingCartIcon } = require('@heroicons/react/24/outline') +const { ShoppingCartIcon, PhotoIcon } = require('@heroicons/react/24/outline') const { default: Link } = require('next/link') const Cardheader = (cartCount) => { - const [count, setCardCount] = useState(null) - const [products, setProducts] = useState(null) + 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 getCart = async () => { - const listCart = await getCartApi() - setProducts(listCart.products) + 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]) useEffect(() => { - getCart() + 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) }, []) + const handleCheckout = async () => { + SetButtonTerapkan(true) + let checkoutAll = await odooApi('POST', `/api/v1/user/${auth.id}/cart/select-all`) + router.push('/shop/checkout') + } + return (
- + +
0 && 'mr-2'}`}> + + {countCart > 0 && ( + + {countCart} + + )} +
+ + Keranjang +
+ Belanja +
+ +
+
+
-
0 && 'mr-2'}`}> - - {cartCount.cartCount > 0 && ( - - {cartCount.cartCount} - - )} -
- - Keranjang -
- Belanja -
- - {/*
*/} -
Keranjang Belanja
@@ -53,71 +132,121 @@ const Cardheader = (cartCount) => {

-
    - {products && - products?.map((product, index) => ( - <> -
  • -
    -
    - - {product?.name} - -
    -
    - - {' '} -

    - {product.parent.name} -

    - - - {product?.price?.discountPercentage > 0 && ( -
    -
    - {product?.price?.discountPercentage}% -
    -
    - {currencyFormat(product?.price?.price)} -
    + {!auth && ( +
    +

    + Silahkan{' '} + + Login + {' '} + Untuk Melihat Daftar Keranjang Belanja Anda +

    +
    + )} + {isLoading && + itemLoading.map((item) => ( +
    +
    +
    + +
    +
    +
    +
    +
    +
    +
    +
    + ))} + {products.length === 0 && !isLoading && ( +
    +

    + Tidak Ada Produk di Keranjang Belanja Anda +

    +
    + )} + {products.length > 0 && !isLoading && ( + <> +
      + {products && + products?.map((product, index) => ( + <> +
    • +
      +
      + + {product?.name} +
      - )} -
      -
      - {currencyFormat(product?.price?.priceDiscount)} +
      + + {' '} +

      + {product.parent.name} +

      + + + {product?.price?.discountPercentage > 0 && ( +
      +
      + {product?.price?.discountPercentage}% +
      +
      + {currencyFormat(product?.price?.price)} +
      +
      + )} +
      +
      + {currencyFormat(product?.price?.priceDiscount)} +
      +
      -
      -
      -
    • - - ))} -
    -
    +
  • + + ))} +
+
+ + )}
- {/*
- - Buka Keranjang Belanja - -
*/} + {products.length > 0 && !isLoading && ( + <> +
+ Sub Total Sebelum PPN : + Rp. {currencyFormat(subTotal)} +
+
+ +
+ + )}
-- cgit v1.2.3