1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
import odooApi from "../api/odooApi"
import { getAuth } from "./auth"
/**
* Retrieves cart data from localStorage, if available.
*
* @returns {Object} - The cart data as an object, or an empty object if not found.
*/
const getCart = () => {
if (typeof window !== 'undefined' && window.localStorage) {
const cart = localStorage.getItem('cart')
if (cart) return JSON.parse(cart)
}
return {}
}
/**
* Saves cart data to localStorage, if available.
*
* @param {Object} cart - The cart data to be saved.
* @returns {boolean} - Returns `true` if cart data is saved successfully, `false` otherwise.
*/
const setCart = (cart) => {
if (typeof window !== 'undefined' && window.localStorage) {
const storageChangeEvent = new Event('localStorageChange')
localStorage.setItem('cart', JSON.stringify(cart))
window.dispatchEvent(storageChangeEvent)
return true
}
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.
*
* @param {Object} options - The options object containing the productId.
* @param {string} options.productId - The ID of the product to be retrieved from the cart.
* @returns {Object} - Returns the item object from the cart data, or `undefined` if not found.
*/
const getItemCart = ({ productId }) => {
let cart = getCart()
return cart[productId]
}
/**
* Updates an item in the cart data with the given productId, quantity, and selected status.
*
* @param {Object} options - The options object containing the productId, quantity, and selected status.
* @param {string} options.productId - The ID of the product to be updated in the cart.
* @param {number} options.quantity - The new quantity of the product in the cart.
* @param {boolean} [options.selected=false] - The new selected status of the product in the cart. Default is `false`.
* @returns {boolean} - Returns `true`.
*/
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
}
/**
* Deletes an item from the cart data with the given productId.
*
* @param {Object} options - The options object containing the productId.
* @param {string} options.productId - The ID of the product to be deleted from the cart.
* @returns {boolean} - Returns `true`.
*/
const deleteItemCart = ({ productId }) => {
let cart = getCart()
delete cart[productId]
deleteCart(productId)
setCart(cart)
return true
}
export { getCart, getItemCart, updateItemCart, deleteItemCart, addCart, getCartApi, getCountCart}
|