From 927022b91855dba6d64365b68b4d20551256e7db Mon Sep 17 00:00:00 2001 From: "HATEC\\SPVDEV001" Date: Tue, 27 Jun 2023 16:47:16 +0700 Subject: product detail mobile & cart --- src/lib/cart/components/Cart.jsx | 173 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 160 insertions(+), 13 deletions(-) (limited to 'src/lib/cart/components') diff --git a/src/lib/cart/components/Cart.jsx b/src/lib/cart/components/Cart.jsx index cfbc3989..29bbf080 100644 --- a/src/lib/cart/components/Cart.jsx +++ b/src/lib/cart/components/Cart.jsx @@ -7,10 +7,7 @@ import { useEffect, useState } from 'react' import { addCart, deleteItemCart, - getCart, getCartApi, - getCartnew, - getItemCart, updateItemCart } from '@/core/utils/cart' import { CheckIcon, TrashIcon } from '@heroicons/react/24/outline' @@ -27,6 +24,8 @@ import productSearchApi from '@/lib/product/api/productSearchApi' import whatsappUrl from '@/core/utils/whatsappUrl' import useAuth from '@/core/hooks/useAuth' import LogoSpinner from '@/core/components/elements/Spinner/LogoSpinner' +import { getPromotionProgram } from '@/lib/promotinProgram/api/homepageApi' +import PromotionType from '@/lib/promotinProgram/components/PromotionType' const Cart = () => { const router = useRouter() @@ -50,7 +49,21 @@ const Cart = () => { useEffect(() => { if (cart) { - setProducts(cart.products) + const processProducts = async () => { + const updatedProducts = await Promise.all( + cart.products.map(async (product) => { + const id = product.id + const program = await getPromotionProgram({ id }) + const isPromo = program.length > 0 ? 1 : 0 + return { + ...product, + isPromo + } + }) + ) + setProducts(updatedProducts) + } + processProducts() setIsLoading(false) } }, [cart]) @@ -62,6 +75,10 @@ const Cart = () => { const [deleteConfirmation, setDeleteConfirmation] = useState(null) const [productRecomendation, setProductRecomendation] = useState(null) + const [promotionType, setPromotionType] = useState(null) + const [variantId, setVariantId] = useState(null) + const [quantity, setQuantity] = useState(null) + const [promotionActiveId, setPromotionActiveId] = useState(null) useEffect(() => { if (!products) return @@ -84,6 +101,18 @@ const Cart = () => { setTotalDiscountAmount(calculateTotalDiscountAmount) }, [products]) + useEffect(() => { + if(!promotionActiveId) return + + console.log('bajingan') + updateItemCart({ + productId: variantId, + quantity, + programLineId:promotionActiveId, + selected: true + }) + }, [promotionActiveId, variantId, quantity]) + useEffect(() => { const LoadProductSimilar = async () => { const randProductIndex = Math.floor(Math.random() * products.length) @@ -152,6 +181,13 @@ const Cart = () => { toast.success('Berhasil menghapus barang dari keranjang') } + const handlePopUpPromo = (variantId, quantity, promoId = null) => { + setPromotionType(true) + setVariantId(variantId) + setQuantity(quantity) + setPromotionActiveId(promoId) + } + return ( <> { + setPromotionType(false)} + > +
+ +
+
+
@@ -351,15 +404,61 @@ const Cart = () => { {products && products?.map((product) => ( <> - - -
-
Promo
-
Pilih Promo Yang Tersedia Bisa lebih Hemat
-
Cek Promo
-
- - + {product.isPromo > 0 && ( + + +
+ {product.program ? ( + <> +
+ + {product.program.type.label} + +
+
+ Anda Lebih hamat{' '} + + {' '} + {currencyFormat(product.program.price.priceDiscount)} + +
+ + ) : ( + <> +
+ Promo +
+
+ Pilih Promo Yang Tersedia Bisa lebih Hemat +
+ + )} +
handlePopUpPromo(product.id, product.quantity, product.program?.id)} + className='ml-auto text-red-500 flex gap-x-1 cursor-pointer' + > +
Cek Promo
+
+ +
+
+
+ + + )} {
+ {product.program && ( + + + +
+ {product.program.name} +
+
+
+
+ {product.program.type.label} +
+
{product.program.name}
+
+
+ + + + + + {product?.price?.discountPercentage > 0 && ( +
+
+ {currencyFormat(product?.price?.price)} +
+
+ )} +
+ {product?.program?.price.priceDiscount > 0 ? 'Gratis' : ''} +
+ + +
+ {product.program.price.priceDiscount > 0 ? 'Gratis' : ''} +
+ + + + )} ))} -- cgit v1.2.3