diff options
| author | trisusilo <tri.susilo@altama.co.id> | 2023-07-24 03:32:03 +0000 |
|---|---|---|
| committer | trisusilo <tri.susilo@altama.co.id> | 2023-07-24 03:32:03 +0000 |
| commit | 4fcb73a336a6025a0ead194b317543efcd4f0e4b (patch) | |
| tree | 20ea651ea2c02be2bae2c2915ca5ab7ff2f95538 /src/core/utils | |
| parent | d61b3ca0213395099e4cea9ace8172d4e622fb52 (diff) | |
| parent | 984f1b13b408ee9652072392d6312d609b353315 (diff) | |
Merged in Feature/promotion_programvaoucher (pull request #15)
Feature/promotion programvaoucher
Diffstat (limited to 'src/core/utils')
| -rw-r--r-- | src/core/utils/cart.js | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/src/core/utils/cart.js b/src/core/utils/cart.js index d987cda7..16befdf7 100644 --- a/src/core/utils/cart.js +++ b/src/core/utils/cart.js @@ -1,3 +1,6 @@ +import odooApi from "../api/odooApi" +import { getAuth } from "./auth" + /** * Retrieves cart data from localStorage, if available. * @@ -27,6 +30,49 @@ const setCart = (cart) => { return false } +const addCart = async (product_id, qty, selected, programLineId = null) => { + const data = { + 'product_id' : product_id, + 'qty' : qty, + 'selected' : selected, + '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 () => { + const id = getAuth()?.id + const cart = await odooApi('GET', `/api/v1/user/${id}/cart`) + + return cart +} + +const getCountCart = async () => { + const id = getAuth()?.id + if(id){ + const cart = await odooApi('GET', `/api/v1/user/${id}/cart/count`) + return cart + } + return +} + +const deleteCart = async (product_id) => { + const id = getAuth()?.id + const cartDelete = await odooApi( + 'DELETE', + `/api/v1/user/${id}/cart?product_ids=${product_id}` + ) +} + /** * Retrieves an item from the cart data based on the given product ID. * @@ -48,11 +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 }) => { +const updateItemCart = async ({ productId, quantity, selected = false , programLineId}) => { let cart = getCart() quantity = parseInt(quantity) cart[productId] = { productId, quantity, selected } setCart(cart) + await addCart(productId, quantity, selected, programLineId) return true } @@ -66,8 +113,9 @@ const updateItemCart = ({ productId, quantity, selected = false }) => { const deleteItemCart = ({ productId }) => { let cart = getCart() delete cart[productId] + deleteCart(productId) setCart(cart) return true } -export { getCart, getItemCart, updateItemCart, deleteItemCart } +export { getCart, getItemCart, updateItemCart, deleteItemCart, addCart, getCartApi, getCountCart} |
