@open-tender/utils
Version:
A library of utils for use with Open Tender applications that utilize our cloud-based Order API.
65 lines (64 loc) • 2.95 kB
JavaScript
import { formatDollars, getItemOptions, hasGroupsBelowMin, makeItemSignature } from '../utils';
export const useMenuItem = (builtItem, auth, lookup, cartId, displaySettings) => {
const signature = makeItemSignature(builtItem);
const favoriteId = lookup && signature ? lookup[signature] : null;
const { calories: showCals = false, tags: showTags = false, allergens: showAllergens = false, madeFor: showMadeFor = true, notes: showNotes = true } = displaySettings || {};
const { description, quantity, price, totalPrice, cals, totalCals, totalPoints, tags, allergens, groups, ingredients, totalAllergens, pos_ext_id } = builtItem;
const hasTags = showTags && Array.isArray(tags) && tags.length ? true : false;
const hasAllergens = showAllergens && Array.isArray(allergens) && allergens.length ? true : false;
const hasTagsAllergens = hasTags || hasAllergens ? true : false;
const sizeGroup = groups ? groups.find(i => i.isSize) : null;
const sizeSelected = !sizeGroup || sizeGroup.options.filter(o => o.quantity > 0).length > 0;
const defaultOption = !sizeGroup
? null
: sizeGroup.options.find(i => i.isDefault) || sizeGroup.options[0];
const currentPrice = totalPrice
? totalPrice
: defaultOption
? defaultOption.price
: price;
const displayPrice = formatDollars(currentPrice.toString());
const displayCals = showCals ? (totalPrice ? totalCals : cals) : null;
const missingSize = sizeGroup
? !sizeGroup.options.find(i => i.quantity >= 1)
: false;
const hasGroups = groups ? groups.filter(g => !g.isSize).length > 0 : false;
const groupsBelowMin = hasGroupsBelowMin(null, groups);
const isIncomplete = quantity === 0 || groupsBelowMin;
const requiresCustomization = isIncomplete && !missingSize;
const nonSizeGroups = groups ? groups.filter(i => !i.isSize) : null;
const currentOptions = nonSizeGroups
? getItemOptions(Object.assign(Object.assign({}, builtItem), { groups: nonSizeGroups }))
: [];
const hasSelections = currentOptions.length > 0;
const hasMadeFor = showMadeFor && !cartId ? true : false;
const hasNotes = showNotes ? true : false;
const hasIngredients = ingredients && ingredients.length > 0;
const hasCals = displayCals !== null;
const hasInfo = hasCals || hasIngredients;
return Object.assign(Object.assign({}, builtItem), { description,
tags,
allergens,
displayPrice,
displayCals,
totalPoints,
totalAllergens,
auth,
favoriteId,
isIncomplete,
hasTagsAllergens,
hasTags,
hasAllergens,
hasGroups,
pos_ext_id,
sizeGroup,
sizeSelected,
nonSizeGroups,
requiresCustomization,
hasCals,
hasIngredients,
hasInfo,
hasMadeFor,
hasNotes,
hasSelections });
};