UNPKG

@open-tender/utils

Version:

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

53 lines (52 loc) 2 kB
import { useState } from 'react'; import { calcNutritionalInfo } from '../utils'; export const useAccordion = (builtItem, toggleOption, cartId, config) => { var _a; const { hasCals: showCals = false, hasCustomize = false, hasMadeFor: showMadeFor = true, hasNotes: showNotes = true } = config || {}; const [open, setOpen] = 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 ? 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 }; };