@open-tender/utils 0.0.11 → 0.0.13

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.
@@ -0,0 +1 @@
1
+ export * from './menu';
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./menu"), exports);
@@ -0,0 +1,2 @@
1
+ export * from './useBuilder';
2
+ export * from './useOrderItem';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./useBuilder"), exports);
5
+ tslib_1.__exportStar(require("./useOrderItem"), exports);
@@ -0,0 +1,14 @@
1
+ import { CartItem, CartItemOptions } from '@open-tender/types';
2
+ declare const useBuilder: (orderItem: Partial<CartItem>) => {
3
+ item: Partial<CartItem>;
4
+ increment: () => void;
5
+ decrement: () => void;
6
+ setQuantity: (quantity: number) => void;
7
+ setMadeFor: (madeFor: string) => void;
8
+ setNotes: (notes: string) => void;
9
+ toggleOption: (groupId: number, optionId: number) => void;
10
+ incrementOption: (groupId: number, optionId: number) => void;
11
+ decrementOption: (groupId: number, optionId: number) => void;
12
+ setOptionQuantity: (groupId: number, optionId: number, quantity: number, nested: CartItemOptions) => void;
13
+ };
14
+ export default useBuilder;
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const utils_1 = require("../../utils");
5
+ const useBuilder = (orderItem) => {
6
+ const [item, setItem] = (0, react_1.useState)(orderItem);
7
+ const increment = () => {
8
+ var _a, _b, _c, _d;
9
+ const newQuantity = item.max
10
+ ? Math.min(((_a = item.quantity) !== null && _a !== void 0 ? _a : 0) + ((_b = item.increment) !== null && _b !== void 0 ? _b : 0), item.max)
11
+ : ((_c = item.quantity) !== null && _c !== void 0 ? _c : 0) + ((_d = item.increment) !== null && _d !== void 0 ? _d : 0);
12
+ setItem((0, utils_1.calcPrices)(Object.assign(Object.assign({}, item), { quantity: newQuantity })));
13
+ };
14
+ const decrement = () => {
15
+ var _a, _b, _c;
16
+ const newQuantity = Math.max(((_a = item.quantity) !== null && _a !== void 0 ? _a : 0) - ((_b = item.increment) !== null && _b !== void 0 ? _b : 0), ((_c = item.min) !== null && _c !== void 0 ? _c : 0));
17
+ setItem((0, utils_1.calcPrices)(Object.assign(Object.assign({}, item), { quantity: newQuantity })));
18
+ };
19
+ const setQuantity = (quantity) => {
20
+ setItem((0, utils_1.calcPrices)(Object.assign(Object.assign({}, item), { quantity: quantity })));
21
+ };
22
+ const setMadeFor = (madeFor) => {
23
+ setItem(Object.assign(Object.assign({}, item), { madeFor }));
24
+ };
25
+ const setNotes = (notes) => {
26
+ setItem(Object.assign(Object.assign({}, item), { notes }));
27
+ };
28
+ const toggleOption = (groupId, optionId) => {
29
+ var _a;
30
+ const groups = (_a = item === null || item === void 0 ? void 0 : item.groups) === null || _a === void 0 ? void 0 : _a.map((group) => {
31
+ var _a;
32
+ if (group.id === groupId) {
33
+ const options = (_a = group === null || group === void 0 ? void 0 : group.options) === null || _a === void 0 ? void 0 : _a.map((option) => {
34
+ const newQuantity = option.id === optionId ? 1 : 0;
35
+ return Object.assign(Object.assign({}, option), { quantity: newQuantity });
36
+ });
37
+ return Object.assign(Object.assign({}, group), { options });
38
+ }
39
+ return group;
40
+ });
41
+ setItem((0, utils_1.calcPrices)(Object.assign(Object.assign({}, item), { groups: groups })));
42
+ };
43
+ const incrementOption = (groupId, optionId) => {
44
+ var _a;
45
+ const groups = (_a = item === null || item === void 0 ? void 0 : item.groups) === null || _a === void 0 ? void 0 : _a.map((group) => {
46
+ var _a, _b, _c;
47
+ if (group.id === groupId) {
48
+ const count = (_a = group === null || group === void 0 ? void 0 : group.options) === null || _a === void 0 ? void 0 : _a.filter((o) => o.id !== optionId).reduce((t, o) => t + o.quantity, 0);
49
+ if (group.max !== 0 && (count !== null && count !== void 0 ? count : 0) >= ((_b = group.max) !== null && _b !== void 0 ? _b : 0))
50
+ return group;
51
+ const options = (_c = group.options) === null || _c === void 0 ? void 0 : _c.map((option) => {
52
+ var _a;
53
+ if (option.id === optionId) {
54
+ let quantity = option.quantity + option.increment;
55
+ const quantities = [quantity];
56
+ if (option.max !== 0)
57
+ quantities.push(option.max);
58
+ if (group.max !== 0)
59
+ quantities.push(((_a = group.max) !== null && _a !== void 0 ? _a : 0) - (count !== null && count !== void 0 ? count : 0));
60
+ quantity = Math.min(...quantities);
61
+ return Object.assign(Object.assign({}, option), { quantity });
62
+ }
63
+ return option;
64
+ });
65
+ return Object.assign(Object.assign({}, group), { options });
66
+ }
67
+ return group;
68
+ });
69
+ setItem((0, utils_1.calcPrices)(Object.assign(Object.assign({}, item), { groups: groups })));
70
+ };
71
+ const decrementOption = (groupId, optionId) => {
72
+ var _a;
73
+ const groups = (_a = item.groups) === null || _a === void 0 ? void 0 : _a.map((group) => {
74
+ var _a;
75
+ if (group.id === groupId) {
76
+ const options = (_a = group.options) === null || _a === void 0 ? void 0 : _a.map((option) => {
77
+ if (option.id === optionId) {
78
+ const quantity = Math.max(option.quantity - option.increment, 0);
79
+ return Object.assign(Object.assign({}, option), { quantity });
80
+ }
81
+ return option;
82
+ });
83
+ return Object.assign(Object.assign({}, group), { options });
84
+ }
85
+ return group;
86
+ });
87
+ setItem((0, utils_1.calcPrices)(Object.assign(Object.assign({}, item), { groups: groups })));
88
+ };
89
+ const setOptionQuantity = (groupId, optionId, quantity, nested) => {
90
+ var _a;
91
+ const groups = (_a = item.groups) === null || _a === void 0 ? void 0 : _a.map((group) => {
92
+ var _a, _b, _c;
93
+ if (group.id === groupId) {
94
+ const count = (_a = group.options) === null || _a === void 0 ? void 0 : _a.filter((o) => o.id !== optionId).reduce((t, o) => t + o.quantity, 0);
95
+ if (group.max !== 0 && (count !== null && count !== void 0 ? count : 0) >= ((_b = group.max) !== null && _b !== void 0 ? _b : 0))
96
+ return group;
97
+ const options = (_c = group.options) === null || _c === void 0 ? void 0 : _c.map((option) => {
98
+ var _a;
99
+ const optionGroups = nested || option.groups || [];
100
+ if (option.id === optionId) {
101
+ if (`${quantity}` === '') {
102
+ return Object.assign(Object.assign({}, option), { quantity, groups: optionGroups });
103
+ }
104
+ else {
105
+ const quantities = [quantity];
106
+ if (option.max !== 0)
107
+ quantities.push(option.max);
108
+ if (group.max !== 0)
109
+ quantities.push(((_a = group.max) !== null && _a !== void 0 ? _a : 0) - (count !== null && count !== void 0 ? count : 0));
110
+ quantity = Math.min(...quantities);
111
+ quantity = Math.max(quantity, option.min);
112
+ return Object.assign(Object.assign({}, option), { quantity, groups: optionGroups });
113
+ }
114
+ }
115
+ return option;
116
+ });
117
+ return Object.assign(Object.assign({}, group), { options });
118
+ }
119
+ return group;
120
+ });
121
+ setItem((0, utils_1.calcPrices)(Object.assign(Object.assign({}, item), { groups: groups })));
122
+ };
123
+ return {
124
+ item,
125
+ increment,
126
+ decrement,
127
+ setQuantity,
128
+ setMadeFor,
129
+ setNotes,
130
+ toggleOption,
131
+ incrementOption,
132
+ decrementOption,
133
+ setOptionQuantity,
134
+ };
135
+ };
136
+ exports.default = useBuilder;
@@ -0,0 +1,57 @@
1
+ import { CartCounts, Favorite, CartItem, SoldOut } from "@open-tender/types";
2
+ declare const useOrderItem: (orderItem: CartItem, favorite?: Favorite | null, soldOut?: SoldOut, allergenAlerts?: Array<string>, displaySettings?: Record<string, string>, cartCounts?: CartCounts, isMobile?: boolean) => {
3
+ showImage: string;
4
+ displayDesc: any;
5
+ displayTags: string[];
6
+ displayAllergens: string[];
7
+ displayPrice: string;
8
+ displayCals: string | null;
9
+ isIncomplete: boolean;
10
+ isSoldOut: boolean;
11
+ allergenAlert: string | null;
12
+ hasSelections: boolean;
13
+ showQuickAdd: string;
14
+ sizeOnly: boolean | undefined;
15
+ cartCount: number;
16
+ index?: number | undefined;
17
+ id?: number | undefined;
18
+ name?: string | undefined;
19
+ shortName?: string | undefined;
20
+ cart_guest_id?: number | null | undefined;
21
+ category: string | null;
22
+ customer_id?: number | null | undefined;
23
+ description: string | null;
24
+ imageUrl: string | null;
25
+ slug: string;
26
+ allergens: string[];
27
+ tags: string[];
28
+ ingredients: string | null;
29
+ madeFor?: string | null | undefined;
30
+ notes?: string | null | undefined;
31
+ nutritionalInfo: import("@open-tender/types").NutritionalInfo | null;
32
+ cals: number | null;
33
+ groups: import("@open-tender/types").CartItemGroups;
34
+ quantity: number;
35
+ price: number;
36
+ increment: number;
37
+ max: number;
38
+ min: number;
39
+ points: number | null;
40
+ upsellItems: number[];
41
+ similarItems: number[];
42
+ totalPrice: number;
43
+ totalPoints: number | null;
44
+ totalCals: number | null;
45
+ item_no?: number | undefined;
46
+ nutritional_info: import("@open-tender/types").NutritionalInfo | null;
47
+ price_total?: number | undefined;
48
+ favorite_id?: number | undefined;
49
+ images?: import("@open-tender/types").OrderItemImages | undefined;
50
+ short_description?: string | undefined;
51
+ shortDescription?: string | undefined;
52
+ made_for?: string | undefined;
53
+ signature?: string | undefined;
54
+ isDefault?: number | boolean | undefined;
55
+ favorite?: Favorite | undefined;
56
+ };
57
+ export default useOrderItem;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("../../utils");
4
+ const useOrderItem = (orderItem, favorite = null, soldOut = [], allergenAlerts = [], displaySettings = {}, cartCounts = {}, isMobile = true) => {
5
+ var _a, _b, _c, _d;
6
+ const { menuImages: showImage, calories: showCals, tags: showTags, allergens: showAllergens, quickAdd, quickAddMobile, menuItemDescription, menuItemDescriptionMobile, } = displaySettings;
7
+ const showQuickAdd = isMobile ? quickAddMobile : quickAdd;
8
+ const { description, shortDescription, quantity, price, totalPrice, cals, totalCals, tags, allergens, groups, } = orderItem;
9
+ const cartCount = orderItem.id ? cartCounts[orderItem.id] : 0;
10
+ const isSoldOut = orderItem.id ? soldOut.includes(orderItem.id) : false;
11
+ const displayTags = showTags && tags.length ? tags : [];
12
+ const displayAllergens = showAllergens && allergens.length ? allergens : [];
13
+ const contains = allergens.filter((allergen) => allergenAlerts.includes(allergen));
14
+ const allergenAlert = contains.length ? `${contains.join(', ')}` : null;
15
+ const sizeGroup = groups.find((i) => i.isSize);
16
+ const sizeOnly = sizeGroup && groups.length === 1;
17
+ const defaultOption = !sizeGroup
18
+ ? null
19
+ : ((_a = sizeGroup === null || sizeGroup === void 0 ? void 0 : sizeGroup.options) === null || _a === void 0 ? void 0 : _a.find((i) => i.isDefault)) || sizeGroup.options[0];
20
+ const currentPrice = totalPrice
21
+ ? totalPrice
22
+ : defaultOption
23
+ ? defaultOption.price
24
+ : price;
25
+ const displayPrice = (0, utils_1.formatDollars)(`${currentPrice}`);
26
+ const itemCals = showCals ? (totalPrice ? totalCals : cals) : null;
27
+ const displayCals = itemCals ? `${itemCals.toFixed(0)}` : null;
28
+ const groupsBelowMin = groups.filter((g) => { var _a, _b; return ((_a = g.quantity) !== null && _a !== void 0 ? _a : 0) < ((_b = g.min) !== null && _b !== void 0 ? _b : 0); }).length > 0;
29
+ const isIncomplete = totalPrice === 0 || `${quantity}` === '' || groupsBelowMin;
30
+ const nonSizeGroups = groups.filter((i) => !i.isSize);
31
+ const currentOptions = (0, utils_1.getItemOptions)({ groups: nonSizeGroups });
32
+ const hasSelections = ((_b = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.length) !== null && _b !== void 0 ? _b : 0) > 0;
33
+ const showDesc = isMobile ? menuItemDescriptionMobile : menuItemDescription;
34
+ const desc = showDesc === 'HIDE'
35
+ ? ''
36
+ : showDesc === 'SHORT'
37
+ ? shortDescription
38
+ : description;
39
+ const optionNames = favorite
40
+ ? (_d = (_c = favorite === null || favorite === void 0 ? void 0 : favorite.item) === null || _c === void 0 ? void 0 : _c.groups) === null || _d === void 0 ? void 0 : _d.reduce((arr, group) => {
41
+ const names = group.options.map((o) => o.name);
42
+ return [...arr, ...names];
43
+ }, []).join(', ')
44
+ : null;
45
+ const displayDesc = optionNames || desc;
46
+ return Object.assign(Object.assign({}, orderItem), { showImage,
47
+ displayDesc,
48
+ displayTags,
49
+ displayAllergens,
50
+ displayPrice,
51
+ displayCals,
52
+ isIncomplete,
53
+ isSoldOut,
54
+ allergenAlert,
55
+ hasSelections,
56
+ showQuickAdd,
57
+ sizeOnly,
58
+ cartCount });
59
+ };
60
+ exports.default = useOrderItem;
@@ -1 +1,2 @@
1
1
  export * from './utils';
2
+ export * from './hooks';
package/dist/cjs/index.js CHANGED
@@ -2,3 +2,4 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./utils"), exports);
5
+ tslib_1.__exportStar(require("./hooks"), exports);
@@ -1,10 +1,10 @@
1
- import { UnpreparedOrderData, Money, Order, Tender, CheckoutConfig, OrderCart, SoldOut, MenuCategories, Address, MenuItem, CartItem, ServiceType, OrderType, CustomerRevenueCenter, OrderItem, OrderItemImage, Cart, Favorites, Orders, CartItemGroups, Customer, MenuItems, CartItemOption, GuestItem, CartOwner, GuestData } from '@open-tender/types';
1
+ import { UnpreparedOrderData, Money, Order, Tender, CheckoutConfig, OrderCart, SoldOut, MenuCategories, Address, MenuItem, CartItem, ServiceType, OrderType, CustomerRevenueCenter, OrderItem, OrderItemImage, Cart, Favorites, Orders, CartItemGroups, Customer, MenuItems, CartItemGroup, CartItemOption, GuestItem, CartOwner, GuestData } from '@open-tender/types';
2
2
  export declare const displayPrice: (price: string) => string;
3
3
  export declare const makeDisplayPrice: (item: MenuItem) => string | null;
4
4
  export declare const addCommas: (x: number, d: number) => string;
5
5
  export declare const formatQuantity: (n: string) => string;
6
6
  export declare const formatDollars: (str: string, space?: string, decimals?: number) => string;
7
- export declare const getItemOptions: (item: CartItem) => CartItemOption[] | undefined;
7
+ export declare const getItemOptions: (item: Partial<CartItem>) => CartItemOption[] | undefined;
8
8
  export declare const makeModifierNames: (item: CartItem) => string | undefined;
9
9
  export declare const convertStringToArray: (str: string) => string[];
10
10
  export declare const makeOrderTypeFromParam: (param: string) => (string | boolean)[] | null;
@@ -41,6 +41,7 @@ export declare const calcPrices: (item: Partial<CartItem>) => {
41
41
  min: number;
42
42
  isSoldOut: boolean;
43
43
  points: number;
44
+ groups?: Partial<CartItemGroup>[] | undefined;
44
45
  }[] | undefined;
45
46
  id?: number | undefined;
46
47
  name?: string | undefined;
@@ -121,6 +122,7 @@ export declare const makeOrderItem: (item: MenuItem, isEdit?: boolean, soldOut?:
121
122
  min: number;
122
123
  isSoldOut: boolean;
123
124
  points: number;
125
+ groups?: Partial<CartItemGroup>[] | undefined;
124
126
  }[] | undefined;
125
127
  id?: number | undefined;
126
128
  name?: string | undefined;
@@ -363,6 +365,7 @@ export declare const validateCart: (cart: OrderCart, categories: MenuCategories,
363
365
  min: number;
364
366
  isSoldOut: boolean;
365
367
  points: number;
368
+ groups?: Partial<CartItemGroup>[] | undefined;
366
369
  }[] | undefined;
367
370
  id?: number | undefined;
368
371
  name?: string | undefined;
@@ -0,0 +1 @@
1
+ export * from './menu';
@@ -0,0 +1 @@
1
+ export * from './menu';
@@ -0,0 +1,2 @@
1
+ export * from './useBuilder';
2
+ export * from './useOrderItem';
@@ -0,0 +1,2 @@
1
+ export * from './useBuilder';
2
+ export * from './useOrderItem';
@@ -0,0 +1,14 @@
1
+ import { CartItem, CartItemOptions } from '@open-tender/types';
2
+ declare const useBuilder: (orderItem: Partial<CartItem>) => {
3
+ item: Partial<CartItem>;
4
+ increment: () => void;
5
+ decrement: () => void;
6
+ setQuantity: (quantity: number) => void;
7
+ setMadeFor: (madeFor: string) => void;
8
+ setNotes: (notes: string) => void;
9
+ toggleOption: (groupId: number, optionId: number) => void;
10
+ incrementOption: (groupId: number, optionId: number) => void;
11
+ decrementOption: (groupId: number, optionId: number) => void;
12
+ setOptionQuantity: (groupId: number, optionId: number, quantity: number, nested: CartItemOptions) => void;
13
+ };
14
+ export default useBuilder;
@@ -0,0 +1,134 @@
1
+ import { useState } from 'react';
2
+ import { calcPrices } from '../../utils';
3
+ const useBuilder = (orderItem) => {
4
+ const [item, setItem] = useState(orderItem);
5
+ const increment = () => {
6
+ var _a, _b, _c, _d;
7
+ const newQuantity = item.max
8
+ ? Math.min(((_a = item.quantity) !== null && _a !== void 0 ? _a : 0) + ((_b = item.increment) !== null && _b !== void 0 ? _b : 0), item.max)
9
+ : ((_c = item.quantity) !== null && _c !== void 0 ? _c : 0) + ((_d = item.increment) !== null && _d !== void 0 ? _d : 0);
10
+ setItem(calcPrices(Object.assign(Object.assign({}, item), { quantity: newQuantity })));
11
+ };
12
+ const decrement = () => {
13
+ var _a, _b, _c;
14
+ const newQuantity = Math.max(((_a = item.quantity) !== null && _a !== void 0 ? _a : 0) - ((_b = item.increment) !== null && _b !== void 0 ? _b : 0), ((_c = item.min) !== null && _c !== void 0 ? _c : 0));
15
+ setItem(calcPrices(Object.assign(Object.assign({}, item), { quantity: newQuantity })));
16
+ };
17
+ const setQuantity = (quantity) => {
18
+ setItem(calcPrices(Object.assign(Object.assign({}, item), { quantity: quantity })));
19
+ };
20
+ const setMadeFor = (madeFor) => {
21
+ setItem(Object.assign(Object.assign({}, item), { madeFor }));
22
+ };
23
+ const setNotes = (notes) => {
24
+ setItem(Object.assign(Object.assign({}, item), { notes }));
25
+ };
26
+ const toggleOption = (groupId, optionId) => {
27
+ var _a;
28
+ const groups = (_a = item === null || item === void 0 ? void 0 : item.groups) === null || _a === void 0 ? void 0 : _a.map((group) => {
29
+ var _a;
30
+ if (group.id === groupId) {
31
+ const options = (_a = group === null || group === void 0 ? void 0 : group.options) === null || _a === void 0 ? void 0 : _a.map((option) => {
32
+ const newQuantity = option.id === optionId ? 1 : 0;
33
+ return Object.assign(Object.assign({}, option), { quantity: newQuantity });
34
+ });
35
+ return Object.assign(Object.assign({}, group), { options });
36
+ }
37
+ return group;
38
+ });
39
+ setItem(calcPrices(Object.assign(Object.assign({}, item), { groups: groups })));
40
+ };
41
+ const incrementOption = (groupId, optionId) => {
42
+ var _a;
43
+ const groups = (_a = item === null || item === void 0 ? void 0 : item.groups) === null || _a === void 0 ? void 0 : _a.map((group) => {
44
+ var _a, _b, _c;
45
+ if (group.id === groupId) {
46
+ const count = (_a = group === null || group === void 0 ? void 0 : group.options) === null || _a === void 0 ? void 0 : _a.filter((o) => o.id !== optionId).reduce((t, o) => t + o.quantity, 0);
47
+ if (group.max !== 0 && (count !== null && count !== void 0 ? count : 0) >= ((_b = group.max) !== null && _b !== void 0 ? _b : 0))
48
+ return group;
49
+ const options = (_c = group.options) === null || _c === void 0 ? void 0 : _c.map((option) => {
50
+ var _a;
51
+ if (option.id === optionId) {
52
+ let quantity = option.quantity + option.increment;
53
+ const quantities = [quantity];
54
+ if (option.max !== 0)
55
+ quantities.push(option.max);
56
+ if (group.max !== 0)
57
+ quantities.push(((_a = group.max) !== null && _a !== void 0 ? _a : 0) - (count !== null && count !== void 0 ? count : 0));
58
+ quantity = Math.min(...quantities);
59
+ return Object.assign(Object.assign({}, option), { quantity });
60
+ }
61
+ return option;
62
+ });
63
+ return Object.assign(Object.assign({}, group), { options });
64
+ }
65
+ return group;
66
+ });
67
+ setItem(calcPrices(Object.assign(Object.assign({}, item), { groups: groups })));
68
+ };
69
+ const decrementOption = (groupId, optionId) => {
70
+ var _a;
71
+ const groups = (_a = item.groups) === null || _a === void 0 ? void 0 : _a.map((group) => {
72
+ var _a;
73
+ if (group.id === groupId) {
74
+ const options = (_a = group.options) === null || _a === void 0 ? void 0 : _a.map((option) => {
75
+ if (option.id === optionId) {
76
+ const quantity = Math.max(option.quantity - option.increment, 0);
77
+ return Object.assign(Object.assign({}, option), { quantity });
78
+ }
79
+ return option;
80
+ });
81
+ return Object.assign(Object.assign({}, group), { options });
82
+ }
83
+ return group;
84
+ });
85
+ setItem(calcPrices(Object.assign(Object.assign({}, item), { groups: groups })));
86
+ };
87
+ const setOptionQuantity = (groupId, optionId, quantity, nested) => {
88
+ var _a;
89
+ const groups = (_a = item.groups) === null || _a === void 0 ? void 0 : _a.map((group) => {
90
+ var _a, _b, _c;
91
+ if (group.id === groupId) {
92
+ const count = (_a = group.options) === null || _a === void 0 ? void 0 : _a.filter((o) => o.id !== optionId).reduce((t, o) => t + o.quantity, 0);
93
+ if (group.max !== 0 && (count !== null && count !== void 0 ? count : 0) >= ((_b = group.max) !== null && _b !== void 0 ? _b : 0))
94
+ return group;
95
+ const options = (_c = group.options) === null || _c === void 0 ? void 0 : _c.map((option) => {
96
+ var _a;
97
+ const optionGroups = nested || option.groups || [];
98
+ if (option.id === optionId) {
99
+ if (`${quantity}` === '') {
100
+ return Object.assign(Object.assign({}, option), { quantity, groups: optionGroups });
101
+ }
102
+ else {
103
+ const quantities = [quantity];
104
+ if (option.max !== 0)
105
+ quantities.push(option.max);
106
+ if (group.max !== 0)
107
+ quantities.push(((_a = group.max) !== null && _a !== void 0 ? _a : 0) - (count !== null && count !== void 0 ? count : 0));
108
+ quantity = Math.min(...quantities);
109
+ quantity = Math.max(quantity, option.min);
110
+ return Object.assign(Object.assign({}, option), { quantity, groups: optionGroups });
111
+ }
112
+ }
113
+ return option;
114
+ });
115
+ return Object.assign(Object.assign({}, group), { options });
116
+ }
117
+ return group;
118
+ });
119
+ setItem(calcPrices(Object.assign(Object.assign({}, item), { groups: groups })));
120
+ };
121
+ return {
122
+ item,
123
+ increment,
124
+ decrement,
125
+ setQuantity,
126
+ setMadeFor,
127
+ setNotes,
128
+ toggleOption,
129
+ incrementOption,
130
+ decrementOption,
131
+ setOptionQuantity,
132
+ };
133
+ };
134
+ export default useBuilder;
@@ -0,0 +1,57 @@
1
+ import { CartCounts, Favorite, CartItem, SoldOut } from "@open-tender/types";
2
+ declare const useOrderItem: (orderItem: CartItem, favorite?: Favorite | null, soldOut?: SoldOut, allergenAlerts?: Array<string>, displaySettings?: Record<string, string>, cartCounts?: CartCounts, isMobile?: boolean) => {
3
+ showImage: string;
4
+ displayDesc: any;
5
+ displayTags: string[];
6
+ displayAllergens: string[];
7
+ displayPrice: string;
8
+ displayCals: string | null;
9
+ isIncomplete: boolean;
10
+ isSoldOut: boolean;
11
+ allergenAlert: string | null;
12
+ hasSelections: boolean;
13
+ showQuickAdd: string;
14
+ sizeOnly: boolean | undefined;
15
+ cartCount: number;
16
+ index?: number | undefined;
17
+ id?: number | undefined;
18
+ name?: string | undefined;
19
+ shortName?: string | undefined;
20
+ cart_guest_id?: number | null | undefined;
21
+ category: string | null;
22
+ customer_id?: number | null | undefined;
23
+ description: string | null;
24
+ imageUrl: string | null;
25
+ slug: string;
26
+ allergens: string[];
27
+ tags: string[];
28
+ ingredients: string | null;
29
+ madeFor?: string | null | undefined;
30
+ notes?: string | null | undefined;
31
+ nutritionalInfo: import("@open-tender/types").NutritionalInfo | null;
32
+ cals: number | null;
33
+ groups: import("@open-tender/types").CartItemGroups;
34
+ quantity: number;
35
+ price: number;
36
+ increment: number;
37
+ max: number;
38
+ min: number;
39
+ points: number | null;
40
+ upsellItems: number[];
41
+ similarItems: number[];
42
+ totalPrice: number;
43
+ totalPoints: number | null;
44
+ totalCals: number | null;
45
+ item_no?: number | undefined;
46
+ nutritional_info: import("@open-tender/types").NutritionalInfo | null;
47
+ price_total?: number | undefined;
48
+ favorite_id?: number | undefined;
49
+ images?: import("@open-tender/types").OrderItemImages | undefined;
50
+ short_description?: string | undefined;
51
+ shortDescription?: string | undefined;
52
+ made_for?: string | undefined;
53
+ signature?: string | undefined;
54
+ isDefault?: number | boolean | undefined;
55
+ favorite?: Favorite | undefined;
56
+ };
57
+ export default useOrderItem;
@@ -0,0 +1,58 @@
1
+ import { formatDollars, getItemOptions } from "../../utils";
2
+ const useOrderItem = (orderItem, favorite = null, soldOut = [], allergenAlerts = [], displaySettings = {}, cartCounts = {}, isMobile = true) => {
3
+ var _a, _b, _c, _d;
4
+ const { menuImages: showImage, calories: showCals, tags: showTags, allergens: showAllergens, quickAdd, quickAddMobile, menuItemDescription, menuItemDescriptionMobile, } = displaySettings;
5
+ const showQuickAdd = isMobile ? quickAddMobile : quickAdd;
6
+ const { description, shortDescription, quantity, price, totalPrice, cals, totalCals, tags, allergens, groups, } = orderItem;
7
+ const cartCount = orderItem.id ? cartCounts[orderItem.id] : 0;
8
+ const isSoldOut = orderItem.id ? soldOut.includes(orderItem.id) : false;
9
+ const displayTags = showTags && tags.length ? tags : [];
10
+ const displayAllergens = showAllergens && allergens.length ? allergens : [];
11
+ const contains = allergens.filter((allergen) => allergenAlerts.includes(allergen));
12
+ const allergenAlert = contains.length ? `${contains.join(', ')}` : null;
13
+ const sizeGroup = groups.find((i) => i.isSize);
14
+ const sizeOnly = sizeGroup && groups.length === 1;
15
+ const defaultOption = !sizeGroup
16
+ ? null
17
+ : ((_a = sizeGroup === null || sizeGroup === void 0 ? void 0 : sizeGroup.options) === null || _a === void 0 ? void 0 : _a.find((i) => i.isDefault)) || sizeGroup.options[0];
18
+ const currentPrice = totalPrice
19
+ ? totalPrice
20
+ : defaultOption
21
+ ? defaultOption.price
22
+ : price;
23
+ const displayPrice = formatDollars(`${currentPrice}`);
24
+ const itemCals = showCals ? (totalPrice ? totalCals : cals) : null;
25
+ const displayCals = itemCals ? `${itemCals.toFixed(0)}` : null;
26
+ const groupsBelowMin = groups.filter((g) => { var _a, _b; return ((_a = g.quantity) !== null && _a !== void 0 ? _a : 0) < ((_b = g.min) !== null && _b !== void 0 ? _b : 0); }).length > 0;
27
+ const isIncomplete = totalPrice === 0 || `${quantity}` === '' || groupsBelowMin;
28
+ const nonSizeGroups = groups.filter((i) => !i.isSize);
29
+ const currentOptions = getItemOptions({ groups: nonSizeGroups });
30
+ const hasSelections = ((_b = currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.length) !== null && _b !== void 0 ? _b : 0) > 0;
31
+ const showDesc = isMobile ? menuItemDescriptionMobile : menuItemDescription;
32
+ const desc = showDesc === 'HIDE'
33
+ ? ''
34
+ : showDesc === 'SHORT'
35
+ ? shortDescription
36
+ : description;
37
+ const optionNames = favorite
38
+ ? (_d = (_c = favorite === null || favorite === void 0 ? void 0 : favorite.item) === null || _c === void 0 ? void 0 : _c.groups) === null || _d === void 0 ? void 0 : _d.reduce((arr, group) => {
39
+ const names = group.options.map((o) => o.name);
40
+ return [...arr, ...names];
41
+ }, []).join(', ')
42
+ : null;
43
+ const displayDesc = optionNames || desc;
44
+ return Object.assign(Object.assign({}, orderItem), { showImage,
45
+ displayDesc,
46
+ displayTags,
47
+ displayAllergens,
48
+ displayPrice,
49
+ displayCals,
50
+ isIncomplete,
51
+ isSoldOut,
52
+ allergenAlert,
53
+ hasSelections,
54
+ showQuickAdd,
55
+ sizeOnly,
56
+ cartCount });
57
+ };
58
+ export default useOrderItem;
@@ -1 +1,2 @@
1
1
  export * from './utils';
2
+ export * from './hooks';
package/dist/esm/index.js CHANGED
@@ -1 +1,2 @@
1
1
  export * from './utils';
2
+ export * from './hooks';
@@ -1,10 +1,10 @@
1
- import { UnpreparedOrderData, Money, Order, Tender, CheckoutConfig, OrderCart, SoldOut, MenuCategories, Address, MenuItem, CartItem, ServiceType, OrderType, CustomerRevenueCenter, OrderItem, OrderItemImage, Cart, Favorites, Orders, CartItemGroups, Customer, MenuItems, CartItemOption, GuestItem, CartOwner, GuestData } from '@open-tender/types';
1
+ import { UnpreparedOrderData, Money, Order, Tender, CheckoutConfig, OrderCart, SoldOut, MenuCategories, Address, MenuItem, CartItem, ServiceType, OrderType, CustomerRevenueCenter, OrderItem, OrderItemImage, Cart, Favorites, Orders, CartItemGroups, Customer, MenuItems, CartItemGroup, CartItemOption, GuestItem, CartOwner, GuestData } from '@open-tender/types';
2
2
  export declare const displayPrice: (price: string) => string;
3
3
  export declare const makeDisplayPrice: (item: MenuItem) => string | null;
4
4
  export declare const addCommas: (x: number, d: number) => string;
5
5
  export declare const formatQuantity: (n: string) => string;
6
6
  export declare const formatDollars: (str: string, space?: string, decimals?: number) => string;
7
- export declare const getItemOptions: (item: CartItem) => CartItemOption[] | undefined;
7
+ export declare const getItemOptions: (item: Partial<CartItem>) => CartItemOption[] | undefined;
8
8
  export declare const makeModifierNames: (item: CartItem) => string | undefined;
9
9
  export declare const convertStringToArray: (str: string) => string[];
10
10
  export declare const makeOrderTypeFromParam: (param: string) => (string | boolean)[] | null;
@@ -41,6 +41,7 @@ export declare const calcPrices: (item: Partial<CartItem>) => {
41
41
  min: number;
42
42
  isSoldOut: boolean;
43
43
  points: number;
44
+ groups?: Partial<CartItemGroup>[] | undefined;
44
45
  }[] | undefined;
45
46
  id?: number | undefined;
46
47
  name?: string | undefined;
@@ -121,6 +122,7 @@ export declare const makeOrderItem: (item: MenuItem, isEdit?: boolean, soldOut?:
121
122
  min: number;
122
123
  isSoldOut: boolean;
123
124
  points: number;
125
+ groups?: Partial<CartItemGroup>[] | undefined;
124
126
  }[] | undefined;
125
127
  id?: number | undefined;
126
128
  name?: string | undefined;
@@ -363,6 +365,7 @@ export declare const validateCart: (cart: OrderCart, categories: MenuCategories,
363
365
  min: number;
364
366
  isSoldOut: boolean;
365
367
  points: number;
368
+ groups?: Partial<CartItemGroup>[] | undefined;
366
369
  }[] | undefined;
367
370
  id?: number | undefined;
368
371
  name?: string | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
4
  "description": "A library of utils for use with Open Tender applications that utilize our cloud-based Order API.",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -57,7 +57,7 @@
57
57
  "react": "^18.2.0"
58
58
  },
59
59
  "dependencies": {
60
- "@open-tender/types": "^0.0.62",
60
+ "@open-tender/types": "^0.0.65",
61
61
  "date-fns": "^2.29.3",
62
62
  "date-fns-tz": "^1.3.7",
63
63
  "react-native-responsive-screen": "^1.4.2",