Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
add checkout context
Browse files Browse the repository at this point in the history
  • Loading branch information
nerrad committed Dec 15, 2019
1 parent b7150de commit b85b11a
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions assets/js/base/context/checkout-context.js
Original file line number Diff line number Diff line change
@@ -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 (
<CheckoutContext.Provider value={ contextValue }>
{ children }
</CheckoutContext.Provider>
);
};

export default CheckoutProvider;

0 comments on commit b85b11a

Please sign in to comment.