UNPKG

@commercelayer/react-components

Version:
2 lines 1.43 kB
"use client"; import{jsx as _jsx}from"react/jsx-runtime";import BillingAddressContext from"../../context/BillingAddressContext";import{useContext,useEffect,useReducer}from"react";import billingAddressReducer,{billingAddressInitialState,setBillingAddress,setBillingCustomerAddressId}from"../../reducers/BillingAddressReducer";import CommerceLayerContext from"../../context/CommerceLayerContext";import OrderContext from"../../context/OrderContext";import AddressContext from"../../context/AddressContext";export function BillingAddressContainer(props){const{children}=props,[state,dispatch]=useReducer(billingAddressReducer,billingAddressInitialState),config=useContext(CommerceLayerContext),{order,include,addResourceToInclude}=useContext(OrderContext),{shipToDifferentAddress,setCloneAddress}=useContext(AddressContext);useEffect(()=>(include?.includes("billing_address")||addResourceToInclude({newResource:"billing_address",resourcesIncluded:include}),order&&config&&setBillingCustomerAddressId({dispatch,order,setCloneAddress}),()=>{dispatch({type:"cleanup",payload:{}})}),[order,include]);const contextValue={...state,setBillingAddress:async(id,options)=>{await setBillingAddress(id,{config,dispatch,order,shipToDifferentAddress,customerAddressId:options?.customerAddressId}),setCloneAddress(id,"billing_address")}};return _jsx(BillingAddressContext.Provider,{value:contextValue,children})}export default BillingAddressContainer;