@open-tender/utils
Version:
A library of utils for use with Open Tender applications that utilize our cloud-based Order API.
73 lines (72 loc) • 3.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useModifier = void 0;
const utils_1 = require("../utils");
const useModifier = (group, option, levels = [], toggleOption, incrementOption, decrementOption, setOptionQuantity, allergenAlerts, displaySettings) => {
const { calories: showCals = false, tags: showTags = false, allergens: showAllergens = false, modifierImage: showImage = true, modifierDescription: showDescription = true } = displaySettings || {};
const { imageUrl } = option;
const bgStyle = imageUrl ? { backgroundImage: `url(${imageUrl}` } : undefined;
const isRadio = group.min === 1 && group.max === 1;
const isCheckbox = group.options.filter(i => i.max !== 1).length === 0 ||
(group.max === 1 && !isRadio);
const groupAtMax = group.max !== 0 && group.quantity === group.max;
const optionAtMax = option.max !== 0 && option.quantity === option.max;
const oneOnly = group.max === 1 || option.max === 1;
const incrementDisabled = groupAtMax || optionAtMax || option.isSoldOut;
const decrementDisabled = option.quantity === option.min;
const hidePrice = !option.price ||
(group.included !== 0 &&
(group.included === group.max || group.quantity < group.included));
const price = hidePrice ? null : (0, utils_1.formatDollars)(option.price.toString());
const cals = showCals ? option.cals : null;
const allergens = showAllergens ? option.allergens : [];
const tags = showTags ? option.tags : [];
const hasIngredients = option.ingredients && option.ingredients.length > 0 ? true : false;
const hasCals = cals !== null;
const itemAllergens = allergens && allergens.length
? allergens.filter(allergen => allergenAlerts.includes(allergen))
: [];
const allergenAlert = itemAllergens.length > 0;
const optionGroups = option.groups && option.groups.length ? option.groups : null;
const showGroups = option.quantity >= 1 && optionGroups && !group.isPizza ? true : false;
const hasInfo = hasCals || hasIngredients;
const updatedLevels = [...levels, [group.id, option.id]];
const toggle = () => {
toggleOption(updatedLevels);
};
const increment = () => {
incrementOption(updatedLevels);
};
const decrement = () => {
decrementOption(updatedLevels);
};
const adjust = (quantity) => {
setOptionQuantity(updatedLevels, quantity);
};
return Object.assign(Object.assign({}, option), { price,
cals,
tags,
allergens,
optionGroups,
showImage,
showDescription,
hasCals,
hasIngredients,
hasInfo,
allergenAlert,
bgStyle,
isCheckbox,
isRadio,
showGroups,
toggle,
adjust,
increment,
decrement,
incrementDisabled,
decrementDisabled,
updatedLevels,
optionAtMax,
groupAtMax,
oneOnly });
};
exports.useModifier = useModifier;