@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 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const utils_1 = require("../../utils");
|
|
4
|
+
const makeOption = ({ id, name, price = '0.00', calories, isDefault = false, groups = [] }) => ({
|
|
5
|
+
id,
|
|
6
|
+
name,
|
|
7
|
+
price,
|
|
8
|
+
allergens: '',
|
|
9
|
+
tags: '',
|
|
10
|
+
opt_is_default: isDefault,
|
|
11
|
+
min_quantity: 0,
|
|
12
|
+
max_quantity: 10,
|
|
13
|
+
increment: 1,
|
|
14
|
+
suspend_until: null,
|
|
15
|
+
small_image_url: null,
|
|
16
|
+
nutritional_info: calories === undefined ? null : { calories, sodium: calories * 2 },
|
|
17
|
+
option_groups: groups
|
|
18
|
+
});
|
|
19
|
+
const makeGroup = (id, options, included = 0) => ({
|
|
20
|
+
id,
|
|
21
|
+
name: `group ${id}`,
|
|
22
|
+
included_options: included,
|
|
23
|
+
min_options: 0,
|
|
24
|
+
max_options: 0,
|
|
25
|
+
option_items: options
|
|
26
|
+
});
|
|
27
|
+
const makeMenuItem = (id, name, groups = [], calories = 500) => ({
|
|
28
|
+
id,
|
|
29
|
+
name,
|
|
30
|
+
price: '10.00',
|
|
31
|
+
allergens: '',
|
|
32
|
+
tags: '',
|
|
33
|
+
increment: 1,
|
|
34
|
+
min_quantity: 1,
|
|
35
|
+
max_quantity: 10,
|
|
36
|
+
suspend_until: null,
|
|
37
|
+
large_image_url: `large-${id}.png`,
|
|
38
|
+
small_image_url: `small-${id}.png`,
|
|
39
|
+
upsell_items: [],
|
|
40
|
+
nutritional_info: calories === null ? null : { calories, sodium: calories * 2 },
|
|
41
|
+
option_groups: groups
|
|
42
|
+
});
|
|
43
|
+
const findOption = (item, id) => {
|
|
44
|
+
const options = item.groups.flatMap(g => g.options);
|
|
45
|
+
return options.find(o => o.id === id);
|
|
46
|
+
};
|
|
47
|
+
const select = (item, quantities) => {
|
|
48
|
+
const update = (node) => (Object.assign(Object.assign({}, node), { groups: node.groups.map(g => (Object.assign(Object.assign({}, g), { options: g.options.map(o => { var _a; return update(Object.assign(Object.assign({}, o), { quantity: (_a = quantities[o.id]) !== null && _a !== void 0 ? _a : o.quantity })); }) }))) }));
|
|
49
|
+
return update(item);
|
|
50
|
+
};
|
|
51
|
+
const sandwich = makeMenuItem(1, 'Roast Beef', [
|
|
52
|
+
makeGroup(10, [
|
|
53
|
+
makeOption({ id: 11, name: 'Remove Cheese', calories: -80 }),
|
|
54
|
+
makeOption({ id: 12, name: 'Add Bacon', price: '1.50', calories: 90 })
|
|
55
|
+
])
|
|
56
|
+
]);
|
|
57
|
+
describe('calcPricesKiosk', () => {
|
|
58
|
+
it('prices an item the way calcPrices does', () => {
|
|
59
|
+
const item = select((0, utils_1.makeOrderItem)(sandwich), { 12: 2 });
|
|
60
|
+
const priced = (0, utils_1.calcPricesKiosk)(item);
|
|
61
|
+
expect(priced.totalPrice).toBe((0, utils_1.calcPrices)(item).totalPrice);
|
|
62
|
+
expect(priced.totalPrice).toBe(13);
|
|
63
|
+
});
|
|
64
|
+
it('counts the calories of a "Remove" option until it is selected', () => {
|
|
65
|
+
const item = (0, utils_1.makeOrderItem)(sandwich);
|
|
66
|
+
const notRemoved = (0, utils_1.calcPricesKiosk)(item);
|
|
67
|
+
expect(findOption(notRemoved, 11).totalCals).toBe(80);
|
|
68
|
+
expect(notRemoved.totalCals).toBe(580);
|
|
69
|
+
const removed = (0, utils_1.calcPricesKiosk)(select(item, { 11: 1 }));
|
|
70
|
+
expect(findOption(removed, 11).totalCals).toBe(0);
|
|
71
|
+
expect(removed.totalCals).toBe(500);
|
|
72
|
+
// calcPrices is not changed
|
|
73
|
+
expect((0, utils_1.calcPrices)(item).totalCals).toBe(500);
|
|
74
|
+
expect((0, utils_1.calcPrices)(select(item, { 11: 1 })).totalCals).toBe(420);
|
|
75
|
+
});
|
|
76
|
+
it('totals the calories of the options of an item that has none', () => {
|
|
77
|
+
const menuItem = makeMenuItem(2, 'Build Your Own', [makeGroup(20, [makeOption({ id: 21, name: 'Beef', calories: 200 })])], null);
|
|
78
|
+
const item = select((0, utils_1.makeOrderItem)(menuItem), { 21: 1 });
|
|
79
|
+
expect((0, utils_1.calcPrices)(item).totalCals).toBeNull();
|
|
80
|
+
expect((0, utils_1.calcPricesKiosk)(item).totalCals).toBe(200);
|
|
81
|
+
expect((0, utils_1.calcPricesKiosk)(select(item, { 21: 0 })).totalCals).toBeNull();
|
|
82
|
+
});
|
|
83
|
+
it('adds the price deltas to the options of a meal', () => {
|
|
84
|
+
const meal = makeMenuItem(3, 'Roast Beef Meal', [
|
|
85
|
+
makeGroup(30, [
|
|
86
|
+
makeOption({ id: 31, name: 'Small Fries', isDefault: true }),
|
|
87
|
+
makeOption({
|
|
88
|
+
id: 32,
|
|
89
|
+
name: 'Large Meal Fries',
|
|
90
|
+
price: '1.00',
|
|
91
|
+
groups: [
|
|
92
|
+
makeGroup(40, [
|
|
93
|
+
makeOption({ id: 41, name: 'Cheese Sauce', price: '0.50' }),
|
|
94
|
+
makeOption({ id: 42, name: 'Salt', isDefault: true })
|
|
95
|
+
])
|
|
96
|
+
]
|
|
97
|
+
})
|
|
98
|
+
]),
|
|
99
|
+
makeGroup(50, [makeOption({ id: 51, name: 'Cookie', price: '2.00' })])
|
|
100
|
+
]);
|
|
101
|
+
const priced = (0, utils_1.calcPricesKiosk)(select((0, utils_1.makeOrderItem)(meal), { 41: 2 }));
|
|
102
|
+
expect(findOption(priced, 31)).toMatchObject({
|
|
103
|
+
priceDelta: 0,
|
|
104
|
+
userExtra: 0,
|
|
105
|
+
displayDelta: 0,
|
|
106
|
+
hasDefaultSibling: true
|
|
107
|
+
});
|
|
108
|
+
// 1.00 over the default option, plus 2 x 0.50 added inside it
|
|
109
|
+
expect(findOption(priced, 32)).toMatchObject({
|
|
110
|
+
priceDelta: 1,
|
|
111
|
+
userExtra: 1,
|
|
112
|
+
displayDelta: 2,
|
|
113
|
+
hasDefaultSibling: true
|
|
114
|
+
});
|
|
115
|
+
// a group without a default option shows the price of the option
|
|
116
|
+
expect(findOption(priced, 51)).toMatchObject({
|
|
117
|
+
priceDelta: null,
|
|
118
|
+
displayDelta: 2,
|
|
119
|
+
hasDefaultSibling: false
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
it('does not add the price deltas to an item that is not a meal', () => {
|
|
123
|
+
const priced = (0, utils_1.calcPricesKiosk)((0, utils_1.makeOrderItem)(sandwich));
|
|
124
|
+
expect(findOption(priced, 12).displayDelta).toBeUndefined();
|
|
125
|
+
});
|
|
126
|
+
it('prices an item that calcPrices priced before', () => {
|
|
127
|
+
const item = select((0, utils_1.makeOrderItem)(sandwich), { 12: 3 });
|
|
128
|
+
expect((0, utils_1.calcPricesKiosk)((0, utils_1.calcPrices)(item))).toEqual((0, utils_1.calcPricesKiosk)(item));
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
describe('calcNutritionalInfoKiosk', () => {
|
|
132
|
+
it('counts a "Remove" option until it is selected', () => {
|
|
133
|
+
const item = (0, utils_1.makeOrderItem)(sandwich);
|
|
134
|
+
expect((0, utils_1.calcNutritionalInfoKiosk)(item)).toMatchObject({
|
|
135
|
+
calories: 580,
|
|
136
|
+
sodium: 1160
|
|
137
|
+
});
|
|
138
|
+
expect((0, utils_1.calcNutritionalInfoKiosk)(select(item, { 11: 1, 12: 1 }))).toMatchObject({ calories: 590, sodium: 1180 });
|
|
139
|
+
// calcNutritionalInfo is not changed
|
|
140
|
+
expect((0, utils_1.calcNutritionalInfo)(item)).toMatchObject({ calories: 500 });
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
describe('kiosk cart functions', () => {
|
|
144
|
+
it('makes an item with its small image and kiosk prices', () => {
|
|
145
|
+
const item = (0, utils_1.makeOrderItemKiosk)(sandwich);
|
|
146
|
+
expect(item.smallImageUrl).toBe('small-1.png');
|
|
147
|
+
expect(item.imageUrl).toBe('large-1.png');
|
|
148
|
+
expect(item.totalCals).toBe(580);
|
|
149
|
+
const [upsell] = (0, utils_1.makeUpsellItemsKiosk)([1], { 1: sandwich });
|
|
150
|
+
expect(upsell).toEqual(item);
|
|
151
|
+
});
|
|
152
|
+
it('rehydrates items with kiosk prices', () => {
|
|
153
|
+
const built = select((0, utils_1.makeOrderItemKiosk)(sandwich), { 12: 1 });
|
|
154
|
+
const [simpleItem] = (0, utils_1.makeSimpleCart)([Object.assign(Object.assign({}, built), { quantity: 2 })]);
|
|
155
|
+
const item = (0, utils_1.rehydrateOrderItemKiosk)(sandwich, simpleItem);
|
|
156
|
+
expect(item.quantity).toBe(2);
|
|
157
|
+
expect(item.totalPrice).toBe(23);
|
|
158
|
+
expect(item.totalCals).toBe(1340);
|
|
159
|
+
const { cart, cartCounts } = (0, utils_1.rehydrateCartKiosk)([sandwich], [simpleItem]);
|
|
160
|
+
expect(cart).toEqual([Object.assign(Object.assign({}, item), { index: 0 })]);
|
|
161
|
+
expect(cartCounts).toEqual({ 1: 2 });
|
|
162
|
+
});
|
|
163
|
+
it('keeps kiosk prices when the quantity of a cart item changes', () => {
|
|
164
|
+
const item = (0, utils_1.makeOrderItemKiosk)(sandwich);
|
|
165
|
+
let cart = (0, utils_1.addItemKiosk)([], item).cart;
|
|
166
|
+
cart = (0, utils_1.addItemKiosk)([...cart], item).cart;
|
|
167
|
+
expect(cart).toHaveLength(1);
|
|
168
|
+
expect(cart[0].quantity).toBe(2);
|
|
169
|
+
expect(cart[0].totalCals).toBe(1160);
|
|
170
|
+
cart = (0, utils_1.incrementItemKiosk)([...cart], 0).cart;
|
|
171
|
+
expect(cart[0].totalCals).toBe(1740);
|
|
172
|
+
cart = (0, utils_1.decrementItemKiosk)([...cart], 0).cart;
|
|
173
|
+
expect(cart[0].totalCals).toBe(1160);
|
|
174
|
+
expect(cart[0].totalPrice).toBe(20);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
@@ -133,3 +133,41 @@ describe('group order checkout payload', () => {
|
|
|
133
133
|
expect(payload.cart).toEqual(GROUP_CHECKOUT_CART.slice(1));
|
|
134
134
|
});
|
|
135
135
|
});
|
|
136
|
+
describe('existing order payload', () => {
|
|
137
|
+
const orderData = {
|
|
138
|
+
address: null,
|
|
139
|
+
cart: [],
|
|
140
|
+
cartId: null,
|
|
141
|
+
customer: null,
|
|
142
|
+
deposit: null,
|
|
143
|
+
details: null,
|
|
144
|
+
deviceType: null,
|
|
145
|
+
discounts: null,
|
|
146
|
+
orderId: null,
|
|
147
|
+
points: null,
|
|
148
|
+
prepType: null,
|
|
149
|
+
promoCodes: null,
|
|
150
|
+
requestedAt: 'asap',
|
|
151
|
+
revenueCenterId: 3,
|
|
152
|
+
serviceType: 'PICKUP',
|
|
153
|
+
surcharges: null,
|
|
154
|
+
table: null,
|
|
155
|
+
tenders: null,
|
|
156
|
+
tip: null
|
|
157
|
+
};
|
|
158
|
+
it('sends the order uuid together with the order id', () => {
|
|
159
|
+
const payload = (0, utils_1.prepareOrder)(Object.assign(Object.assign({}, orderData), { orderId: 42, orderUuid: 'abc-123' }));
|
|
160
|
+
expect(payload.order_id).toBe(42);
|
|
161
|
+
expect(payload.order_uuid).toBe('abc-123');
|
|
162
|
+
});
|
|
163
|
+
it('omits the order uuid without an order id', () => {
|
|
164
|
+
const payload = (0, utils_1.prepareOrder)(Object.assign(Object.assign({}, orderData), { orderUuid: 'abc-123' }));
|
|
165
|
+
expect(payload).not.toHaveProperty('order_id');
|
|
166
|
+
expect(payload).not.toHaveProperty('order_uuid');
|
|
167
|
+
});
|
|
168
|
+
it('omits the order uuid when it is null', () => {
|
|
169
|
+
const payload = (0, utils_1.prepareOrder)(Object.assign(Object.assign({}, orderData), { orderId: 42, orderUuid: null }));
|
|
170
|
+
expect(payload.order_id).toBe(42);
|
|
171
|
+
expect(payload).not.toHaveProperty('order_uuid');
|
|
172
|
+
});
|
|
173
|
+
});
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
export * from './types';
|
|
2
2
|
export * from './useAccordion';
|
|
3
|
+
export * from './useAccordionKiosk';
|
|
3
4
|
export * from './useAddressForm';
|
|
4
5
|
export * from './useAllergenForm';
|
|
5
6
|
export * from './useBuilder';
|
|
7
|
+
export * from './useBuilderKiosk';
|
|
6
8
|
export * from './useCartGuestForm';
|
|
7
9
|
export * from './useCartPoints';
|
|
8
10
|
export * from './useClaimRewardCodeForm';
|
package/dist/cjs/hooks/index.js
CHANGED
|
@@ -3,9 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
tslib_1.__exportStar(require("./types"), exports);
|
|
5
5
|
tslib_1.__exportStar(require("./useAccordion"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./useAccordionKiosk"), exports);
|
|
6
7
|
tslib_1.__exportStar(require("./useAddressForm"), exports);
|
|
7
8
|
tslib_1.__exportStar(require("./useAllergenForm"), exports);
|
|
8
9
|
tslib_1.__exportStar(require("./useBuilder"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./useBuilderKiosk"), exports);
|
|
9
11
|
tslib_1.__exportStar(require("./useCartGuestForm"), exports);
|
|
10
12
|
tslib_1.__exportStar(require("./useCartPoints"), exports);
|
|
11
13
|
tslib_1.__exportStar(require("./useClaimRewardCodeForm"), exports);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { CartItem, CartLevels, ConfigAccordion } from '@open-tender/types';
|
|
3
|
+
export declare const useAccordionKiosk: (builtItem: CartItem, toggleOption: (levels: CartLevels) => void, cartId?: number | null, config?: ConfigAccordion, setImageUrl?: ((imageUrl: string | null) => void) | undefined) => {
|
|
4
|
+
open: string | null;
|
|
5
|
+
setOpen: import("react").Dispatch<import("react").SetStateAction<string | null>>;
|
|
6
|
+
groups: import("@open-tender/types").CartItemGroups;
|
|
7
|
+
ingredients: string | null;
|
|
8
|
+
totalCals: number | null;
|
|
9
|
+
madeFor: string | null;
|
|
10
|
+
notes: string | null;
|
|
11
|
+
hasMadeFor: boolean;
|
|
12
|
+
hasNotes: boolean;
|
|
13
|
+
hasInstructions: boolean;
|
|
14
|
+
hasCals: boolean;
|
|
15
|
+
nutritionalInfo: import("@open-tender/types").NutritionalInfo | null;
|
|
16
|
+
hasIngredients: boolean;
|
|
17
|
+
instructionsTitle: string;
|
|
18
|
+
sizeGroup: import("@open-tender/types").CartItemGroup | null | undefined;
|
|
19
|
+
selectedSize: import("@open-tender/types").CartItemOption | null | undefined;
|
|
20
|
+
showAccordion: boolean | import("@open-tender/types").CartItemGroup;
|
|
21
|
+
toggleSize: (optionId: number) => void;
|
|
22
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useAccordionKiosk = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const utils_1 = require("../utils");
|
|
6
|
+
// The kiosk variant of useAccordion, left as it is for the other apps:
|
|
7
|
+
// - the nutritional info comes from calcNutritionalInfoKiosk
|
|
8
|
+
// - setImageUrl, when given, gets the image of the size the guest picks
|
|
9
|
+
const useAccordionKiosk = (builtItem, toggleOption, cartId, config, setImageUrl) => {
|
|
10
|
+
var _a;
|
|
11
|
+
const { hasCals: showCals = false, hasCustomize = false, hasMadeFor: showMadeFor = true, hasNotes: showNotes = true } = config || {};
|
|
12
|
+
const [open, setOpen] = (0, react_1.useState)(null);
|
|
13
|
+
const { groups, ingredients, totalCals, madeFor, notes } = builtItem;
|
|
14
|
+
const hasMadeFor = showMadeFor && !cartId ? true : false;
|
|
15
|
+
const hasNotes = showNotes ? true : false;
|
|
16
|
+
const hasInstructions = hasMadeFor || hasNotes;
|
|
17
|
+
const hasCals = showCals && totalCals ? true : false;
|
|
18
|
+
const nutritionalInfo = hasCals ? (0, utils_1.calcNutritionalInfoKiosk)(builtItem) : null;
|
|
19
|
+
const hasIngredients = ingredients && ingredients.length > 0 ? true : false;
|
|
20
|
+
const instructionsTitle = hasMadeFor && hasNotes
|
|
21
|
+
? 'Name / Special Instructions'
|
|
22
|
+
: hasMadeFor
|
|
23
|
+
? 'Name'
|
|
24
|
+
: hasNotes
|
|
25
|
+
? 'Special Instructions'
|
|
26
|
+
: '';
|
|
27
|
+
const sizeGroup = groups ? groups.find(i => i.isSize) : null;
|
|
28
|
+
const selectedSize = sizeGroup
|
|
29
|
+
? (_a = sizeGroup === null || sizeGroup === void 0 ? void 0 : sizeGroup.options) === null || _a === void 0 ? void 0 : _a.find(i => { var _a; return ((_a = i.quantity) !== null && _a !== void 0 ? _a : 0) >= 1; })
|
|
30
|
+
: null;
|
|
31
|
+
const showAccordion = sizeGroup || hasCustomize || hasInstructions || hasIngredients || hasCals;
|
|
32
|
+
const toggleSize = (optionId) => {
|
|
33
|
+
if (sizeGroup) {
|
|
34
|
+
toggleOption([[sizeGroup.id, optionId]]);
|
|
35
|
+
if (setImageUrl) {
|
|
36
|
+
const selectedOption = sizeGroup.options.find(i => i.id === optionId);
|
|
37
|
+
if (selectedOption && selectedOption.imageUrl) {
|
|
38
|
+
setImageUrl(selectedOption.imageUrl);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
setOpen(null);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
return {
|
|
45
|
+
open,
|
|
46
|
+
setOpen,
|
|
47
|
+
groups,
|
|
48
|
+
ingredients,
|
|
49
|
+
totalCals,
|
|
50
|
+
madeFor,
|
|
51
|
+
notes,
|
|
52
|
+
hasMadeFor,
|
|
53
|
+
hasNotes,
|
|
54
|
+
hasInstructions,
|
|
55
|
+
hasCals,
|
|
56
|
+
nutritionalInfo,
|
|
57
|
+
hasIngredients,
|
|
58
|
+
instructionsTitle,
|
|
59
|
+
sizeGroup,
|
|
60
|
+
selectedSize,
|
|
61
|
+
showAccordion,
|
|
62
|
+
toggleSize
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
exports.useAccordionKiosk = useAccordionKiosk;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { CartItem, CartItemGroups, CartLevels } from '@open-tender/types';
|
|
2
|
+
export declare const useBuilderKiosk: (orderItem: CartItem, orderTimeoutCallback?: () => void, quantityLimit?: number | null, onQuantityLimit?: () => void) => {
|
|
3
|
+
item: CartItem;
|
|
4
|
+
increment: () => void;
|
|
5
|
+
decrement: () => void;
|
|
6
|
+
setQuantity: (quantity: number) => void;
|
|
7
|
+
setMadeFor: (madeFor: string) => void;
|
|
8
|
+
setNotes: (notes: string) => void;
|
|
9
|
+
setMadeForNotes: (madeFor: string, notes: string) => void;
|
|
10
|
+
toggleOption: (levels: CartLevels) => CartItem;
|
|
11
|
+
incrementOption: (levels: CartLevels) => CartItem;
|
|
12
|
+
decrementOption: (levels: CartLevels) => CartItem;
|
|
13
|
+
setOptionQuantity: (levels: CartLevels, quantity: number) => CartItem;
|
|
14
|
+
setGroups: (groups: CartItemGroups) => void;
|
|
15
|
+
setImageUrl: (imageUrl: string | null) => void;
|
|
16
|
+
};
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useBuilderKiosk = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const utils_1 = require("../utils");
|
|
6
|
+
// The kiosk variant of useBuilder, left as it is for the other apps:
|
|
7
|
+
// - items are priced with calcPricesKiosk
|
|
8
|
+
// - setOptionQuantity can set the quantity of an option of a full group, and
|
|
9
|
+
// of a nested option (the quantity was dropped below the first level)
|
|
10
|
+
// - setGroups and setImageUrl are added
|
|
11
|
+
// quantityLimit is how high the item can go in the cart (see
|
|
12
|
+
// getIncrementedQuantity), onQuantityLimit is called when + is refused.
|
|
13
|
+
const useBuilderKiosk = (orderItem, orderTimeoutCallback, quantityLimit, onQuantityLimit) => {
|
|
14
|
+
const [item, setItem] = (0, react_1.useState)(orderItem);
|
|
15
|
+
(0, react_1.useEffect)(() => {
|
|
16
|
+
orderTimeoutCallback && orderTimeoutCallback();
|
|
17
|
+
}, [item, orderTimeoutCallback]);
|
|
18
|
+
const increment = () => {
|
|
19
|
+
const { quantity, refused } = (0, utils_1.getIncrementedQuantity)(item, quantityLimit);
|
|
20
|
+
if (refused) {
|
|
21
|
+
onQuantityLimit === null || onQuantityLimit === void 0 ? void 0 : onQuantityLimit();
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const pricedItem = (0, utils_1.calcPricesKiosk)(Object.assign(Object.assign({}, item), { quantity }));
|
|
25
|
+
setItem(pricedItem);
|
|
26
|
+
};
|
|
27
|
+
const decrement = () => {
|
|
28
|
+
const newQuantity = Math.max(item.quantity - item.increment, item.min);
|
|
29
|
+
const pricedItem = (0, utils_1.calcPricesKiosk)(Object.assign(Object.assign({}, item), { quantity: newQuantity }));
|
|
30
|
+
setItem(pricedItem);
|
|
31
|
+
};
|
|
32
|
+
const setQuantity = (quantity) => {
|
|
33
|
+
const pricedItem = (0, utils_1.calcPricesKiosk)(Object.assign(Object.assign({}, item), { quantity }));
|
|
34
|
+
setItem(pricedItem);
|
|
35
|
+
};
|
|
36
|
+
const setMadeFor = (madeFor) => {
|
|
37
|
+
setItem(Object.assign(Object.assign({}, item), { madeFor }));
|
|
38
|
+
};
|
|
39
|
+
const setNotes = (notes) => {
|
|
40
|
+
setItem(prevItem => (Object.assign(Object.assign({}, prevItem), { notes })));
|
|
41
|
+
};
|
|
42
|
+
const setMadeForNotes = (madeFor, notes) => {
|
|
43
|
+
setItem(Object.assign(Object.assign({}, item), { madeFor, notes }));
|
|
44
|
+
};
|
|
45
|
+
const toggleGroupOption = (group, optionId) => {
|
|
46
|
+
const options = group.options.map(option => {
|
|
47
|
+
return Object.assign(Object.assign({}, option), { quantity: option.id === optionId ? 1 : 0 });
|
|
48
|
+
});
|
|
49
|
+
return Object.assign(Object.assign({}, group), { options });
|
|
50
|
+
};
|
|
51
|
+
const incrementGroupOption = (group, optionId) => {
|
|
52
|
+
const count = group.options
|
|
53
|
+
.filter(o => o.id !== optionId)
|
|
54
|
+
.reduce((t, o) => t + o.quantity, 0);
|
|
55
|
+
if (group.max !== 0 && count >= group.max)
|
|
56
|
+
return group;
|
|
57
|
+
const options = group.options.map(option => {
|
|
58
|
+
if (option.id === optionId) {
|
|
59
|
+
let quantity = option.quantity + option.increment;
|
|
60
|
+
const quantities = [quantity];
|
|
61
|
+
if (option.max !== 0)
|
|
62
|
+
quantities.push(option.max);
|
|
63
|
+
if (group.max !== 0)
|
|
64
|
+
quantities.push(group.max - count);
|
|
65
|
+
quantity = Math.min(...quantities);
|
|
66
|
+
return Object.assign(Object.assign({}, option), { quantity });
|
|
67
|
+
}
|
|
68
|
+
return option;
|
|
69
|
+
});
|
|
70
|
+
return Object.assign(Object.assign({}, group), { options });
|
|
71
|
+
};
|
|
72
|
+
const decrementGroupOption = (group, optionId) => {
|
|
73
|
+
const options = group.options.map(option => {
|
|
74
|
+
if (option.id === optionId) {
|
|
75
|
+
const quantity = Math.max(option.quantity - option.increment, 0);
|
|
76
|
+
return Object.assign(Object.assign({}, option), { quantity });
|
|
77
|
+
}
|
|
78
|
+
return option;
|
|
79
|
+
});
|
|
80
|
+
return Object.assign(Object.assign({}, group), { options });
|
|
81
|
+
};
|
|
82
|
+
const setGroupOptionQuantity = (group, optionId, quantity) => {
|
|
83
|
+
if (quantity === undefined)
|
|
84
|
+
return group;
|
|
85
|
+
const count = group.options
|
|
86
|
+
.filter(o => o.id !== optionId)
|
|
87
|
+
.reduce((t, o) => t + o.quantity, 0);
|
|
88
|
+
const options = group.options.map(option => {
|
|
89
|
+
if (option.id === optionId) {
|
|
90
|
+
const quantities = [quantity];
|
|
91
|
+
if (option.max !== 0)
|
|
92
|
+
quantities.push(option.max);
|
|
93
|
+
if (group.max !== 0)
|
|
94
|
+
quantities.push(group.max - count);
|
|
95
|
+
const limited = Math.min(...quantities);
|
|
96
|
+
return Object.assign(Object.assign({}, option), { quantity: Math.max(limited, option.min) });
|
|
97
|
+
}
|
|
98
|
+
return option;
|
|
99
|
+
});
|
|
100
|
+
return Object.assign(Object.assign({}, group), { options });
|
|
101
|
+
};
|
|
102
|
+
const updateGroups = (groups, levels, fn, quantity) => {
|
|
103
|
+
const [[groupId, optionId], ...rest] = levels;
|
|
104
|
+
return groups.map(group => {
|
|
105
|
+
if (group.id === groupId) {
|
|
106
|
+
if (!rest.length)
|
|
107
|
+
return fn(group, optionId, quantity);
|
|
108
|
+
const options = group.options.map(option => {
|
|
109
|
+
if (option.id === optionId) {
|
|
110
|
+
return Object.assign(Object.assign({}, option), { groups: updateGroups(option.groups, rest, fn, quantity) });
|
|
111
|
+
}
|
|
112
|
+
return option;
|
|
113
|
+
});
|
|
114
|
+
return Object.assign(Object.assign({}, group), { options });
|
|
115
|
+
}
|
|
116
|
+
return group;
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
const toggleOption = (levels) => {
|
|
120
|
+
const groups = updateGroups(item.groups, levels, toggleGroupOption);
|
|
121
|
+
const pricedItem = (0, utils_1.calcPricesKiosk)(Object.assign(Object.assign({}, item), { groups: groups }));
|
|
122
|
+
setItem(pricedItem);
|
|
123
|
+
return pricedItem;
|
|
124
|
+
};
|
|
125
|
+
const incrementOption = (levels) => {
|
|
126
|
+
const groups = updateGroups(item.groups, levels, incrementGroupOption);
|
|
127
|
+
const pricedItem = (0, utils_1.calcPricesKiosk)(Object.assign(Object.assign({}, item), { groups: groups }));
|
|
128
|
+
setItem(pricedItem);
|
|
129
|
+
return pricedItem;
|
|
130
|
+
};
|
|
131
|
+
const decrementOption = (levels) => {
|
|
132
|
+
const groups = updateGroups(item.groups, levels, decrementGroupOption);
|
|
133
|
+
const pricedItem = (0, utils_1.calcPricesKiosk)(Object.assign(Object.assign({}, item), { groups: groups }));
|
|
134
|
+
setItem(pricedItem);
|
|
135
|
+
return pricedItem;
|
|
136
|
+
};
|
|
137
|
+
const setOptionQuantity = (levels, quantity) => {
|
|
138
|
+
const groups = updateGroups(item.groups, levels, setGroupOptionQuantity, quantity);
|
|
139
|
+
const pricedItem = (0, utils_1.calcPricesKiosk)(Object.assign(Object.assign({}, item), { groups: groups }));
|
|
140
|
+
setItem(pricedItem);
|
|
141
|
+
return pricedItem;
|
|
142
|
+
};
|
|
143
|
+
const setGroups = (groups) => {
|
|
144
|
+
const pricedItem = (0, utils_1.calcPricesKiosk)(Object.assign(Object.assign({}, item), { groups }));
|
|
145
|
+
setItem(pricedItem);
|
|
146
|
+
};
|
|
147
|
+
const setImageUrl = (imageUrl) => {
|
|
148
|
+
setItem(prevItem => (Object.assign(Object.assign({}, prevItem), { imageUrl })));
|
|
149
|
+
};
|
|
150
|
+
return {
|
|
151
|
+
item,
|
|
152
|
+
increment,
|
|
153
|
+
decrement,
|
|
154
|
+
setQuantity,
|
|
155
|
+
setMadeFor,
|
|
156
|
+
setNotes,
|
|
157
|
+
setMadeForNotes,
|
|
158
|
+
toggleOption,
|
|
159
|
+
incrementOption,
|
|
160
|
+
decrementOption,
|
|
161
|
+
setOptionQuantity,
|
|
162
|
+
setGroups,
|
|
163
|
+
setImageUrl
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
exports.useBuilderKiosk = useBuilderKiosk;
|
package/dist/cjs/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
|
+
};
|