From d22df6bd30b8ed4bcfa938dcbbedc5fc376c2304 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 20 Feb 2023 10:49:35 +0700 Subject: cart refactor --- src/lib/cart/components/Cart.jsx | 200 +++++++++++++++++++++++++++++++-- src/lib/cart/hooks/useCart.js | 4 +- src/lib/product/components/Product.jsx | 8 +- 3 files changed, 196 insertions(+), 16 deletions(-) (limited to 'src/lib') diff --git a/src/lib/cart/components/Cart.jsx b/src/lib/cart/components/Cart.jsx index 5f9ae1c0..3dd54429 100644 --- a/src/lib/cart/components/Cart.jsx +++ b/src/lib/cart/components/Cart.jsx @@ -1,28 +1,208 @@ 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 { getItemCart, updateItemCart } from "@/core/utils/cart" +import { CheckIcon, RectangleGroupIcon } from "@heroicons/react/24/outline" +import { createSlug } from "@/core/utils/slug" +import { useRouter } from "next/router" const Cart = () => { - const { cart } = useCart() + 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) + + 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) + } return ( -
-
+
+

Daftar Produk Belanja

Cari Produk Lain
-
- { cart.data?.map((product) => ( +
+ { products?.map((product) => (
-
- {product?.name} -
-
-
{ product?.parent?.name }
+ + + {product?.name} + +
+ + { product?.parent?.name } + +
{ product.code }
+ { 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: { currencyFormat(totalPriceBeforeTax - totalDiscountAmount + totalTaxAmount) } +
+
+
+ + +
+
) } diff --git a/src/lib/cart/hooks/useCart.js b/src/lib/cart/hooks/useCart.js index 44931b8a..9eb01e74 100644 --- a/src/lib/cart/hooks/useCart.js +++ b/src/lib/cart/hooks/useCart.js @@ -3,11 +3,11 @@ import { useQuery } from "react-query" import _ from "lodash" import CartApi from "../api/CartApi" -const useCart = () => { +const useCart = ({ enabled }) => { const cart = getCart() const variantIds = _.keys(cart).join(',') const fetchCart = async () => CartApi({ variantIds }) - const { data, isLoading } = useQuery('cart', fetchCart) + const { data, isLoading } = useQuery('cart', fetchCart, { enabled }) return { cart: { data, isLoading } diff --git a/src/lib/product/components/Product.jsx b/src/lib/product/components/Product.jsx index 396eba5f..39d29d4d 100644 --- a/src/lib/product/components/Product.jsx +++ b/src/lib/product/components/Product.jsx @@ -8,7 +8,7 @@ import Select from "react-select" import ProductSimilar from "./ProductSimilar" import LazyLoad from "react-lazy-load" import { toast } from "react-hot-toast" -import { addItemCart } from "@/core/utils/cart" +import { updateItemCart } from "@/core/utils/cart" const informationTabOptions = [ { value: 'specification', label: 'Spesifikasi' }, @@ -75,7 +75,7 @@ const Product = ({ product }) => { toast.error('Jumlah barang minimal 1') return } - addItemCart({ + updateItemCart({ productId: activeVariant.id, quantity }) @@ -98,7 +98,7 @@ const Product = ({ product }) => { { activeVariant?.price?.discountPercentage > 0 && (
- {currencyFormat(activeVariant?.price?.priceDiscount)} + {currencyFormat(activeVariant?.price?.price)}
{activeVariant?.price?.discountPercentage}% @@ -106,7 +106,7 @@ const Product = ({ product }) => {
) }

- { activeVariant?.price?.price > 0 ? currencyFormat(activeVariant?.price?.price) : ( + { activeVariant?.price?.priceDiscount > 0 ? currencyFormat(activeVariant?.price?.priceDiscount) : ( Hubungi kami untuk dapatkan harga terbaik,  klik disini -- cgit v1.2.3