@springmicro/cart 0.2.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,6 +14,14 @@ export const cartStore = persistentAtom(
14
14
 
15
15
  export const apiPathDetails = atom<PathDetailsType>({});
16
16
 
17
+ function cartProductToCartDataItemIds(product: CartProduct) {
18
+ return {
19
+ ...product,
20
+ image: undefined,
21
+ name: undefined,
22
+ };
23
+ }
24
+
17
25
  apiPathDetails.listen((pathDetails) => {
18
26
  if (pathDetailsIsFullyDefined(pathDetails)) {
19
27
  // Runs on init if there is a user id and api key. Automatically logs in if userId is updated from undefined.
@@ -22,7 +30,8 @@ apiPathDetails.listen((pathDetails) => {
22
30
  cartStore.set(
23
31
  JSON.stringify({
24
32
  authentication: { loggedIn: true, user_id: cart.user_id },
25
- cart: JSON.parse((cart as any).cart),
33
+ items: JSON.parse((cart as any).items),
34
+ order: cart.order ? JSON.parse((cart as any).order) : undefined,
26
35
  })
27
36
  );
28
37
  });
@@ -35,7 +44,11 @@ apiPathDetails.listen((pathDetails) => {
35
44
 
36
45
  export function addToCart(p: CartProduct) {
37
46
  const cart: Cart = JSON.parse(cartStore.get());
38
- const newCart = { ...cart, cart: [...cart.cart, p] };
47
+ const newCart: Cart = {
48
+ ...cart,
49
+ items: [...cart.items, p],
50
+ order: undefined,
51
+ };
39
52
 
40
53
  const pathDetails = apiPathDetails.get();
41
54
  if (pathDetailsIsFullyDefined(pathDetails)) {
@@ -44,7 +57,7 @@ export function addToCart(p: CartProduct) {
44
57
  pathDetails,
45
58
  JSON.stringify({
46
59
  user_id: pathDetails.userId,
47
- cart: JSON.stringify(newCart.cart),
60
+ items: newCart.items.map((p) => cartProductToCartDataItemIds(p)),
48
61
  })
49
62
  ).then(async () => {
50
63
  cartStore.set(JSON.stringify(newCart));
@@ -55,11 +68,11 @@ export function addToCart(p: CartProduct) {
55
68
  }
56
69
 
57
70
  export function removeFromCart(i: number) {
58
- const cart = JSON.parse(cartStore.get());
71
+ const cart: Cart = JSON.parse(cartStore.get());
59
72
 
60
- const products: CartProduct[] = [...cart.cart];
73
+ const products: CartProduct[] = [...cart.items];
61
74
  products.splice(i, 1);
62
- const newCart = { ...cart, cart: products };
75
+ const newCart: Cart = { ...cart, items: products, order: undefined };
63
76
 
64
77
  const pathDetails = apiPathDetails.get();
65
78
  if (pathDetailsIsFullyDefined(pathDetails)) {
@@ -68,7 +81,7 @@ export function removeFromCart(i: number) {
68
81
 
69
82
  const body = {
70
83
  user_id: pathDetails.userId,
71
- cart: JSON.stringify(newCart.cart),
84
+ items: newCart.items.map((p) => cartProductToCartDataItemIds(p)),
72
85
  };
73
86
 
74
87
  fetchFromCartApi(
@@ -84,8 +97,8 @@ export function removeFromCart(i: number) {
84
97
  }
85
98
 
86
99
  export function clearCart() {
87
- const cart = JSON.parse(cartStore.get());
88
- const newCart = { ...cart, cart: [] };
100
+ const cart: Cart = JSON.parse(cartStore.get());
101
+ const newCart: Cart = { ...cart, items: [], order: undefined };
89
102
 
90
103
  const pathDetails = apiPathDetails.get();
91
104
  if (pathDetailsIsFullyDefined(pathDetails)) {
@@ -96,3 +109,25 @@ export function clearCart() {
96
109
  cartStore.set(JSON.stringify(newCart));
97
110
  }
98
111
  }
112
+
113
+ export function setOrder(order: { id: number; reference: string }) {
114
+ const cart: Cart = JSON.parse(cartStore.get());
115
+ const newCart: Cart = { ...cart, order };
116
+
117
+ const pathDetails = apiPathDetails.get();
118
+ if (pathDetailsIsFullyDefined(pathDetails)) {
119
+ fetchFromCartApi(
120
+ "PUT",
121
+ pathDetails,
122
+ JSON.stringify({
123
+ user_id: pathDetails.userId,
124
+ items: newCart.items.map((p) => cartProductToCartDataItemIds(p)),
125
+ order: JSON.stringify(order),
126
+ })
127
+ ).then(async () => {
128
+ cartStore.set(JSON.stringify(newCart));
129
+ });
130
+ } else {
131
+ cartStore.set(JSON.stringify(newCart));
132
+ }
133
+ }