From 525166e62b793b351d1a6be2421c0a33b22f7b7b Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 20 Feb 2023 11:35:25 +0700 Subject: cart refactor --- src/lib/cart/components/Cart.jsx | 77 +++++++++++++++++++++++++++------- src/lib/product/components/Product.jsx | 2 +- 2 files changed, 64 insertions(+), 15 deletions(-) (limited to 'src/lib') diff --git a/src/lib/cart/components/Cart.jsx b/src/lib/cart/components/Cart.jsx index 3dd54429..69b4ded4 100644 --- a/src/lib/cart/components/Cart.jsx +++ b/src/lib/cart/components/Cart.jsx @@ -3,10 +3,12 @@ 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 { getItemCart, updateItemCart } from "@/core/utils/cart" -import { CheckIcon, RectangleGroupIcon } from "@heroicons/react/24/outline" +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" const Cart = () => { const router = useRouter() @@ -17,6 +19,8 @@ const Cart = () => { 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) => { @@ -98,6 +102,14 @@ const Cart = () => { 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 (
@@ -105,35 +117,38 @@ const Cart = () => {

Daftar Produk Belanja

Cari Produk Lain
+
{ products?.map((product) => ( -
+
{product?.name}
{ product?.parent?.name } -
{ product.code }
+
+ { product?.code } {product?.attributes.length > 0 ? `| ${product?.attributes.join(', ')}` : ''} +
{ product?.price?.discountPercentage > 0 && (
@@ -149,35 +164,43 @@ const Cart = () => {
- { currencyFormat(product?.price?.priceDiscount * product.quantity) } + { currencyFormat(product?.price?.priceDiscount * product?.quantity) }
updateQuantity(e.target.value, product.id)} - onBlur={(e) => updateQuantity(e.target.value, product.id, 'BLUR')} + type="number" + value={product?.quantity} + onChange={(e) => updateQuantity(e.target.value, product?.id)} + onBlur={(e) => updateQuantity(e.target.value, product?.id, 'BLUR')} /> +
)) }
+
@@ -203,6 +226,32 @@ const Cart = () => {
+ + setDeleteConfirmation(null)} + title="Hapus dari Keranjang" + > +
+ Apakah anda yakin menghapus barang {deleteConfirmation?.name} dari keranjang? +
+
+ + +
+
) } diff --git a/src/lib/product/components/Product.jsx b/src/lib/product/components/Product.jsx index 39d29d4d..92f4e37d 100644 --- a/src/lib/product/components/Product.jsx +++ b/src/lib/product/components/Product.jsx @@ -245,7 +245,7 @@ const Product = ({ product }) => { } const TabButton = ({ children, active, ...props }) => { - const activeClassName = active ? 'text-red_r-11 border-b border-red_r-11' : 'text-gray_r-11' + const activeClassName = active ? 'text-red_r-11 underline underline-offset-4' : 'text-gray_r-11' return (