@wix/headless-restaurants-olo 0.0.34 → 0.0.35
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/cjs/dist/react/ItemDetails.d.ts +4 -4
- package/cjs/dist/react/ItemDetails.js +5 -6
- package/cjs/dist/react/core/ItemDetails.d.ts +1 -1
- package/cjs/dist/react/core/ItemDetails.js +3 -3
- package/cjs/dist/services/item-details-service.d.ts +1 -1
- package/cjs/dist/services/item-details-service.js +10 -16
- package/cjs/dist/services/utils.d.ts +4 -0
- package/cjs/dist/services/utils.js +12 -0
- package/dist/react/ItemDetails.d.ts +4 -4
- package/dist/react/ItemDetails.js +5 -6
- package/dist/react/core/ItemDetails.d.ts +1 -1
- package/dist/react/core/ItemDetails.js +3 -3
- package/dist/services/item-details-service.d.ts +1 -1
- package/dist/services/item-details-service.js +10 -16
- package/dist/services/utils.d.ts +4 -0
- package/dist/services/utils.js +12 -0
- package/package.json +2 -2
|
@@ -131,7 +131,7 @@ export interface AddToCartButtonProps {
|
|
|
131
131
|
addToCartLabelMap: Record<AddToCartButtonState, string>;
|
|
132
132
|
/** Children render prop that receives button state and line item data */
|
|
133
133
|
children?: AsChildChildren<{
|
|
134
|
-
lineItem: LineItem;
|
|
134
|
+
onHandleAddToCart: (onClick: (lineItem: LineItem) => void) => void;
|
|
135
135
|
buttonState: AddToCartButtonState;
|
|
136
136
|
addToCartButtonDisabled?: boolean;
|
|
137
137
|
/** Content to display when loading */
|
|
@@ -139,7 +139,6 @@ export interface AddToCartButtonProps {
|
|
|
139
139
|
/** Text label for the button */
|
|
140
140
|
label: React.ReactNode;
|
|
141
141
|
formattedPrice: string;
|
|
142
|
-
modifierGroupHasError: boolean;
|
|
143
142
|
}>;
|
|
144
143
|
}
|
|
145
144
|
/**
|
|
@@ -166,9 +165,10 @@ export interface AddToCartButtonProps {
|
|
|
166
165
|
* addToCartLabelMap={labelMap}
|
|
167
166
|
* asChild
|
|
168
167
|
* >
|
|
169
|
-
* {({ buttonState, formattedPrice,
|
|
168
|
+
* {({ buttonState, formattedPrice, addToCartButtonDisabled,onHandleAddToCart }) => (
|
|
170
169
|
* <button
|
|
171
|
-
*
|
|
170
|
+
* onClick={() => onHandleAddToCart(onClickFunction)}
|
|
171
|
+
* disabled={addToCartButtonDisabled}
|
|
172
172
|
* className="bg-primary text-primary-foreground"
|
|
173
173
|
* >
|
|
174
174
|
* <span>{labelMap[buttonState]}</span>
|
|
@@ -118,9 +118,10 @@ Variants.displayName = 'ItemDetails.Variants';
|
|
|
118
118
|
* addToCartLabelMap={labelMap}
|
|
119
119
|
* asChild
|
|
120
120
|
* >
|
|
121
|
-
* {({ buttonState, formattedPrice,
|
|
121
|
+
* {({ buttonState, formattedPrice, addToCartButtonDisabled,onHandleAddToCart }) => (
|
|
122
122
|
* <button
|
|
123
|
-
*
|
|
123
|
+
* onClick={() => onHandleAddToCart(onClickFunction)}
|
|
124
|
+
* disabled={addToCartButtonDisabled}
|
|
124
125
|
* className="bg-primary text-primary-foreground"
|
|
125
126
|
* >
|
|
126
127
|
* <span>{labelMap[buttonState]}</span>
|
|
@@ -131,17 +132,15 @@ Variants.displayName = 'ItemDetails.Variants';
|
|
|
131
132
|
* ```
|
|
132
133
|
*/
|
|
133
134
|
export const AddToCartButton = React.forwardRef(({ asChild, children, className, addToCartLabelMap, ...props }, ref) => {
|
|
134
|
-
return (_jsx(CoreItemDetails.LineItemComponent, { addToCartLabelMap: addToCartLabelMap, children: ({
|
|
135
|
+
return (_jsx(CoreItemDetails.LineItemComponent, { addToCartLabelMap: addToCartLabelMap, children: ({ onHandleAddToCart, buttonState, addToCartButtonDisabled, loadingState, labelText, formattedPrice, lineItem, }) => {
|
|
135
136
|
const label = (_jsxs(_Fragment, { children: [_jsx("span", { children: labelText }), " ", _jsx("span", { children: " | " }), _jsx("span", { children: formattedPrice })] }));
|
|
136
137
|
return (_jsx(AsChildSlot, { asChild: asChild, className: className, customElement: children, customElementProps: {
|
|
137
138
|
buttonState,
|
|
138
139
|
addToCartButtonDisabled,
|
|
139
140
|
loadingState,
|
|
140
|
-
|
|
141
|
-
lineItems: [lineItem],
|
|
141
|
+
onHandleAddToCart,
|
|
142
142
|
label,
|
|
143
143
|
formattedPrice,
|
|
144
|
-
modifierGroupHasError,
|
|
145
144
|
}, ref: ref, ...props, children: _jsx(Commerce.Actions.AddToCart, { asChild: false, label: label, className: className, lineItems: [lineItem], ...props }) }));
|
|
146
145
|
} }));
|
|
147
146
|
});
|
|
@@ -21,12 +21,12 @@ interface ItemDetailsLineItemProps {
|
|
|
21
21
|
addToCartLabelMap: Record<AddToCartButtonState, string>;
|
|
22
22
|
children: (props: {
|
|
23
23
|
loadingState: string | React.ReactNode;
|
|
24
|
+
onHandleAddToCart: (onClick: (lineItem: LineItem) => void) => void;
|
|
24
25
|
lineItem: LineItem;
|
|
25
26
|
buttonState: AddToCartButtonState;
|
|
26
27
|
addToCartButtonDisabled: boolean;
|
|
27
28
|
labelText: string;
|
|
28
29
|
formattedPrice: string;
|
|
29
|
-
modifierGroupHasError: boolean;
|
|
30
30
|
}) => React.ReactNode;
|
|
31
31
|
}
|
|
32
32
|
export declare const LineItemComponent: React.FC<ItemDetailsLineItemProps>;
|
|
@@ -31,6 +31,7 @@ export const LineItemComponent = ({ addToCartLabelMap, children, }) => {
|
|
|
31
31
|
const service = useService(ItemServiceDefinition);
|
|
32
32
|
const oloSettingsService = useService(OLOSettingsServiceDefinition);
|
|
33
33
|
const lineItem = service.lineItem?.get?.() ?? {};
|
|
34
|
+
const onHandleAddToCart = service.onHandleAddToCart;
|
|
34
35
|
const loadingState = service.isLoading?.get?.() ?? false;
|
|
35
36
|
const buttonState = service.buttonState?.get?.() ?? AddToCartButtonState.VALID_TO_CONTINUE;
|
|
36
37
|
const addToCartButtonDisabled = service.addToCartButtonDisabled?.get?.() ?? false;
|
|
@@ -38,15 +39,14 @@ export const LineItemComponent = ({ addToCartLabelMap, children, }) => {
|
|
|
38
39
|
const formatCurrency = oloSettingsService.formatCurrency;
|
|
39
40
|
const formattedPrice = formatCurrency(price);
|
|
40
41
|
const labelText = addToCartLabelMap[buttonState];
|
|
41
|
-
const modifierGroupHasError = service.doesModifierGroupHaveError?.get?.() ?? false;
|
|
42
42
|
return children({
|
|
43
43
|
loadingState,
|
|
44
|
-
|
|
44
|
+
onHandleAddToCart,
|
|
45
45
|
buttonState,
|
|
46
46
|
addToCartButtonDisabled,
|
|
47
47
|
labelText,
|
|
48
48
|
formattedPrice,
|
|
49
|
-
|
|
49
|
+
lineItem,
|
|
50
50
|
});
|
|
51
51
|
};
|
|
52
52
|
export const QuantityComponent = ({ children, }) => {
|
|
@@ -16,6 +16,7 @@ export interface ItemServiceAPI {
|
|
|
16
16
|
quantity: Signal<number>;
|
|
17
17
|
specialRequest: Signal<string>;
|
|
18
18
|
lineItem: ReadOnlySignal<LineItem>;
|
|
19
|
+
onHandleAddToCart: (onClick: (lineItem: LineItem) => void) => void;
|
|
19
20
|
buttonState: ReadOnlySignal<AddToCartButtonState | undefined>;
|
|
20
21
|
addToCartButtonDisabled: Signal<boolean>;
|
|
21
22
|
price: ReadOnlySignal<number>;
|
|
@@ -43,7 +44,6 @@ export interface ItemServiceAPI {
|
|
|
43
44
|
endDate?: Date;
|
|
44
45
|
weeklyAvailabilitySummary?: WeeklyAvailability;
|
|
45
46
|
};
|
|
46
|
-
doesModifierGroupHaveError: ReadOnlySignal<boolean>;
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
49
|
* Service definition for the Item service.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { defineService, implementService } from '@wix/services-definitions';
|
|
2
2
|
import { SignalsServiceDefinition, } from '@wix/services-definitions/core-services/signals';
|
|
3
3
|
import { AddToCartButtonState, AvailabilityStatus, } from './common-types.js';
|
|
4
|
-
import { getAreNotEnoughModifiersOfMandatoryModifierGroupInStock, getModifiersInitState, getLineItemModifiers, getPriceVariantOptions, getSelectedModifierPrices, getSelectedVariantPrice, calculateItemPrice,
|
|
4
|
+
import { getAreNotEnoughModifiersOfMandatoryModifierGroupInStock, getModifiersInitState, getLineItemModifiers, getPriceVariantOptions, getSelectedModifierPrices, getSelectedVariantPrice, calculateItemPrice, getModifierGroupErrors, } from './utils.js';
|
|
5
5
|
import { OLOSettingsServiceDefinition } from './olo-settings-service.js';
|
|
6
6
|
/**
|
|
7
7
|
* Service definition for the Item service.
|
|
@@ -131,6 +131,14 @@ export const ItemService = implementService.withConfig()(ItemServiceDefinition,
|
|
|
131
131
|
},
|
|
132
132
|
};
|
|
133
133
|
});
|
|
134
|
+
const onHandleAddToCart = (onClick) => {
|
|
135
|
+
const currentSelectedModifiers = selectedModifiers.get();
|
|
136
|
+
const { errors, hasError } = getModifierGroupErrors(currentSelectedModifiers, modifierGroups);
|
|
137
|
+
modifierGroupError.set(errors);
|
|
138
|
+
if (hasError)
|
|
139
|
+
return;
|
|
140
|
+
onClick(lineItem.get());
|
|
141
|
+
};
|
|
134
142
|
const updateQuantity = (_quantity) => {
|
|
135
143
|
quantity.set(_quantity);
|
|
136
144
|
};
|
|
@@ -164,20 +172,6 @@ export const ItemService = implementService.withConfig()(ItemServiceDefinition,
|
|
|
164
172
|
}
|
|
165
173
|
}
|
|
166
174
|
};
|
|
167
|
-
const doesModifierGroupHaveError = signalsService.computed(() => {
|
|
168
|
-
const currentSelectedModifiers = selectedModifiers.get();
|
|
169
|
-
const errors = {};
|
|
170
|
-
modifierGroups.forEach((group) => {
|
|
171
|
-
if (group._id) {
|
|
172
|
-
const selectedCount = (currentSelectedModifiers[group._id] || [])
|
|
173
|
-
.length;
|
|
174
|
-
const isValid = checkModifiersValidation(group.rule, selectedCount);
|
|
175
|
-
errors[group._id] = !isValid;
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
modifierGroupError.set(errors);
|
|
179
|
-
return Object.values(errors).some((error) => error);
|
|
180
|
-
});
|
|
181
175
|
const toggleModifier = (modifierGroupId, modifierId, singleSelect = false) => {
|
|
182
176
|
const currentSelectedModifiers = selectedModifiers.get();
|
|
183
177
|
const modifierIds = getModifierIds(modifierGroupId, modifierId, singleSelect);
|
|
@@ -206,6 +200,7 @@ export const ItemService = implementService.withConfig()(ItemServiceDefinition,
|
|
|
206
200
|
error,
|
|
207
201
|
specialRequest,
|
|
208
202
|
lineItem,
|
|
203
|
+
onHandleAddToCart,
|
|
209
204
|
selectedVariant,
|
|
210
205
|
selectedModifiers,
|
|
211
206
|
availabilityStatus,
|
|
@@ -215,7 +210,6 @@ export const ItemService = implementService.withConfig()(ItemServiceDefinition,
|
|
|
215
210
|
addToCartButtonDisabled,
|
|
216
211
|
price,
|
|
217
212
|
futureAvailability,
|
|
218
|
-
doesModifierGroupHaveError,
|
|
219
213
|
};
|
|
220
214
|
});
|
|
221
215
|
/**
|
|
@@ -35,6 +35,10 @@ export declare const chooseUpToX: ({ required, minSelections, maxSelections, }:
|
|
|
35
35
|
export declare const hasToChooseBetweenXAndY: ({ required, minSelections, maxSelections, }: ruleUtilsArgs) => boolean;
|
|
36
36
|
export declare const getRuleTypeMapValue: (ruleTypeMap: RuleTypeMap, ruleType: RuleType, rule: EnhancedModifierGroup["rule"]) => string | undefined;
|
|
37
37
|
export declare const checkModifiersValidation: (rule: EnhancedModifierGroup["rule"], selectedCount: number) => boolean;
|
|
38
|
+
export declare const getModifierGroupErrors: (selectedModifiers: Record<string, Array<string>>, modifierGroups: EnhancedModifierGroup[]) => {
|
|
39
|
+
errors: Record<string, boolean>;
|
|
40
|
+
hasError: boolean;
|
|
41
|
+
};
|
|
38
42
|
export declare const getAreNotEnoughModifiersOfMandatoryModifierGroupInStock: (modifierGroups: EnhancedModifierGroup[]) => boolean;
|
|
39
43
|
export declare const getLineItemModifiers: (selectedModifiers: Record<string, Array<string>>, modifierGroups: EnhancedModifierGroup[], formatCurrency: (price?: number) => string) => {
|
|
40
44
|
id: string;
|
|
@@ -158,6 +158,18 @@ export const checkModifiersValidation = (rule, selectedCount) => {
|
|
|
158
158
|
}
|
|
159
159
|
return true;
|
|
160
160
|
};
|
|
161
|
+
export const getModifierGroupErrors = (selectedModifiers, modifierGroups) => {
|
|
162
|
+
const errors = {};
|
|
163
|
+
modifierGroups.forEach((group) => {
|
|
164
|
+
if (group._id) {
|
|
165
|
+
const selectedCount = (selectedModifiers[group._id] || []).length;
|
|
166
|
+
const isValid = checkModifiersValidation(group.rule, selectedCount);
|
|
167
|
+
errors[group._id] = !isValid;
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
const hasError = Object.values(errors).some((error) => error);
|
|
171
|
+
return { errors, hasError };
|
|
172
|
+
};
|
|
161
173
|
export const getAreNotEnoughModifiersOfMandatoryModifierGroupInStock = (modifierGroups) => modifierGroups.some((modifierGroup) => {
|
|
162
174
|
const isModifierGroupMandatory = modifierGroup.rule?.required;
|
|
163
175
|
const minimumRequiredModifiers = modifierGroup.rule?.minSelections ?? 0;
|
|
@@ -131,7 +131,7 @@ export interface AddToCartButtonProps {
|
|
|
131
131
|
addToCartLabelMap: Record<AddToCartButtonState, string>;
|
|
132
132
|
/** Children render prop that receives button state and line item data */
|
|
133
133
|
children?: AsChildChildren<{
|
|
134
|
-
lineItem: LineItem;
|
|
134
|
+
onHandleAddToCart: (onClick: (lineItem: LineItem) => void) => void;
|
|
135
135
|
buttonState: AddToCartButtonState;
|
|
136
136
|
addToCartButtonDisabled?: boolean;
|
|
137
137
|
/** Content to display when loading */
|
|
@@ -139,7 +139,6 @@ export interface AddToCartButtonProps {
|
|
|
139
139
|
/** Text label for the button */
|
|
140
140
|
label: React.ReactNode;
|
|
141
141
|
formattedPrice: string;
|
|
142
|
-
modifierGroupHasError: boolean;
|
|
143
142
|
}>;
|
|
144
143
|
}
|
|
145
144
|
/**
|
|
@@ -166,9 +165,10 @@ export interface AddToCartButtonProps {
|
|
|
166
165
|
* addToCartLabelMap={labelMap}
|
|
167
166
|
* asChild
|
|
168
167
|
* >
|
|
169
|
-
* {({ buttonState, formattedPrice,
|
|
168
|
+
* {({ buttonState, formattedPrice, addToCartButtonDisabled,onHandleAddToCart }) => (
|
|
170
169
|
* <button
|
|
171
|
-
*
|
|
170
|
+
* onClick={() => onHandleAddToCart(onClickFunction)}
|
|
171
|
+
* disabled={addToCartButtonDisabled}
|
|
172
172
|
* className="bg-primary text-primary-foreground"
|
|
173
173
|
* >
|
|
174
174
|
* <span>{labelMap[buttonState]}</span>
|
|
@@ -118,9 +118,10 @@ Variants.displayName = 'ItemDetails.Variants';
|
|
|
118
118
|
* addToCartLabelMap={labelMap}
|
|
119
119
|
* asChild
|
|
120
120
|
* >
|
|
121
|
-
* {({ buttonState, formattedPrice,
|
|
121
|
+
* {({ buttonState, formattedPrice, addToCartButtonDisabled,onHandleAddToCart }) => (
|
|
122
122
|
* <button
|
|
123
|
-
*
|
|
123
|
+
* onClick={() => onHandleAddToCart(onClickFunction)}
|
|
124
|
+
* disabled={addToCartButtonDisabled}
|
|
124
125
|
* className="bg-primary text-primary-foreground"
|
|
125
126
|
* >
|
|
126
127
|
* <span>{labelMap[buttonState]}</span>
|
|
@@ -131,17 +132,15 @@ Variants.displayName = 'ItemDetails.Variants';
|
|
|
131
132
|
* ```
|
|
132
133
|
*/
|
|
133
134
|
export const AddToCartButton = React.forwardRef(({ asChild, children, className, addToCartLabelMap, ...props }, ref) => {
|
|
134
|
-
return (_jsx(CoreItemDetails.LineItemComponent, { addToCartLabelMap: addToCartLabelMap, children: ({
|
|
135
|
+
return (_jsx(CoreItemDetails.LineItemComponent, { addToCartLabelMap: addToCartLabelMap, children: ({ onHandleAddToCart, buttonState, addToCartButtonDisabled, loadingState, labelText, formattedPrice, lineItem, }) => {
|
|
135
136
|
const label = (_jsxs(_Fragment, { children: [_jsx("span", { children: labelText }), " ", _jsx("span", { children: " | " }), _jsx("span", { children: formattedPrice })] }));
|
|
136
137
|
return (_jsx(AsChildSlot, { asChild: asChild, className: className, customElement: children, customElementProps: {
|
|
137
138
|
buttonState,
|
|
138
139
|
addToCartButtonDisabled,
|
|
139
140
|
loadingState,
|
|
140
|
-
|
|
141
|
-
lineItems: [lineItem],
|
|
141
|
+
onHandleAddToCart,
|
|
142
142
|
label,
|
|
143
143
|
formattedPrice,
|
|
144
|
-
modifierGroupHasError,
|
|
145
144
|
}, ref: ref, ...props, children: _jsx(Commerce.Actions.AddToCart, { asChild: false, label: label, className: className, lineItems: [lineItem], ...props }) }));
|
|
146
145
|
} }));
|
|
147
146
|
});
|
|
@@ -21,12 +21,12 @@ interface ItemDetailsLineItemProps {
|
|
|
21
21
|
addToCartLabelMap: Record<AddToCartButtonState, string>;
|
|
22
22
|
children: (props: {
|
|
23
23
|
loadingState: string | React.ReactNode;
|
|
24
|
+
onHandleAddToCart: (onClick: (lineItem: LineItem) => void) => void;
|
|
24
25
|
lineItem: LineItem;
|
|
25
26
|
buttonState: AddToCartButtonState;
|
|
26
27
|
addToCartButtonDisabled: boolean;
|
|
27
28
|
labelText: string;
|
|
28
29
|
formattedPrice: string;
|
|
29
|
-
modifierGroupHasError: boolean;
|
|
30
30
|
}) => React.ReactNode;
|
|
31
31
|
}
|
|
32
32
|
export declare const LineItemComponent: React.FC<ItemDetailsLineItemProps>;
|
|
@@ -31,6 +31,7 @@ export const LineItemComponent = ({ addToCartLabelMap, children, }) => {
|
|
|
31
31
|
const service = useService(ItemServiceDefinition);
|
|
32
32
|
const oloSettingsService = useService(OLOSettingsServiceDefinition);
|
|
33
33
|
const lineItem = service.lineItem?.get?.() ?? {};
|
|
34
|
+
const onHandleAddToCart = service.onHandleAddToCart;
|
|
34
35
|
const loadingState = service.isLoading?.get?.() ?? false;
|
|
35
36
|
const buttonState = service.buttonState?.get?.() ?? AddToCartButtonState.VALID_TO_CONTINUE;
|
|
36
37
|
const addToCartButtonDisabled = service.addToCartButtonDisabled?.get?.() ?? false;
|
|
@@ -38,15 +39,14 @@ export const LineItemComponent = ({ addToCartLabelMap, children, }) => {
|
|
|
38
39
|
const formatCurrency = oloSettingsService.formatCurrency;
|
|
39
40
|
const formattedPrice = formatCurrency(price);
|
|
40
41
|
const labelText = addToCartLabelMap[buttonState];
|
|
41
|
-
const modifierGroupHasError = service.doesModifierGroupHaveError?.get?.() ?? false;
|
|
42
42
|
return children({
|
|
43
43
|
loadingState,
|
|
44
|
-
|
|
44
|
+
onHandleAddToCart,
|
|
45
45
|
buttonState,
|
|
46
46
|
addToCartButtonDisabled,
|
|
47
47
|
labelText,
|
|
48
48
|
formattedPrice,
|
|
49
|
-
|
|
49
|
+
lineItem,
|
|
50
50
|
});
|
|
51
51
|
};
|
|
52
52
|
export const QuantityComponent = ({ children, }) => {
|
|
@@ -16,6 +16,7 @@ export interface ItemServiceAPI {
|
|
|
16
16
|
quantity: Signal<number>;
|
|
17
17
|
specialRequest: Signal<string>;
|
|
18
18
|
lineItem: ReadOnlySignal<LineItem>;
|
|
19
|
+
onHandleAddToCart: (onClick: (lineItem: LineItem) => void) => void;
|
|
19
20
|
buttonState: ReadOnlySignal<AddToCartButtonState | undefined>;
|
|
20
21
|
addToCartButtonDisabled: Signal<boolean>;
|
|
21
22
|
price: ReadOnlySignal<number>;
|
|
@@ -43,7 +44,6 @@ export interface ItemServiceAPI {
|
|
|
43
44
|
endDate?: Date;
|
|
44
45
|
weeklyAvailabilitySummary?: WeeklyAvailability;
|
|
45
46
|
};
|
|
46
|
-
doesModifierGroupHaveError: ReadOnlySignal<boolean>;
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
49
|
* Service definition for the Item service.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { defineService, implementService } from '@wix/services-definitions';
|
|
2
2
|
import { SignalsServiceDefinition, } from '@wix/services-definitions/core-services/signals';
|
|
3
3
|
import { AddToCartButtonState, AvailabilityStatus, } from './common-types.js';
|
|
4
|
-
import { getAreNotEnoughModifiersOfMandatoryModifierGroupInStock, getModifiersInitState, getLineItemModifiers, getPriceVariantOptions, getSelectedModifierPrices, getSelectedVariantPrice, calculateItemPrice,
|
|
4
|
+
import { getAreNotEnoughModifiersOfMandatoryModifierGroupInStock, getModifiersInitState, getLineItemModifiers, getPriceVariantOptions, getSelectedModifierPrices, getSelectedVariantPrice, calculateItemPrice, getModifierGroupErrors, } from './utils.js';
|
|
5
5
|
import { OLOSettingsServiceDefinition } from './olo-settings-service.js';
|
|
6
6
|
/**
|
|
7
7
|
* Service definition for the Item service.
|
|
@@ -131,6 +131,14 @@ export const ItemService = implementService.withConfig()(ItemServiceDefinition,
|
|
|
131
131
|
},
|
|
132
132
|
};
|
|
133
133
|
});
|
|
134
|
+
const onHandleAddToCart = (onClick) => {
|
|
135
|
+
const currentSelectedModifiers = selectedModifiers.get();
|
|
136
|
+
const { errors, hasError } = getModifierGroupErrors(currentSelectedModifiers, modifierGroups);
|
|
137
|
+
modifierGroupError.set(errors);
|
|
138
|
+
if (hasError)
|
|
139
|
+
return;
|
|
140
|
+
onClick(lineItem.get());
|
|
141
|
+
};
|
|
134
142
|
const updateQuantity = (_quantity) => {
|
|
135
143
|
quantity.set(_quantity);
|
|
136
144
|
};
|
|
@@ -164,20 +172,6 @@ export const ItemService = implementService.withConfig()(ItemServiceDefinition,
|
|
|
164
172
|
}
|
|
165
173
|
}
|
|
166
174
|
};
|
|
167
|
-
const doesModifierGroupHaveError = signalsService.computed(() => {
|
|
168
|
-
const currentSelectedModifiers = selectedModifiers.get();
|
|
169
|
-
const errors = {};
|
|
170
|
-
modifierGroups.forEach((group) => {
|
|
171
|
-
if (group._id) {
|
|
172
|
-
const selectedCount = (currentSelectedModifiers[group._id] || [])
|
|
173
|
-
.length;
|
|
174
|
-
const isValid = checkModifiersValidation(group.rule, selectedCount);
|
|
175
|
-
errors[group._id] = !isValid;
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
modifierGroupError.set(errors);
|
|
179
|
-
return Object.values(errors).some((error) => error);
|
|
180
|
-
});
|
|
181
175
|
const toggleModifier = (modifierGroupId, modifierId, singleSelect = false) => {
|
|
182
176
|
const currentSelectedModifiers = selectedModifiers.get();
|
|
183
177
|
const modifierIds = getModifierIds(modifierGroupId, modifierId, singleSelect);
|
|
@@ -206,6 +200,7 @@ export const ItemService = implementService.withConfig()(ItemServiceDefinition,
|
|
|
206
200
|
error,
|
|
207
201
|
specialRequest,
|
|
208
202
|
lineItem,
|
|
203
|
+
onHandleAddToCart,
|
|
209
204
|
selectedVariant,
|
|
210
205
|
selectedModifiers,
|
|
211
206
|
availabilityStatus,
|
|
@@ -215,7 +210,6 @@ export const ItemService = implementService.withConfig()(ItemServiceDefinition,
|
|
|
215
210
|
addToCartButtonDisabled,
|
|
216
211
|
price,
|
|
217
212
|
futureAvailability,
|
|
218
|
-
doesModifierGroupHaveError,
|
|
219
213
|
};
|
|
220
214
|
});
|
|
221
215
|
/**
|
package/dist/services/utils.d.ts
CHANGED
|
@@ -35,6 +35,10 @@ export declare const chooseUpToX: ({ required, minSelections, maxSelections, }:
|
|
|
35
35
|
export declare const hasToChooseBetweenXAndY: ({ required, minSelections, maxSelections, }: ruleUtilsArgs) => boolean;
|
|
36
36
|
export declare const getRuleTypeMapValue: (ruleTypeMap: RuleTypeMap, ruleType: RuleType, rule: EnhancedModifierGroup["rule"]) => string | undefined;
|
|
37
37
|
export declare const checkModifiersValidation: (rule: EnhancedModifierGroup["rule"], selectedCount: number) => boolean;
|
|
38
|
+
export declare const getModifierGroupErrors: (selectedModifiers: Record<string, Array<string>>, modifierGroups: EnhancedModifierGroup[]) => {
|
|
39
|
+
errors: Record<string, boolean>;
|
|
40
|
+
hasError: boolean;
|
|
41
|
+
};
|
|
38
42
|
export declare const getAreNotEnoughModifiersOfMandatoryModifierGroupInStock: (modifierGroups: EnhancedModifierGroup[]) => boolean;
|
|
39
43
|
export declare const getLineItemModifiers: (selectedModifiers: Record<string, Array<string>>, modifierGroups: EnhancedModifierGroup[], formatCurrency: (price?: number) => string) => {
|
|
40
44
|
id: string;
|
package/dist/services/utils.js
CHANGED
|
@@ -158,6 +158,18 @@ export const checkModifiersValidation = (rule, selectedCount) => {
|
|
|
158
158
|
}
|
|
159
159
|
return true;
|
|
160
160
|
};
|
|
161
|
+
export const getModifierGroupErrors = (selectedModifiers, modifierGroups) => {
|
|
162
|
+
const errors = {};
|
|
163
|
+
modifierGroups.forEach((group) => {
|
|
164
|
+
if (group._id) {
|
|
165
|
+
const selectedCount = (selectedModifiers[group._id] || []).length;
|
|
166
|
+
const isValid = checkModifiersValidation(group.rule, selectedCount);
|
|
167
|
+
errors[group._id] = !isValid;
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
const hasError = Object.values(errors).some((error) => error);
|
|
171
|
+
return { errors, hasError };
|
|
172
|
+
};
|
|
161
173
|
export const getAreNotEnoughModifiersOfMandatoryModifierGroupInStock = (modifierGroups) => modifierGroups.some((modifierGroup) => {
|
|
162
174
|
const isModifierGroupMandatory = modifierGroup.rule?.required;
|
|
163
175
|
const minimumRequiredModifiers = modifierGroup.rule?.minSelections ?? 0;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wix/headless-restaurants-olo",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.35",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -77,5 +77,5 @@
|
|
|
77
77
|
"groupId": "com.wixpress.headless-components"
|
|
78
78
|
}
|
|
79
79
|
},
|
|
80
|
-
"falconPackageHash": "
|
|
80
|
+
"falconPackageHash": "8fef913fc66b2bade48d7c257f0f51bb8b909775971887e2af22001c"
|
|
81
81
|
}
|