pi-kiosk-shared 2.0.0 → 2.0.1

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.
Files changed (2) hide show
  1. package/dist/utils.js +64 -24
  2. package/package.json +1 -1
package/dist/utils.js CHANGED
@@ -80,39 +80,79 @@ export const createEmptyCart = () => ({
80
80
  totalItems: 0
81
81
  });
82
82
  export const addToCart = (cart, product, quantity = 1) => {
83
- const existingItem = cart.items.find((item) => item.product.id === product.id);
84
- if (existingItem) {
85
- existingItem.quantity += quantity;
83
+ const existingItemIndex = cart.items.findIndex((item) => item.product.id === product.id);
84
+ // Create a new items array
85
+ const newItems = [...cart.items];
86
+ if (existingItemIndex !== -1) {
87
+ // Create a new item object with updated quantity (immutable update)
88
+ const existingItem = cart.items[existingItemIndex];
89
+ newItems[existingItemIndex] = {
90
+ ...existingItem,
91
+ quantity: existingItem.quantity + quantity
92
+ };
86
93
  }
87
94
  else {
88
- cart.items.push({ product, quantity });
95
+ // Add new item to the array
96
+ newItems.push({ product, quantity });
89
97
  }
90
- cart.totalAmount = roundPrice(cart.items.reduce((sum, item) => sum + (item.product.price * item.quantity), 0));
91
- cart.totalItems = cart.items.reduce((sum, item) => sum + item.quantity, 0);
92
- return cart;
98
+ // Calculate totals
99
+ const totalAmount = roundPrice(newItems.reduce((sum, item) => sum + (item.product.price * item.quantity), 0));
100
+ const totalItems = newItems.reduce((sum, item) => sum + item.quantity, 0);
101
+ // Return a new cart object (immutable)
102
+ return {
103
+ ...cart,
104
+ items: newItems,
105
+ totalAmount,
106
+ totalItems
107
+ };
93
108
  };
94
109
  export const removeFromCart = (cart, productId) => {
95
- cart.items = cart.items.filter((item) => item.product.id !== productId);
96
- cart.totalAmount = roundPrice(cart.items.reduce((sum, item) => sum + (item.product.price * item.quantity), 0));
97
- cart.totalItems = cart.items.reduce((sum, item) => sum + item.quantity, 0);
98
- return cart;
110
+ // Create a new items array (immutable)
111
+ const newItems = cart.items.filter((item) => item.product.id !== productId);
112
+ // Calculate totals
113
+ const totalAmount = roundPrice(newItems.reduce((sum, item) => sum + (item.product.price * item.quantity), 0));
114
+ const totalItems = newItems.reduce((sum, item) => sum + item.quantity, 0);
115
+ // Return a new cart object (immutable)
116
+ return {
117
+ ...cart,
118
+ items: newItems,
119
+ totalAmount,
120
+ totalItems
121
+ };
99
122
  };
100
123
  export const updateCartItemQuantity = (cart, productId, quantity) => {
101
- const item = cart.items.find((item) => item.product.id === productId);
102
- if (item) {
103
- if (quantity <= 0) {
104
- return removeFromCart(cart, productId);
105
- }
106
- item.quantity = quantity;
107
- cart.totalAmount = roundPrice(cart.items.reduce((sum, item) => sum + (item.product.price * item.quantity), 0));
108
- cart.totalItems = cart.items.reduce((sum, item) => sum + item.quantity, 0);
124
+ if (quantity <= 0) {
125
+ return removeFromCart(cart, productId);
126
+ }
127
+ const itemIndex = cart.items.findIndex((item) => item.product.id === productId);
128
+ if (itemIndex === -1) {
129
+ return cart;
109
130
  }
110
- return cart;
131
+ // Create a new items array with updated item (immutable)
132
+ const newItems = [...cart.items];
133
+ const existingItem = cart.items[itemIndex];
134
+ newItems[itemIndex] = {
135
+ ...existingItem,
136
+ quantity
137
+ };
138
+ // Calculate totals
139
+ const totalAmount = roundPrice(newItems.reduce((sum, item) => sum + (item.product.price * item.quantity), 0));
140
+ const totalItems = newItems.reduce((sum, item) => sum + item.quantity, 0);
141
+ // Return a new cart object (immutable)
142
+ return {
143
+ ...cart,
144
+ items: newItems,
145
+ totalAmount,
146
+ totalItems
147
+ };
111
148
  };
112
149
  export const clearCart = (cart) => {
113
- cart.items = [];
114
- cart.totalAmount = 0;
115
- cart.totalItems = 0;
116
- return cart;
150
+ // Return a new cart object (immutable)
151
+ return {
152
+ ...cart,
153
+ items: [],
154
+ totalAmount: 0,
155
+ totalItems: 0
156
+ };
117
157
  };
118
158
  // Note: getErrorMessage is now exported from ./errors.ts instead
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-kiosk-shared",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "private": false,
5
5
  "description": "Shared components and utilities for Pi Kiosk system",
6
6
  "keywords": [