UNPKG

@open-tender/utils

Version:

A library of utils for use with Open Tender applications that utilize our cloud-based Order API.

57 lines (56 loc) 2.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useAccordion = void 0; const react_1 = require("react"); const utils_1 = require("../utils"); const useAccordion = (builtItem, toggleOption, cartId, config) => { var _a; const { hasCals: showCals = false, hasCustomize = false, hasMadeFor: showMadeFor = true, hasNotes: showNotes = true } = config || {}; const [open, setOpen] = (0, react_1.useState)(null); const { groups, ingredients, totalCals, madeFor, notes } = builtItem; const hasMadeFor = showMadeFor && !cartId ? true : false; const hasNotes = showNotes ? true : false; const hasInstructions = hasMadeFor || hasNotes; const hasCals = showCals && totalCals ? true : false; const nutritionalInfo = hasCals ? (0, utils_1.calcNutritionalInfo)(builtItem) : null; const hasIngredients = ingredients && ingredients.length > 0 ? true : false; const instructionsTitle = hasMadeFor && hasNotes ? 'Name / Special Instructions' : hasMadeFor ? 'Name' : hasNotes ? 'Special Instructions' : ''; const sizeGroup = groups ? groups.find(i => i.isSize) : null; const selectedSize = sizeGroup ? (_a = sizeGroup === null || sizeGroup === void 0 ? void 0 : sizeGroup.options) === null || _a === void 0 ? void 0 : _a.find(i => { var _a; return ((_a = i.quantity) !== null && _a !== void 0 ? _a : 0) >= 1; }) : null; const showAccordion = sizeGroup || hasCustomize || hasInstructions || hasIngredients || hasCals; const toggleSize = (optionId) => { if (sizeGroup) { toggleOption([[sizeGroup.id, optionId]]); setOpen(null); } }; return { open, setOpen, groups, ingredients, totalCals, madeFor, notes, hasMadeFor, hasNotes, hasInstructions, hasCals, nutritionalInfo, hasIngredients, instructionsTitle, sizeGroup, selectedSize, showAccordion, toggleSize }; }; exports.useAccordion = useAccordion;