@open-tender/utils 0.5.75 → 0.5.77
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/cart.test.js +40 -0
- package/dist/cjs/__tests__/utils/itemMaxQuantity.test.d.ts +1 -0
- package/dist/cjs/__tests__/utils/itemMaxQuantity.test.js +177 -0
- package/dist/cjs/__tests__/utils/makeSimpleCart.test.d.ts +1 -0
- package/dist/cjs/__tests__/utils/makeSimpleCart.test.js +135 -0
- package/dist/cjs/hooks/useBuilder.d.ts +1 -1
- package/dist/cjs/hooks/useBuilder.js +9 -5
- package/dist/cjs/utils/cart.d.ts +5 -0
- package/dist/cjs/utils/cart.js +31 -13
- package/dist/cjs/utils/index.d.ts +1 -0
- package/dist/cjs/utils/index.js +1 -0
- package/dist/cjs/utils/itemMaxQuantity.d.ts +47 -0
- package/dist/cjs/utils/itemMaxQuantity.js +99 -0
- package/dist/esm/__tests__/utils/cart.test.js +41 -1
- package/dist/esm/__tests__/utils/itemMaxQuantity.test.d.ts +1 -0
- package/dist/esm/__tests__/utils/itemMaxQuantity.test.js +175 -0
- package/dist/esm/__tests__/utils/makeSimpleCart.test.d.ts +1 -0
- package/dist/esm/__tests__/utils/makeSimpleCart.test.js +133 -0
- package/dist/esm/hooks/useBuilder.d.ts +1 -1
- package/dist/esm/hooks/useBuilder.js +10 -6
- package/dist/esm/utils/cart.d.ts +5 -0
- package/dist/esm/utils/cart.js +29 -12
- package/dist/esm/utils/index.d.ts +1 -0
- package/dist/esm/utils/index.js +1 -0
- package/dist/esm/utils/itemMaxQuantity.d.ts +47 -0
- package/dist/esm/utils/itemMaxQuantity.js +87 -0
- package/package.json +1 -1
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { makeMenuItemLookup } from './favorites';
|
|
2
|
+
export const OWNER_PARTICIPANT = 'owner';
|
|
3
|
+
export const makeMaxQuantityMessage = (max, name) => `Maximum ${max} ${name} allowed.`;
|
|
4
|
+
export const getCartParticipant = (line, byParticipant) => byParticipant && line.cartGuestId
|
|
5
|
+
? `guest-${line.cartGuestId}`
|
|
6
|
+
: OWNER_PARTICIPANT;
|
|
7
|
+
// The maximum on the current menu wins over the one saved on a cart line,
|
|
8
|
+
// which can be out of date for a cart restored from the browser.
|
|
9
|
+
export const makeMaxQuantityLookup = (categories) => {
|
|
10
|
+
const menuItems = categories && categories.length ? makeMenuItemLookup(categories) : null;
|
|
11
|
+
return item => {
|
|
12
|
+
const menuItem = menuItems ? menuItems[item.id] : null;
|
|
13
|
+
return menuItem ? menuItem.max_quantity || 0 : item.max || 0;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export const getItemQuantityInCart = (cart, line, options = {}) => {
|
|
17
|
+
const { byParticipant, excludeIndex } = options;
|
|
18
|
+
const participant = getCartParticipant(line, byParticipant);
|
|
19
|
+
return (cart || []).reduce((total, i) => {
|
|
20
|
+
const skip = i.id !== line.id ||
|
|
21
|
+
getCartParticipant(i, byParticipant) !== participant ||
|
|
22
|
+
(excludeIndex !== undefined && i.index === excludeIndex);
|
|
23
|
+
return skip ? total : total + i.quantity;
|
|
24
|
+
}, 0);
|
|
25
|
+
};
|
|
26
|
+
// How many more of an item can go in the cart, null when there is no limit.
|
|
27
|
+
export const getRemainingAllowance = (cart, item, getMax, options = {}) => {
|
|
28
|
+
const max = getMax(item);
|
|
29
|
+
if (!max)
|
|
30
|
+
return null;
|
|
31
|
+
const inCart = getItemQuantityInCart(cart, item, Object.assign(Object.assign({}, options), { excludeIndex: item.index }));
|
|
32
|
+
return Math.max(max - inCart, 0);
|
|
33
|
+
};
|
|
34
|
+
const check = (max, total, name) => max && total > max
|
|
35
|
+
? { allowed: false, message: makeMaxQuantityMessage(max, name) }
|
|
36
|
+
: { allowed: true, message: null };
|
|
37
|
+
// An item with an index replaces that cart line, so the line is not counted
|
|
38
|
+
// twice, and a line can always be saved with no more than it already has.
|
|
39
|
+
export const checkItemAddition = (cart, item, getMax, options = {}) => {
|
|
40
|
+
const previous = item.index !== undefined
|
|
41
|
+
? (cart || []).find(i => i.index === item.index)
|
|
42
|
+
: undefined;
|
|
43
|
+
if (previous && item.quantity <= previous.quantity) {
|
|
44
|
+
return { allowed: true, message: null };
|
|
45
|
+
}
|
|
46
|
+
const inCart = getItemQuantityInCart(cart, item, Object.assign(Object.assign({}, options), { excludeIndex: item.index }));
|
|
47
|
+
return check(getMax(item), inCart + item.quantity, item.name);
|
|
48
|
+
};
|
|
49
|
+
export const checkItemIncrement = (cart, item, getMax, options = {}) => {
|
|
50
|
+
const inCart = getItemQuantityInCart(cart, item, options);
|
|
51
|
+
return check(getMax(item), inCart + (item.increment || 1), item.name);
|
|
52
|
+
};
|
|
53
|
+
// Every item that is over its maximum, once per participant.
|
|
54
|
+
export const findItemsOverMax = (cart, getMax, options = {}) => {
|
|
55
|
+
const totals = (cart || []).reduce((obj, line) => {
|
|
56
|
+
const key = `${getCartParticipant(line, options.byParticipant)}:${line.id}`;
|
|
57
|
+
const entry = obj[key] || { line, quantity: 0 };
|
|
58
|
+
return Object.assign(Object.assign({}, obj), { [key]: Object.assign(Object.assign({}, entry), { quantity: entry.quantity + line.quantity }) });
|
|
59
|
+
}, {});
|
|
60
|
+
return Object.values(totals).reduce((arr, entry) => {
|
|
61
|
+
const max = getMax(entry.line);
|
|
62
|
+
if (!max || entry.quantity <= max)
|
|
63
|
+
return arr;
|
|
64
|
+
const { id, name } = entry.line;
|
|
65
|
+
const message = makeMaxQuantityMessage(max, name);
|
|
66
|
+
return [...arr, { id, name, max, quantity: entry.quantity, message }];
|
|
67
|
+
}, []);
|
|
68
|
+
};
|
|
69
|
+
// The quantity after the builder's + button. quantityLimit is how high this
|
|
70
|
+
// item can go in the cart:
|
|
71
|
+
// - undefined: keep the item's own maximum, as before
|
|
72
|
+
// - null: no limit
|
|
73
|
+
// - a number: the allowance left, 0 meaning none. An increment that would go
|
|
74
|
+
// over it is refused as a whole and never lowers the quantity.
|
|
75
|
+
export const getIncrementedQuantity = (item, quantityLimit) => {
|
|
76
|
+
const next = item.quantity + item.increment;
|
|
77
|
+
if (quantityLimit === undefined) {
|
|
78
|
+
return {
|
|
79
|
+
quantity: item.max ? Math.min(next, item.max) : next,
|
|
80
|
+
refused: false
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
if (quantityLimit === null || next <= quantityLimit) {
|
|
84
|
+
return { quantity: next, refused: false };
|
|
85
|
+
}
|
|
86
|
+
return { quantity: item.quantity, refused: true };
|
|
87
|
+
};
|
package/package.json
CHANGED