@open-tender/utils 0.5.77 → 0.5.79
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/cjs/__tests__/utils/cartKiosk.test.d.ts +1 -0
- package/dist/cjs/__tests__/utils/cartKiosk.test.js +176 -0
- package/dist/cjs/__tests__/utils/makeSimpleCart.test.js +38 -0
- package/dist/cjs/hooks/index.d.ts +2 -0
- package/dist/cjs/hooks/index.js +2 -0
- package/dist/cjs/hooks/useAccordionKiosk.d.ts +22 -0
- package/dist/cjs/hooks/useAccordionKiosk.js +65 -0
- package/dist/cjs/hooks/useBuilderKiosk.d.ts +16 -0
- package/dist/cjs/hooks/useBuilderKiosk.js +166 -0
- package/dist/cjs/utils/cart.d.ts +36 -1
- package/dist/cjs/utils/cart.js +261 -1
- package/dist/esm/__tests__/utils/cartKiosk.test.d.ts +1 -0
- package/dist/esm/__tests__/utils/cartKiosk.test.js +174 -0
- package/dist/esm/__tests__/utils/makeSimpleCart.test.js +38 -0
- package/dist/esm/hooks/index.d.ts +2 -0
- package/dist/esm/hooks/index.js +2 -0
- package/dist/esm/hooks/useAccordionKiosk.d.ts +22 -0
- package/dist/esm/hooks/useAccordionKiosk.js +61 -0
- package/dist/esm/hooks/useBuilderKiosk.d.ts +16 -0
- package/dist/esm/hooks/useBuilderKiosk.js +162 -0
- package/dist/esm/utils/cart.d.ts +36 -1
- package/dist/esm/utils/cart.js +248 -0
- package/package.json +1 -1
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
import { calcPricesKiosk, getIncrementedQuantity } from '../utils';
|
|
3
|
+
// The kiosk variant of useBuilder, left as it is for the other apps:
|
|
4
|
+
// - items are priced with calcPricesKiosk
|
|
5
|
+
// - setOptionQuantity can set the quantity of an option of a full group, and
|
|
6
|
+
// of a nested option (the quantity was dropped below the first level)
|
|
7
|
+
// - setGroups and setImageUrl are added
|
|
8
|
+
// quantityLimit is how high the item can go in the cart (see
|
|
9
|
+
// getIncrementedQuantity), onQuantityLimit is called when + is refused.
|
|
10
|
+
export const useBuilderKiosk = (orderItem, orderTimeoutCallback, quantityLimit, onQuantityLimit) => {
|
|
11
|
+
const [item, setItem] = useState(orderItem);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
orderTimeoutCallback && orderTimeoutCallback();
|
|
14
|
+
}, [item, orderTimeoutCallback]);
|
|
15
|
+
const increment = () => {
|
|
16
|
+
const { quantity, refused } = getIncrementedQuantity(item, quantityLimit);
|
|
17
|
+
if (refused) {
|
|
18
|
+
onQuantityLimit === null || onQuantityLimit === void 0 ? void 0 : onQuantityLimit();
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const pricedItem = calcPricesKiosk(Object.assign(Object.assign({}, item), { quantity }));
|
|
22
|
+
setItem(pricedItem);
|
|
23
|
+
};
|
|
24
|
+
const decrement = () => {
|
|
25
|
+
const newQuantity = Math.max(item.quantity - item.increment, item.min);
|
|
26
|
+
const pricedItem = calcPricesKiosk(Object.assign(Object.assign({}, item), { quantity: newQuantity }));
|
|
27
|
+
setItem(pricedItem);
|
|
28
|
+
};
|
|
29
|
+
const setQuantity = (quantity) => {
|
|
30
|
+
const pricedItem = calcPricesKiosk(Object.assign(Object.assign({}, item), { quantity }));
|
|
31
|
+
setItem(pricedItem);
|
|
32
|
+
};
|
|
33
|
+
const setMadeFor = (madeFor) => {
|
|
34
|
+
setItem(Object.assign(Object.assign({}, item), { madeFor }));
|
|
35
|
+
};
|
|
36
|
+
const setNotes = (notes) => {
|
|
37
|
+
setItem(prevItem => (Object.assign(Object.assign({}, prevItem), { notes })));
|
|
38
|
+
};
|
|
39
|
+
const setMadeForNotes = (madeFor, notes) => {
|
|
40
|
+
setItem(Object.assign(Object.assign({}, item), { madeFor, notes }));
|
|
41
|
+
};
|
|
42
|
+
const toggleGroupOption = (group, optionId) => {
|
|
43
|
+
const options = group.options.map(option => {
|
|
44
|
+
return Object.assign(Object.assign({}, option), { quantity: option.id === optionId ? 1 : 0 });
|
|
45
|
+
});
|
|
46
|
+
return Object.assign(Object.assign({}, group), { options });
|
|
47
|
+
};
|
|
48
|
+
const incrementGroupOption = (group, optionId) => {
|
|
49
|
+
const count = group.options
|
|
50
|
+
.filter(o => o.id !== optionId)
|
|
51
|
+
.reduce((t, o) => t + o.quantity, 0);
|
|
52
|
+
if (group.max !== 0 && count >= group.max)
|
|
53
|
+
return group;
|
|
54
|
+
const options = group.options.map(option => {
|
|
55
|
+
if (option.id === optionId) {
|
|
56
|
+
let quantity = option.quantity + option.increment;
|
|
57
|
+
const quantities = [quantity];
|
|
58
|
+
if (option.max !== 0)
|
|
59
|
+
quantities.push(option.max);
|
|
60
|
+
if (group.max !== 0)
|
|
61
|
+
quantities.push(group.max - count);
|
|
62
|
+
quantity = Math.min(...quantities);
|
|
63
|
+
return Object.assign(Object.assign({}, option), { quantity });
|
|
64
|
+
}
|
|
65
|
+
return option;
|
|
66
|
+
});
|
|
67
|
+
return Object.assign(Object.assign({}, group), { options });
|
|
68
|
+
};
|
|
69
|
+
const decrementGroupOption = (group, optionId) => {
|
|
70
|
+
const options = group.options.map(option => {
|
|
71
|
+
if (option.id === optionId) {
|
|
72
|
+
const quantity = Math.max(option.quantity - option.increment, 0);
|
|
73
|
+
return Object.assign(Object.assign({}, option), { quantity });
|
|
74
|
+
}
|
|
75
|
+
return option;
|
|
76
|
+
});
|
|
77
|
+
return Object.assign(Object.assign({}, group), { options });
|
|
78
|
+
};
|
|
79
|
+
const setGroupOptionQuantity = (group, optionId, quantity) => {
|
|
80
|
+
if (quantity === undefined)
|
|
81
|
+
return group;
|
|
82
|
+
const count = group.options
|
|
83
|
+
.filter(o => o.id !== optionId)
|
|
84
|
+
.reduce((t, o) => t + o.quantity, 0);
|
|
85
|
+
const options = group.options.map(option => {
|
|
86
|
+
if (option.id === optionId) {
|
|
87
|
+
const quantities = [quantity];
|
|
88
|
+
if (option.max !== 0)
|
|
89
|
+
quantities.push(option.max);
|
|
90
|
+
if (group.max !== 0)
|
|
91
|
+
quantities.push(group.max - count);
|
|
92
|
+
const limited = Math.min(...quantities);
|
|
93
|
+
return Object.assign(Object.assign({}, option), { quantity: Math.max(limited, option.min) });
|
|
94
|
+
}
|
|
95
|
+
return option;
|
|
96
|
+
});
|
|
97
|
+
return Object.assign(Object.assign({}, group), { options });
|
|
98
|
+
};
|
|
99
|
+
const updateGroups = (groups, levels, fn, quantity) => {
|
|
100
|
+
const [[groupId, optionId], ...rest] = levels;
|
|
101
|
+
return groups.map(group => {
|
|
102
|
+
if (group.id === groupId) {
|
|
103
|
+
if (!rest.length)
|
|
104
|
+
return fn(group, optionId, quantity);
|
|
105
|
+
const options = group.options.map(option => {
|
|
106
|
+
if (option.id === optionId) {
|
|
107
|
+
return Object.assign(Object.assign({}, option), { groups: updateGroups(option.groups, rest, fn, quantity) });
|
|
108
|
+
}
|
|
109
|
+
return option;
|
|
110
|
+
});
|
|
111
|
+
return Object.assign(Object.assign({}, group), { options });
|
|
112
|
+
}
|
|
113
|
+
return group;
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
const toggleOption = (levels) => {
|
|
117
|
+
const groups = updateGroups(item.groups, levels, toggleGroupOption);
|
|
118
|
+
const pricedItem = calcPricesKiosk(Object.assign(Object.assign({}, item), { groups: groups }));
|
|
119
|
+
setItem(pricedItem);
|
|
120
|
+
return pricedItem;
|
|
121
|
+
};
|
|
122
|
+
const incrementOption = (levels) => {
|
|
123
|
+
const groups = updateGroups(item.groups, levels, incrementGroupOption);
|
|
124
|
+
const pricedItem = calcPricesKiosk(Object.assign(Object.assign({}, item), { groups: groups }));
|
|
125
|
+
setItem(pricedItem);
|
|
126
|
+
return pricedItem;
|
|
127
|
+
};
|
|
128
|
+
const decrementOption = (levels) => {
|
|
129
|
+
const groups = updateGroups(item.groups, levels, decrementGroupOption);
|
|
130
|
+
const pricedItem = calcPricesKiosk(Object.assign(Object.assign({}, item), { groups: groups }));
|
|
131
|
+
setItem(pricedItem);
|
|
132
|
+
return pricedItem;
|
|
133
|
+
};
|
|
134
|
+
const setOptionQuantity = (levels, quantity) => {
|
|
135
|
+
const groups = updateGroups(item.groups, levels, setGroupOptionQuantity, quantity);
|
|
136
|
+
const pricedItem = calcPricesKiosk(Object.assign(Object.assign({}, item), { groups: groups }));
|
|
137
|
+
setItem(pricedItem);
|
|
138
|
+
return pricedItem;
|
|
139
|
+
};
|
|
140
|
+
const setGroups = (groups) => {
|
|
141
|
+
const pricedItem = calcPricesKiosk(Object.assign(Object.assign({}, item), { groups }));
|
|
142
|
+
setItem(pricedItem);
|
|
143
|
+
};
|
|
144
|
+
const setImageUrl = (imageUrl) => {
|
|
145
|
+
setItem(prevItem => (Object.assign(Object.assign({}, prevItem), { imageUrl })));
|
|
146
|
+
};
|
|
147
|
+
return {
|
|
148
|
+
item,
|
|
149
|
+
increment,
|
|
150
|
+
decrement,
|
|
151
|
+
setQuantity,
|
|
152
|
+
setMadeFor,
|
|
153
|
+
setNotes,
|
|
154
|
+
setMadeForNotes,
|
|
155
|
+
toggleOption,
|
|
156
|
+
incrementOption,
|
|
157
|
+
decrementOption,
|
|
158
|
+
setOptionQuantity,
|
|
159
|
+
setGroups,
|
|
160
|
+
setImageUrl
|
|
161
|
+
};
|
|
162
|
+
};
|
package/dist/esm/utils/cart.d.ts
CHANGED
|
@@ -242,6 +242,41 @@ export declare const validateCart: (cart: Cart, categories: MenuCategories, sold
|
|
|
242
242
|
errors: CartErrors | null;
|
|
243
243
|
};
|
|
244
244
|
export declare const getDefaultTip: (config: CheckoutConfig) => string | null;
|
|
245
|
-
export declare const prepareOrder: (data: OrderData
|
|
245
|
+
export declare const prepareOrder: (data: OrderData & {
|
|
246
|
+
orderUuid?: string | null;
|
|
247
|
+
}) => OrderCreate;
|
|
246
248
|
export declare const checkAmountRemaining: (total: Money, tenders: CheckoutTenders) => number;
|
|
247
249
|
export declare const makeDeviceType: (deviceType: string, deviceId?: string) => "DESKTOP" | "MOBILE" | "TABLET";
|
|
250
|
+
export declare const calcNutritionKiosk: (item: CartItem | CartItemOption) => NutritionalInfoCalcs;
|
|
251
|
+
export declare const calcNutritionalInfoKiosk: (item: CartItem | CartItemOption) => NutritionalInfo;
|
|
252
|
+
export type CartItemOptionKioskDeltas = {
|
|
253
|
+
priceDelta: number | null;
|
|
254
|
+
userExtra: number;
|
|
255
|
+
displayDelta: number;
|
|
256
|
+
hasDefaultSibling: boolean;
|
|
257
|
+
};
|
|
258
|
+
export declare const calcPricesKiosk: (item: CartItem | CartItemOption) => CartItem | CartItemOption;
|
|
259
|
+
export declare const makeOrderItemKiosk: (item: MenuItem, isEdit?: boolean, soldOut?: SoldOut, simpleItem?: SimpleCartItem, hasPoints?: boolean, requestedAt?: RequestedAt | null) => CartItemOption | CartItem;
|
|
260
|
+
export declare const makeUpsellItemsKiosk: (itemIds: number[], itemLookup: Record<string, MenuItem>) => (CartItemOption | CartItem)[];
|
|
261
|
+
export declare const makeUpsellItemsForCartKiosk: (categories: MenuCategories, cartIds: number[], soldOut: number[]) => (CartItemOption | CartItem)[];
|
|
262
|
+
export declare const rehydrateOrderItemKiosk: (menuItem: MenuItem, simpleCartItem: SimpleCartItem, soldOut?: number[], requestedAt?: RequestedAt | null) => CartItem;
|
|
263
|
+
export declare const rehydrateCartKiosk: (menuItems: MenuItems, simpleCartItems: SimpleCart, soldOut?: number[], options?: {
|
|
264
|
+
isReorder?: boolean;
|
|
265
|
+
preferredIds?: Set<number>;
|
|
266
|
+
}) => {
|
|
267
|
+
cart: CartItem[];
|
|
268
|
+
cartCounts: Record<string, number>;
|
|
269
|
+
unavailable: ReorderUnavailableLine[];
|
|
270
|
+
};
|
|
271
|
+
export declare const addItemKiosk: (cart: Cart, item: CartItem) => {
|
|
272
|
+
cart: Cart;
|
|
273
|
+
cartCounts: Record<string, number>;
|
|
274
|
+
};
|
|
275
|
+
export declare const incrementItemKiosk: (cart: Cart, index: number) => {
|
|
276
|
+
cart: Cart;
|
|
277
|
+
cartCounts: Record<string, number>;
|
|
278
|
+
};
|
|
279
|
+
export declare const decrementItemKiosk: (cart: Cart, index: number) => {
|
|
280
|
+
cart: Cart;
|
|
281
|
+
cartCounts: Record<string, number>;
|
|
282
|
+
};
|
package/dist/esm/utils/cart.js
CHANGED
|
@@ -1016,6 +1016,8 @@ export const prepareOrder = (data) => {
|
|
|
1016
1016
|
}
|
|
1017
1017
|
if (data.orderId)
|
|
1018
1018
|
order.order_id = data.orderId;
|
|
1019
|
+
if (data.orderId && data.orderUuid)
|
|
1020
|
+
order.order_uuid = data.orderUuid;
|
|
1019
1021
|
if (data.cartId)
|
|
1020
1022
|
order.cart_id = data.cartId;
|
|
1021
1023
|
if (data.save)
|
|
@@ -1055,3 +1057,249 @@ export const makeDeviceType = (deviceType, deviceId) => {
|
|
|
1055
1057
|
return 'DESKTOP';
|
|
1056
1058
|
}
|
|
1057
1059
|
};
|
|
1060
|
+
/* kiosk variants */
|
|
1061
|
+
// Everything below is used by the kiosk app (open-tender-kiosk-app) and by the
|
|
1062
|
+
// Kiosk containers of @open-tender/store. Each function is the kiosk
|
|
1063
|
+
// counterpart of the function of the same name without the suffix, which is
|
|
1064
|
+
// left as it is for the other apps. A function that calls a kiosk function has
|
|
1065
|
+
// a kiosk variant too, so an item is priced the same way wherever the kiosk
|
|
1066
|
+
// builds, edits or re-prices it.
|
|
1067
|
+
const nutritionKeysKiosk = [
|
|
1068
|
+
'calories',
|
|
1069
|
+
'calories_from_fat',
|
|
1070
|
+
'cholesterol',
|
|
1071
|
+
'dietary_fiber',
|
|
1072
|
+
'protein',
|
|
1073
|
+
'saturated_fat',
|
|
1074
|
+
'sodium',
|
|
1075
|
+
'sugars',
|
|
1076
|
+
'total_carbs',
|
|
1077
|
+
'total_fat',
|
|
1078
|
+
'trans_fat',
|
|
1079
|
+
'serving_weight'
|
|
1080
|
+
];
|
|
1081
|
+
// A "Remove ..." option carries the nutrition of the ingredient it removes.
|
|
1082
|
+
// It counts while it is not selected (the ingredient is still on the item) and
|
|
1083
|
+
// drops out once it is.
|
|
1084
|
+
const isRemoveOptionKiosk = (option) => (option.name || '').toLowerCase().startsWith('remove');
|
|
1085
|
+
// Unlike calcNutrition, this walks every option, selected or not, so that
|
|
1086
|
+
// unselected "Remove ..." options are counted.
|
|
1087
|
+
export const calcNutritionKiosk = (item) => {
|
|
1088
|
+
const { nutritionalInfo, groups } = item;
|
|
1089
|
+
let nutrition = (nutritionalInfo ? Object.assign({}, nutritionalInfo) : {});
|
|
1090
|
+
groups.forEach(g => {
|
|
1091
|
+
g.options.forEach(option => {
|
|
1092
|
+
const isRemove = isRemoveOptionKiosk(option);
|
|
1093
|
+
const selected = (option.quantity || 0) > 0;
|
|
1094
|
+
const optionNutrition = calcNutritionKiosk(option);
|
|
1095
|
+
nutritionKeysKiosk.forEach(key => {
|
|
1096
|
+
const optionPer = optionNutrition ? optionNutrition[key] || 0 : 0;
|
|
1097
|
+
const itemVal = nutrition[key] || 0;
|
|
1098
|
+
const contribution = isRemove
|
|
1099
|
+
? selected
|
|
1100
|
+
? 0
|
|
1101
|
+
: Math.abs(optionPer)
|
|
1102
|
+
: optionPer * (option.quantity || 0);
|
|
1103
|
+
nutrition = Object.assign(Object.assign({}, nutrition), { [key]: itemVal + contribution });
|
|
1104
|
+
});
|
|
1105
|
+
});
|
|
1106
|
+
});
|
|
1107
|
+
return nutrition;
|
|
1108
|
+
};
|
|
1109
|
+
export const calcNutritionalInfoKiosk = (item) => {
|
|
1110
|
+
const nutrition = calcNutritionKiosk(item);
|
|
1111
|
+
const { serving_size = '0.00' } = item.nutritionalInfo || {};
|
|
1112
|
+
return Object.assign(Object.assign({}, nutrition), { serving_size });
|
|
1113
|
+
};
|
|
1114
|
+
// calcPrices, plus:
|
|
1115
|
+
// - "Remove ..." options count their calories while they are not selected
|
|
1116
|
+
// - the options of a meal get the fields of CartItemOptionKioskDeltas
|
|
1117
|
+
// - an item without calories of its own still gets a totalCals when its
|
|
1118
|
+
// options have some
|
|
1119
|
+
export const calcPricesKiosk = (item) => {
|
|
1120
|
+
var _a;
|
|
1121
|
+
const itemName = (item.name || '').toLowerCase();
|
|
1122
|
+
const isMeal = itemName.includes('meal') || itemName.includes('meat & 3');
|
|
1123
|
+
const groups = item.groups.map(g => {
|
|
1124
|
+
let groupQuantity = 0;
|
|
1125
|
+
const optionsWithPrices = g.options.map((o, index) => {
|
|
1126
|
+
return Object.assign(Object.assign({}, calcPricesKiosk(o)), { index });
|
|
1127
|
+
});
|
|
1128
|
+
const sortedOptions = optionsWithPrices.sort((a, b) => {
|
|
1129
|
+
const pricePerA = a.totalPrice && a.quantity ? a.totalPrice / a.quantity : 0;
|
|
1130
|
+
const pricePerB = b.totalPrice && b.quantity ? b.totalPrice / b.quantity : 0;
|
|
1131
|
+
return pricePerA - pricePerB;
|
|
1132
|
+
});
|
|
1133
|
+
const pricedOptions = sortedOptions.map(o => {
|
|
1134
|
+
const pricePer = o.totalPrice && o.quantity ? o.totalPrice / o.quantity : 0;
|
|
1135
|
+
const pointsPer = o.totalPoints && o.quantity ? o.totalPoints / o.quantity : 0;
|
|
1136
|
+
const includedRemaining = Math.max(g.included - groupQuantity, 0);
|
|
1137
|
+
const priceQuantity = Math.max(o.quantity - includedRemaining, 0);
|
|
1138
|
+
const selected = (o.quantity || 0) > 0;
|
|
1139
|
+
const totalCals = isRemoveOptionKiosk(o)
|
|
1140
|
+
? selected
|
|
1141
|
+
? 0
|
|
1142
|
+
: Math.abs(o.cals || 0)
|
|
1143
|
+
: o.totalCals || 0;
|
|
1144
|
+
const option = Object.assign(Object.assign({}, o), { totalPrice: priceQuantity * pricePer, totalPoints: priceQuantity * pointsPer, totalCals });
|
|
1145
|
+
groupQuantity += o.quantity || 0;
|
|
1146
|
+
return option;
|
|
1147
|
+
});
|
|
1148
|
+
const defaultSibling = g.options.find(o => o.isDefault);
|
|
1149
|
+
const hasDefault = !!defaultSibling;
|
|
1150
|
+
const defaultUnitPrice = defaultSibling ? defaultSibling.price || 0 : 0;
|
|
1151
|
+
const withDeltas = isMeal
|
|
1152
|
+
? pricedOptions.map(o => {
|
|
1153
|
+
const priceDelta = hasDefault
|
|
1154
|
+
? (o.price || 0) - defaultUnitPrice
|
|
1155
|
+
: null;
|
|
1156
|
+
const userExtra = (o.groups || []).reduce((sum, gg) => {
|
|
1157
|
+
const children = (gg.options || []);
|
|
1158
|
+
return (sum +
|
|
1159
|
+
children.reduce((s, child) => {
|
|
1160
|
+
const childSelected = (child.quantity || 0) > 0;
|
|
1161
|
+
if (!childSelected)
|
|
1162
|
+
return s;
|
|
1163
|
+
const ownAdded = !child.isDefault
|
|
1164
|
+
? (child.price || 0) * child.quantity
|
|
1165
|
+
: 0;
|
|
1166
|
+
const rolledUp = child.userExtra || 0;
|
|
1167
|
+
return s + ownAdded + rolledUp;
|
|
1168
|
+
}, 0));
|
|
1169
|
+
}, 0);
|
|
1170
|
+
const displayDelta = hasDefault
|
|
1171
|
+
? (priceDelta || 0) + userExtra
|
|
1172
|
+
: (o.price || 0) + userExtra;
|
|
1173
|
+
return Object.assign(Object.assign({}, o), { priceDelta,
|
|
1174
|
+
userExtra,
|
|
1175
|
+
displayDelta, hasDefaultSibling: hasDefault });
|
|
1176
|
+
})
|
|
1177
|
+
: pricedOptions;
|
|
1178
|
+
const options = withDeltas.sort((a, b) => a.index - b.index);
|
|
1179
|
+
return Object.assign(Object.assign({}, g), { quantity: groupQuantity, options });
|
|
1180
|
+
});
|
|
1181
|
+
const optionsPrice = groups.reduce((t, g) => {
|
|
1182
|
+
return t + g.options.reduce((ot, o) => ot + o.totalPrice, 0.0);
|
|
1183
|
+
}, 0.0);
|
|
1184
|
+
const totalPrice = item.quantity * (item.price + optionsPrice);
|
|
1185
|
+
const optionsPoints = groups.reduce((t, g) => {
|
|
1186
|
+
return t + g.options.reduce((ot, o) => ot + o.totalPoints, 0);
|
|
1187
|
+
}, 0.0);
|
|
1188
|
+
const totalPoints = item.points
|
|
1189
|
+
? item.quantity * (item.points + optionsPoints)
|
|
1190
|
+
: null;
|
|
1191
|
+
const optionsCals = groups.reduce((t, g) => {
|
|
1192
|
+
return t + g.options.reduce((ot, o) => ot + o.totalCals, 0);
|
|
1193
|
+
}, 0.0);
|
|
1194
|
+
const totalCals = item.cals != null || optionsCals > 0
|
|
1195
|
+
? item.quantity * (((_a = item.cals) !== null && _a !== void 0 ? _a : 0) + optionsCals)
|
|
1196
|
+
: null;
|
|
1197
|
+
const nonSizeGroups = groups ? groups.filter(i => !i.isSize) : null;
|
|
1198
|
+
const currentOptions = nonSizeGroups
|
|
1199
|
+
? getItemOptions(Object.assign(Object.assign({}, item), { groups: nonSizeGroups }))
|
|
1200
|
+
: [];
|
|
1201
|
+
const modifiersAllergens = currentOptions.reduce((t, o) => {
|
|
1202
|
+
return [...t, ...o.allergens];
|
|
1203
|
+
}, []);
|
|
1204
|
+
const totalAllergens = [...new Set(item.allergens.concat(modifiersAllergens))];
|
|
1205
|
+
return Object.assign(Object.assign({}, item), { groups, totalPrice, totalPoints, totalCals, totalAllergens });
|
|
1206
|
+
};
|
|
1207
|
+
// calcPrices and calcPricesKiosk work out every total from the price,
|
|
1208
|
+
// quantity, calories and points of the item and its options and never from a
|
|
1209
|
+
// total that is already there, so an item priced by calcPrices can be priced
|
|
1210
|
+
// again by calcPricesKiosk. The kiosk variants below rely on this to reuse the
|
|
1211
|
+
// functions they are a variant of.
|
|
1212
|
+
// makeOrderItem priced with calcPricesKiosk, plus smallImageUrl, the small
|
|
1213
|
+
// image the kiosk shows on its menu tiles.
|
|
1214
|
+
export const makeOrderItemKiosk = (item, isEdit, soldOut = [], simpleItem, hasPoints = false, requestedAt = null) => {
|
|
1215
|
+
const orderItem = makeOrderItem(item, isEdit, soldOut, simpleItem, hasPoints, requestedAt);
|
|
1216
|
+
const kioskItem = Object.assign(Object.assign({}, orderItem), { smallImageUrl: item.small_image_url });
|
|
1217
|
+
return calcPricesKiosk(kioskItem);
|
|
1218
|
+
};
|
|
1219
|
+
export const makeUpsellItemsKiosk = (itemIds, itemLookup) => {
|
|
1220
|
+
return itemIds
|
|
1221
|
+
.map(id => itemLookup[id])
|
|
1222
|
+
.filter(i => i)
|
|
1223
|
+
.map(i => makeOrderItemKiosk(i))
|
|
1224
|
+
.filter(i => !i.isSoldOut);
|
|
1225
|
+
};
|
|
1226
|
+
export const makeUpsellItemsForCartKiosk = (categories, cartIds, soldOut) => {
|
|
1227
|
+
const itemLookup = makeMenuItemLookup(categories);
|
|
1228
|
+
const menuItems = cartIds.map(id => itemLookup[id]).filter(Boolean);
|
|
1229
|
+
const upsellItemIds = menuItems.reduce((arr, i) => {
|
|
1230
|
+
const upsellsItems = i.upsell_items.slice(0, cartIds.length > 2 ? 2 : undefined);
|
|
1231
|
+
return [...arr, ...upsellsItems];
|
|
1232
|
+
}, []);
|
|
1233
|
+
const uniqueIds = Array.from(new Set(upsellItemIds));
|
|
1234
|
+
const itemIds = uniqueIds.filter(id => !cartIds.includes(id) && !soldOut.includes(id));
|
|
1235
|
+
return makeUpsellItemsKiosk(itemIds, itemLookup);
|
|
1236
|
+
};
|
|
1237
|
+
export const rehydrateOrderItemKiosk = (menuItem, simpleCartItem, soldOut = [], requestedAt = null) => {
|
|
1238
|
+
const orderItem = rehydrateOrderItem(menuItem, simpleCartItem, soldOut, requestedAt);
|
|
1239
|
+
const kioskItem = Object.assign(Object.assign({}, orderItem), { smallImageUrl: menuItem.small_image_url });
|
|
1240
|
+
return calcPricesKiosk(kioskItem);
|
|
1241
|
+
};
|
|
1242
|
+
export const rehydrateCartKiosk = (menuItems, simpleCartItems, soldOut = [], options = {}) => {
|
|
1243
|
+
const rehydrated = rehydrateCart(menuItems, simpleCartItems, soldOut, options);
|
|
1244
|
+
const cart = rehydrated.cart.map(item => {
|
|
1245
|
+
// a rehydrated item has the id of the menu item it was made from
|
|
1246
|
+
const menuItem = (menuItems || []).find(i => i.id === item.id);
|
|
1247
|
+
const smallImageUrl = menuItem ? menuItem.small_image_url : undefined;
|
|
1248
|
+
const kioskItem = Object.assign(Object.assign({}, item), { smallImageUrl });
|
|
1249
|
+
return calcPricesKiosk(kioskItem);
|
|
1250
|
+
});
|
|
1251
|
+
return Object.assign(Object.assign({}, rehydrated), { cart });
|
|
1252
|
+
};
|
|
1253
|
+
export const addItemKiosk = (cart, item) => {
|
|
1254
|
+
if (typeof item.index === 'undefined') {
|
|
1255
|
+
const itemSignature = makeCartItemSignature(item);
|
|
1256
|
+
const itemSignatures = cart.map(i => (Object.assign({ signature: makeCartItemSignature(i) }, i)));
|
|
1257
|
+
const match = itemSignatures.find(i => i.signature === itemSignature);
|
|
1258
|
+
if (match && match.index !== undefined) {
|
|
1259
|
+
const quantity = item.quantity + match.quantity;
|
|
1260
|
+
const newItem = Object.assign(Object.assign({}, item), { quantity });
|
|
1261
|
+
const updated = calcPricesKiosk(newItem);
|
|
1262
|
+
cart[match.index] = Object.assign(Object.assign({}, updated), { index: match.index });
|
|
1263
|
+
}
|
|
1264
|
+
else {
|
|
1265
|
+
cart.push(Object.assign(Object.assign({}, item), { index: cart.length }));
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
else {
|
|
1269
|
+
cart[item.index] = item;
|
|
1270
|
+
}
|
|
1271
|
+
const cartCounts = calcCartCounts(cart);
|
|
1272
|
+
return { cart, cartCounts };
|
|
1273
|
+
};
|
|
1274
|
+
export const incrementItemKiosk = (cart, index) => {
|
|
1275
|
+
const realIndex = getItemRealIndex(cart, index);
|
|
1276
|
+
if (realIndex > -1) {
|
|
1277
|
+
const item = cart[realIndex];
|
|
1278
|
+
if (item.max === 0 || item.quantity < item.max) {
|
|
1279
|
+
let newQuantity = item.quantity + item.increment;
|
|
1280
|
+
newQuantity =
|
|
1281
|
+
item.max === 0 ? newQuantity : Math.min(item.max, newQuantity);
|
|
1282
|
+
const newItem = calcPricesKiosk(Object.assign(Object.assign({}, item), { quantity: newQuantity }));
|
|
1283
|
+
cart[realIndex] = newItem;
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
const cartCounts = calcCartCounts(cart);
|
|
1287
|
+
return { cart, cartCounts };
|
|
1288
|
+
};
|
|
1289
|
+
export const decrementItemKiosk = (cart, index) => {
|
|
1290
|
+
const realIndex = getItemRealIndex(cart, index);
|
|
1291
|
+
if (realIndex > -1) {
|
|
1292
|
+
const item = cart[realIndex];
|
|
1293
|
+
const newQuantity = Math.max(item.quantity - item.increment, 0);
|
|
1294
|
+
if (newQuantity === 0 || newQuantity < item.min) {
|
|
1295
|
+
cart.splice(realIndex, 1);
|
|
1296
|
+
cart = cart.map((i, _index) => (Object.assign(Object.assign({}, i), { index: _index })));
|
|
1297
|
+
}
|
|
1298
|
+
else {
|
|
1299
|
+
const newItem = calcPricesKiosk(Object.assign(Object.assign({}, item), { quantity: newQuantity }));
|
|
1300
|
+
cart[realIndex] = newItem;
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
const cartCounts = calcCartCounts(cart);
|
|
1304
|
+
return { cart, cartCounts };
|
|
1305
|
+
};
|
package/package.json
CHANGED