summaryrefslogtreecommitdiff
path: root/src/core/utils/cart.js
diff options
context:
space:
mode:
authorHATEC\SPVDEV001 <tri.susilo@altama.co.id>2023-07-24 15:33:17 +0700
committerHATEC\SPVDEV001 <tri.susilo@altama.co.id>2023-07-24 15:33:17 +0700
commit2de322571bad7490baaf439fa2bb12124c646bb5 (patch)
treed7c4151eb2bf83b85cd0532d45dedd02aa06694e /src/core/utils/cart.js
parent8b8ab7680be37e84d0dc6cc1683fe4996f7cc8c1 (diff)
parentd4f3cce1b07c5d4f75892ffc49c8dbbbbb58922f (diff)
Merge branch 'master' into CR/widget_WA
Diffstat (limited to 'src/core/utils/cart.js')
-rw-r--r--src/core/utils/cart.js52
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}