mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Add the UnifiedScriptLoader and usePayPalCommerceGateway hook
This commit is contained in:
parent
5e1c2a22d2
commit
07bedc4460
4 changed files with 111 additions and 5 deletions
|
@ -0,0 +1,32 @@
|
|||
import { useState, useEffect } from '@wordpress/element';
|
||||
import { log } from '../../../../ppcp-axo/resources/js/Helper/Debug';
|
||||
|
||||
const usePayPalCommerceGateway = ( initialConfig ) => {
|
||||
const [ isConfigLoaded, setIsConfigLoaded ] = useState( false );
|
||||
const [ ppcpConfig, setPpcpConfig ] = useState( initialConfig );
|
||||
|
||||
useEffect( () => {
|
||||
const loadConfig = () => {
|
||||
if ( typeof window.PayPalCommerceGateway !== 'undefined' ) {
|
||||
setPpcpConfig( window.PayPalCommerceGateway );
|
||||
setIsConfigLoaded( true );
|
||||
} else {
|
||||
log( 'PayPal Commerce Gateway config not loaded.', 'error' );
|
||||
}
|
||||
};
|
||||
|
||||
if ( document.readyState === 'loading' ) {
|
||||
document.addEventListener( 'DOMContentLoaded', loadConfig );
|
||||
} else {
|
||||
loadConfig();
|
||||
}
|
||||
|
||||
return () => {
|
||||
document.removeEventListener( 'DOMContentLoaded', loadConfig );
|
||||
};
|
||||
}, [] );
|
||||
|
||||
return { isConfigLoaded, ppcpConfig };
|
||||
};
|
||||
|
||||
export default usePayPalCommerceGateway;
|
|
@ -27,7 +27,6 @@ const usePayPalScript = ( namespace, ppcpConfig, isConfigLoaded ) => {
|
|||
useEffect( () => {
|
||||
const loadScript = async () => {
|
||||
if ( ! isPayPalLoaded && isConfigLoaded ) {
|
||||
log( `Loading PayPal script for namespace: ${ namespace }` );
|
||||
try {
|
||||
await UnifiedScriptLoader.loadPayPalScript(
|
||||
namespace,
|
||||
|
@ -37,8 +36,8 @@ const usePayPalScript = ( namespace, ppcpConfig, isConfigLoaded ) => {
|
|||
setIsPayPalLoaded( true );
|
||||
} catch ( error ) {
|
||||
log(
|
||||
`Error loading PayPal script for namespace: ${ namespace }`,
|
||||
error
|
||||
`Error loading PayPal script for namespace: ${ namespace }. Error: ${ error }`,
|
||||
'error'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue