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.
- package/dist/utils.js +64 -24
- 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
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
95
|
+
// Add new item to the array
|
|
96
|
+
newItems.push({ product, quantity });
|
|
89
97
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
|
|
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
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|