import React , { FunctionComponent , useReducer , useEffect , ReactNode , } from 'react' ; import ItemContext , { initialItemContext , InitItemContext , } from '#context/ItemContext' ; import itemReducer , { itemInitialState , setItemState , Items , ItemQuantity , ItemOptions , CustomLineItems , CustomLineItem , } from '#reducers/ItemReducer' ; import { ItemPrices } from '#reducers/ItemReducer' ; import components from '#config/components' ; import { BFSetStateContainer } from '#typings' ; const propTypes = components . ItemContainer . propTypes ; const displayName = components . ItemContainer . displayName ; type ItemContainerProps = { children : ReactNode ; skuCode ? : string | null ; lineItem ? : { name : string ; imageUrl ? : string | null ; } | null ; } ; const ItemContainer = ( props ) => { const { children , skuCode , lineItem } = props ; const [ state , dispatch ] = useReducer ( itemReducer , itemInitialState ) ; useEffect ( ( ) => { if ( skuCode ) { setItemState ( skuCode , { type : 'setSkuCode' , key : 'skuCode' } , dispatch ) ; } if ( lineItem ) { setItemState ( lineItem as CustomLineItem , { type : 'setCustomLineItem' , key : 'lineItem' } , dispatch ) ; } } , [ ] ) ; const setItems = ( items ) => setItemState ( items , { type : 'setItems' , key : 'items' } , dispatch ) ; const setItem = ( item ) => setItemState ( item , { type : 'setItem' , key : 'item' } , dispatch ) ; const setQuantity = ( item ) => setItemState ( item , { type : 'setQuantity' , key : 'quantity' } , dispatch ) ; const setOption = ( item ) => setItemState ( item , { type : 'setOption' , key : 'option' } , dispatch ) ; const setPrices = ( item ) => setItemState ( item , { type : 'setPrices' , key : 'prices' } , dispatch ) ; const setCustomLineItems = ( item ) => setItemState ( item , { type : 'setCustomLineItems' , key : 'lineItems' } , dispatch ) ; const itemValue = { ... initialItemContext , ... state , setItems , setItem , setQuantity , setOption , setPrices , setCustomLineItems , } as InitItemContext ; return ( < ItemContext . Provider value = { itemValue } > { children } < /ItemContext.Provider>); } ; ItemContainer . propTypes = propTypes ; ItemContainer . displayName = displayName ; export default ItemContainer ;