From b85b11a5365978a18bb5024be1d4d8d1b6458fd8 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Sun, 15 Dec 2019 17:06:05 -0500 Subject: [PATCH] add checkout context --- assets/js/base/context/checkout-context.js | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 assets/js/base/context/checkout-context.js diff --git a/assets/js/base/context/checkout-context.js b/assets/js/base/context/checkout-context.js new file mode 100644 index 00000000000..b6c060e543c --- /dev/null +++ b/assets/js/base/context/checkout-context.js @@ -0,0 +1,59 @@ +/** + * External dependencies + */ +import { + createContext, + useContext, + useState, + useMemo, +} from '@wordpress/element'; + +const CheckoutContext = createContext( {} ); + +export const useCheckoutContext = () => { + return useContext( CheckoutContext ); +}; + +const CheckoutProvider = ( { children, initialActivePaymentMethod } ) => { + const [ successRedirectUrl, setSuccessRedirectUrl ] = useState( '' ); + const [ failureRedirectUrl, setFailureRedirectUrl ] = useState( '' ); + const [ checkoutComplete, setCheckoutComplete ] = useState( false ); + const [ checkoutHasError, setCheckoutHasError ] = useState( false ); + const [ notices, updateNotices ] = useState( [] ); + const [ isCalculating, setIsCalculating ] = useState( false ); + const [ activePaymentMethod, setActivePaymentMethod ] = useState( + initialActivePaymentMethod + ); + const contextValue = useMemo( () => { + return { + successRedirectUrl, + setSuccessRedirectUrl, + failureRedirectUrl, + setFailureRedirectUrl, + checkoutComplete, + setCheckoutComplete, + checkoutHasError, + setCheckoutHasError, + isCalculating, + setIsCalculating, + notices, + updateNotices, + activePaymentMethod, + setActivePaymentMethod, + }; + }, [ + successRedirectUrl, + failureRedirectUrl, + checkoutComplete, + checkoutHasError, + activePaymentMethod, + notices, + ] ); + return ( + + { children } + + ); +}; + +export default CheckoutProvider;