woocommerce/plugins/woocommerce-blocks/assets/js/base/hooks/payment-methods/use-active-payment-method.js

33 lines
992 B
JavaScript
Raw Normal View History

/**
* External dependencies
*/
import { useCheckoutContext } from '@woocommerce/base-context/checkout-context';
import { usePaymentMethods } from '@woocommerce/base-hooks';
import { useEffect } from '@wordpress/element';
const useActivePaymentMethod = () => {
const {
activePaymentMethod,
setActivePaymentMethod,
} = useCheckoutContext();
const { paymentMethods, isInitialized } = usePaymentMethods();
// if payment method has not been set yet, let's set it.
useEffect( () => {
// if not initialized yet bail
if ( ! isInitialized ) {
return;
}
if ( ! activePaymentMethod && activePaymentMethod !== null ) {
const paymentMethodIds = Object.keys( paymentMethods );
setActivePaymentMethod(
paymentMethodIds.length > 0
? paymentMethods[ paymentMethodIds[ 0 ] ].name
: null
);
}
}, [ activePaymentMethod, setActivePaymentMethod, isInitialized ] );
return { activePaymentMethod, setActivePaymentMethod };
};
export default useActivePaymentMethod;