blob: ccfe1cd74c96b6c89c3abe1708c208ca4c44c375 (
plain)
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
|
export const cartStorageKey = "lsb_cart_id"
export const tokenStorageKey = "lsb_customer_token"
export const getStoredCartId = () => {
if (typeof window === "undefined") {
return null
}
return window.localStorage.getItem(cartStorageKey)
}
export const setStoredCartId = (cartId) => {
if (typeof window === "undefined") {
return
}
window.localStorage.setItem(cartStorageKey, cartId)
}
export const clearStoredCartId = () => {
if (typeof window === "undefined") {
return
}
window.localStorage.removeItem(cartStorageKey)
}
export const getStoredToken = () => {
if (typeof window === "undefined") {
return null
}
return window.localStorage.getItem(tokenStorageKey)
}
export const setStoredToken = (token) => {
if (typeof window === "undefined") {
return
}
window.localStorage.setItem(tokenStorageKey, token)
}
export const clearStoredToken = () => {
if (typeof window === "undefined") {
return
}
window.localStorage.removeItem(tokenStorageKey)
}
export const ensureCart = async (client) => {
const storedCartId = getStoredCartId()
if (storedCartId) {
try {
const { cart } = await client.carts.retrieve(storedCartId)
return cart
} catch (error) {
clearStoredCartId()
}
}
const { regions } = await client.regions.list()
if (!regions?.length) {
throw new Error("Aucune région disponible pour créer un panier.")
}
const { cart } = await client.carts.create({ region_id: regions[0].id })
setStoredCartId(cart.id)
return cart
}
|