import Link from "@/core/components/elements/Link/Link" import useCart from "../hooks/useCart" import Image from "@/core/components/elements/Image/Image" import currencyFormat from "@/core/utils/currencyFormat" import { useEffect, useState } from "react" import { deleteItemCart, getItemCart, updateItemCart } from "@/core/utils/cart" import { CheckIcon, RectangleGroupIcon, TrashIcon } from "@heroicons/react/24/outline" import { createSlug } from "@/core/utils/slug" import { useRouter } from "next/router" import BottomPopup from "@/core/components/elements/Popup/BottomPopup" import { toast } from "react-hot-toast" import Spinner from "@/core/components/elements/Spinner/Spinner" const Cart = () => { const router = useRouter() const [ products, setProducts ] = useState(null) const { cart } = useCart({ enabled: !products }) const [ totalPriceBeforeTax, setTotalPriceBeforeTax ] = useState(0) const [ totalTaxAmount, setTotalTaxAmount ] = useState(0) const [ totalDiscountAmount, setTotalDiscountAmount ] = useState(0) const [ deleteConfirmation, setDeleteConfirmation ] = useState(null) useEffect(() => { if (cart.data && !products) { const productsWithQuantity = cart.data.map((product) => { const productInCart = getItemCart({ productId: product.id }) if (!productInCart) return return { ...product, quantity: productInCart.quantity, selected: productInCart.selected } }) setProducts(productsWithQuantity) } }, [cart, products]) useEffect(() => { if (!products) return let calculateTotalPriceBeforeTax = 0 let calculateTotalTaxAmount = 0 let calculateTotalDiscountAmount = 0 for (const product of products) { if (product.quantity == '') continue updateItemCart({ productId: product.id, quantity: product.quantity, selected: product.selected }) if (!product.selected) 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 } setTotalPriceBeforeTax(calculateTotalPriceBeforeTax) setTotalTaxAmount(calculateTotalTaxAmount) setTotalDiscountAmount(calculateTotalDiscountAmount) }, [products]) const updateQuantity = (value, productId, operation = '') => { let productIndex = products.findIndex((product) => product.id == productId) if (productIndex < 0) return let productsToUpdate = products let quantity = productsToUpdate[productIndex].quantity if (value != '' && isNaN(parseInt(value))) return value = value != '' ? parseInt(value) : '' switch (operation) { case 'PLUS': quantity += value break case 'MINUS': if ((quantity - value) < 1) return quantity -= value break case 'BLUR': if (value != '') return quantity = 1 break default: quantity = value break } productsToUpdate[productIndex].quantity = quantity setProducts([ ...productsToUpdate ]) } const toggleSelected = (productId) => { let productIndex = products.findIndex((product) => product.id == productId) if (productIndex < 0) return let productsToUpdate = products productsToUpdate[productIndex].selected = !productsToUpdate[productIndex].selected setProducts([ ...productsToUpdate ]) } const selectedProduct = () => { if (!products) return [] return products.filter((product) => product.selected == true) } const deleteProduct = (productId) => { const productsToUpdate = products.filter((product) => product.id != productId) deleteItemCart({ productId }) setDeleteConfirmation(null) setProducts([...productsToUpdate]) toast.success('Berhasil menghapus barang dari keranjang') } return (

Daftar Produk Belanja

Cari Produk Lain
{ cart.isLoading && (
) } { products?.map((product) => (
{product?.name}
{ product?.parent?.name }
{ product?.code } {product?.attributes.length > 0 ? `| ${product?.attributes.join(', ')}` : ''}
{ product?.price?.discountPercentage > 0 && (
{currencyFormat(product?.price?.price)}
{product?.price?.discountPercentage}%
) }
{ currencyFormat(product?.price?.priceDiscount) }
{ currencyFormat(product?.price?.priceDiscount * product?.quantity) }
updateQuantity(e.target.value, product?.id)} onBlur={(e) => updateQuantity(e.target.value, product?.id, 'BLUR')} />
)) }
Total: { selectedProduct().length > 0 ? currencyFormat(totalPriceBeforeTax - totalDiscountAmount + totalTaxAmount) : ' - ' }
setDeleteConfirmation(null)} title="Hapus dari Keranjang" >
Apakah anda yakin menghapus barang {deleteConfirmation?.name} dari keranjang?
) } export default Cart