From c4ff871551cfbf3ed759b3d7be735d7039edb6f5 Mon Sep 17 00:00:00 2001 From: "HATEC\\SPVDEV001" Date: Fri, 30 Jun 2023 14:58:05 +0700 Subject: cart --- src/core/utils/cart.js | 14 ++-- src/lib/cart/components/Cart.jsx | 83 +++++++++++++--------- .../promotinProgram/components/PromotionType.jsx | 1 - 3 files changed, 57 insertions(+), 41 deletions(-) diff --git a/src/core/utils/cart.js b/src/core/utils/cart.js index eb411dd3..16befdf7 100644 --- a/src/core/utils/cart.js +++ b/src/core/utils/cart.js @@ -35,18 +35,18 @@ const addCart = async (product_id, qty, selected, programLineId = null) => { 'product_id' : product_id, 'qty' : qty, 'selected' : selected, + 'program_line_id' : programLineId } - if(programLineId){ - data += { - 'program_line_id' : programLineId, - } - } + const id = getAuth()?.id const cartAdd = await odooApi( 'POST', `/api/v1/user/${id}/cart/create-or-update`, data ) + + return true + } const getCartApi = async () => { @@ -94,12 +94,12 @@ const getItemCart = ({ productId }) => { * @param {boolean} [options.selected=false] - The new selected status of the product in the cart. Default is `false`. * @returns {boolean} - Returns `true`. */ -const updateItemCart = ({ productId, quantity, selected = false , programLineId}) => { +const updateItemCart = async ({ productId, quantity, selected = false , programLineId}) => { let cart = getCart() quantity = parseInt(quantity) cart[productId] = { productId, quantity, selected } setCart(cart) - addCart(productId, quantity, selected, programLineId) + await addCart(productId, quantity, selected, programLineId) return true } diff --git a/src/lib/cart/components/Cart.jsx b/src/lib/cart/components/Cart.jsx index 29bbf080..0cbcacd8 100644 --- a/src/lib/cart/components/Cart.jsx +++ b/src/lib/cart/components/Cart.jsx @@ -4,12 +4,7 @@ import Image from '@/core/components/elements/Image/Image' import NextImage from 'next/image' import currencyFormat from '@/core/utils/currencyFormat' import { useEffect, useState } from 'react' -import { - addCart, - deleteItemCart, - getCartApi, - updateItemCart -} from '@/core/utils/cart' +import { addCart, deleteItemCart, getCartApi, updateItemCart } from '@/core/utils/cart' import { CheckIcon, TrashIcon } from '@heroicons/react/24/outline' import { createSlug } from '@/core/utils/slug' import { useRouter } from 'next/router' @@ -39,12 +34,13 @@ const Cart = () => { if (!auth) return }, [auth]) + const getCart = async () => { + const listCart = await getCartApi() + setCart(listCart) + } + useEffect(() => { - const cart = async () => { - const listCart = await getCartApi() - setCart(listCart) - } - cart() + getCart() }, []) useEffect(() => { @@ -102,16 +98,20 @@ const Cart = () => { }, [products]) useEffect(() => { - if(!promotionActiveId) return - - console.log('bajingan') - updateItemCart({ - productId: variantId, - quantity, - programLineId:promotionActiveId, - selected: true - }) - }, [promotionActiveId, variantId, quantity]) + const updateData = () => { + updateItemCart({ + productId: variantId, + quantity, + programLineId: promotionActiveId, + selected: true + }).then(() => { + getCart().then(() => { + setPromotionType(false) + }) + }) + } + updateData() + }, [promotionActiveId]) useEffect(() => { const LoadProductSimilar = async () => { @@ -416,11 +416,17 @@ const Cart = () => {
- Anda Lebih hamat{' '} - - {' '} - {currencyFormat(product.program.price.priceDiscount)} - + {product.program.type.value == 'merchandise' ? ( + <>Selamat anda mendapatkan merchandise indoteknik.com + ) : ( + <> + Selamat! Pembelian anda lebih hemat + + {' '} + {currencyFormat(product.program.price.priceDiscount)} + + + )}
) : ( @@ -434,7 +440,13 @@ const Cart = () => { )}
handlePopUpPromo(product.id, product.quantity, product.program?.id)} + onClick={() => + handlePopUpPromo( + product.id, + product.quantity, + product.program?.id + ) + } className='ml-auto text-red-500 flex gap-x-1 cursor-pointer' >
Cek Promo
@@ -459,7 +471,10 @@ const Cart = () => { )} - + { />
-
-
- {product.program.type.label} -
-
{product.program.name}
+
+ + {product.program.type.label} + +
+
+ {product.program.name}
diff --git a/src/lib/promotinProgram/components/PromotionType.jsx b/src/lib/promotinProgram/components/PromotionType.jsx index 474e0fc2..0e36d2c1 100644 --- a/src/lib/promotinProgram/components/PromotionType.jsx +++ b/src/lib/promotinProgram/components/PromotionType.jsx @@ -18,7 +18,6 @@ const PromotionType = ({ const [promotionType, setPromotionType] = useState(false) const [promos, setPromotionList] = useState(null) const [activeTitle, setActiveTitle] = useState(null) - console.log('promoactiveid', promotionActiveId) useEffect(() => { const id = variantId -- cgit v1.2.3