From eba3d12e4e9bed952553fd4f64137944b623b01d Mon Sep 17 00:00:00 2001 From: Daniel Dudzic Date: Mon, 7 Oct 2024 15:42:01 +0200 Subject: [PATCH] Update code comments --- .../resources/js/hooks/usePayPalCommerceGateway.js | 14 ++++++++++++++ .../resources/js/hooks/usePayPalScript.js | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/modules/ppcp-axo-block/resources/js/hooks/usePayPalCommerceGateway.js b/modules/ppcp-axo-block/resources/js/hooks/usePayPalCommerceGateway.js index 23c755d26..5205636e3 100644 --- a/modules/ppcp-axo-block/resources/js/hooks/usePayPalCommerceGateway.js +++ b/modules/ppcp-axo-block/resources/js/hooks/usePayPalCommerceGateway.js @@ -1,11 +1,20 @@ import { useState, useEffect } from '@wordpress/element'; import { log } from '../../../../ppcp-axo/resources/js/Helper/Debug'; +/** + * Custom hook to load and manage the PayPal Commerce Gateway configuration. + * + * @param {Object} initialConfig - Initial configuration object. + * @return {Object} An object containing the loaded config and a boolean indicating if it's loaded. + */ const usePayPalCommerceGateway = ( initialConfig ) => { const [ isConfigLoaded, setIsConfigLoaded ] = useState( false ); const [ ppcpConfig, setPpcpConfig ] = useState( initialConfig ); useEffect( () => { + /** + * Function to load the PayPal Commerce Gateway configuration. + */ const loadConfig = () => { if ( typeof window.PayPalCommerceGateway !== 'undefined' ) { setPpcpConfig( window.PayPalCommerceGateway ); @@ -15,17 +24,22 @@ const usePayPalCommerceGateway = ( initialConfig ) => { } }; + // Check if the DOM is still loading if ( document.readyState === 'loading' ) { + // If it's loading, add an event listener for when the DOM is fully loaded document.addEventListener( 'DOMContentLoaded', loadConfig ); } else { + // If it's already loaded, call the loadConfig function immediately loadConfig(); } + // Cleanup function to remove the event listener return () => { document.removeEventListener( 'DOMContentLoaded', loadConfig ); }; }, [] ); + // Return the loaded configuration and the loading status return { isConfigLoaded, ppcpConfig }; }; diff --git a/modules/ppcp-axo-block/resources/js/hooks/usePayPalScript.js b/modules/ppcp-axo-block/resources/js/hooks/usePayPalScript.js index 1999ecaba..a90321f8e 100644 --- a/modules/ppcp-axo-block/resources/js/hooks/usePayPalScript.js +++ b/modules/ppcp-axo-block/resources/js/hooks/usePayPalScript.js @@ -5,7 +5,7 @@ import UnifiedScriptLoader from '../../../../ppcp-button/resources/js/modules/He import { STORE_NAME } from '../stores/axoStore'; /** - * Custom hook to load the PayPal script using UnifiedScriptLoader. + * Custom hook to load the PayPal script. * * @param {string} namespace - Namespace for the PayPal script. * @param {Object} ppcpConfig - Configuration object for PayPal script.