From a698514b32353d8f6386ce8ba8c20941ab65f569 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 26 Feb 2024 15:12:12 +0700 Subject: Add qty append on upsert cart api --- src-migrate/modules/cart/components/ItemSelect.tsx | 8 ++++++- .../product-detail/components/AddToCart.tsx | 10 +++++++- .../modules/product-promo/components/AddToCart.tsx | 10 +++++++- src-migrate/services/cart.ts | 28 +++++++++++++++------- 4 files changed, 45 insertions(+), 11 deletions(-) (limited to 'src-migrate') diff --git a/src-migrate/modules/cart/components/ItemSelect.tsx b/src-migrate/modules/cart/components/ItemSelect.tsx index 1d8886a2..b904a1de 100644 --- a/src-migrate/modules/cart/components/ItemSelect.tsx +++ b/src-migrate/modules/cart/components/ItemSelect.tsx @@ -21,7 +21,13 @@ const CartItemSelect = ({ item }: Props) => { if (typeof auth !== 'object') return setIsLoad(true) - await upsertUserCart(auth.id, item.cart_type, item.id, item.quantity, e.target.checked) + await upsertUserCart({ + userId: auth.id, + type: item.cart_type, + id: item.id, + qty: item.quantity, + selected: e.target.checked + }) await loadCart(auth.id) setIsLoad(false) } diff --git a/src-migrate/modules/product-detail/components/AddToCart.tsx b/src-migrate/modules/product-detail/components/AddToCart.tsx index ebd6be7a..097db98a 100644 --- a/src-migrate/modules/product-detail/components/AddToCart.tsx +++ b/src-migrate/modules/product-detail/components/AddToCart.tsx @@ -36,7 +36,15 @@ const AddToCart = ({ ) return; toast.promise( - upsertUserCart(auth.id, 'product', variantId, quantity, true, source), + upsertUserCart({ + userId: auth.id, + type: 'product', + id: variantId, + qty: quantity, + selected: true, + source: source, + qtyAppend: true + }), { loading: { title: 'Menambahkan ke keranjang', description: 'Mohon tunggu...' }, success: { title: 'Menambahkan ke keranjang', description: 'Berhasil menambahkan ke keranjang belanja' }, diff --git a/src-migrate/modules/product-promo/components/AddToCart.tsx b/src-migrate/modules/product-promo/components/AddToCart.tsx index 95d275fc..192dd231 100644 --- a/src-migrate/modules/product-promo/components/AddToCart.tsx +++ b/src-migrate/modules/product-promo/components/AddToCart.tsx @@ -29,7 +29,15 @@ const ProductPromoAddToCart = ({ promotion }: Props) => { if (status === 'success') return setStatus('loading') - await upsertUserCart(auth.id, 'promotion', promotion.id, 1, true) + await upsertUserCart({ + userId: auth.id, + type: 'promotion', + id: promotion.id, + qty: 1, + selected: true, + source: 'add_to_cart', + qtyAppend: true + }) setStatus('idle') toast({ diff --git a/src-migrate/services/cart.ts b/src-migrate/services/cart.ts index 73967073..11f87125 100644 --- a/src-migrate/services/cart.ts +++ b/src-migrate/services/cart.ts @@ -4,20 +4,32 @@ export const getUserCart = async (userId: number) => { return await odooApi('GET', `/api/v1/user/${userId}/cart`); }; -export const upsertUserCart = async ( - userId: number, - type: 'product' | 'promotion', - id: number, - qty: number, - selected: boolean, - source: 'buy' | 'add_to_cart' = 'add_to_cart' -) => { +interface UpsertUserCartProps { + userId: number; + type: 'product' | 'promotion'; + id: number; + qty: number; + selected: boolean; + source?: 'buy' | 'add_to_cart'; + qtyAppend?: boolean; +} + +export const upsertUserCart = async ({ + userId, + type, + id, + qty, + selected, + source = 'add_to_cart', + qtyAppend = false, +}: UpsertUserCartProps) => { return await odooApi('POST', `/api/v1/user/${userId}/cart/create-or-update`, { product_id: type === 'product' ? id : null, qty, selected, program_line_id: type === 'promotion' ? id : null, source, + qty_append: qtyAppend, }); }; -- cgit v1.2.3