Refactor script loading to allow for adding custom namespaces

This commit is contained in:
Daniel Dudzic 2024-07-26 12:39:38 +02:00
parent d769acf144
commit 09ff65aba4
No known key found for this signature in database
GPG key ID: 31B40D33E3465483
6 changed files with 101 additions and 11 deletions

View file

@ -6,24 +6,29 @@ import {
import { loadPaypalScript } from '../../../ppcp-button/resources/js/modules/Helper/ScriptLoading';
import GooglepayManager from './GooglepayManager';
import { loadCustomScript } from '@paypal/paypal-js';
import GooglepayManagerBlockEditor from './GooglepayManagerBlockEditor';
const ppcpData = wc.wcSettings.getSetting( 'ppcp-gateway_data' );
const ppcpConfig = ppcpData.scriptData;
const buttonData = wc.wcSettings.getSetting( 'ppcp-googlepay_data' );
const buttonConfig = buttonData.scriptData;
const dataNamespace = 'ppcpBlocksEditorPaypalGooglepay';
if ( typeof window.PayPalCommerceGateway === 'undefined' ) {
window.PayPalCommerceGateway = ppcpConfig;
}
const GooglePayComponent = () => {
const GooglePayComponent = ( props ) => {
const [ bootstrapped, setBootstrapped ] = useState( false );
const [ paypalLoaded, setPaypalLoaded ] = useState( false );
const [ googlePayLoaded, setGooglePayLoaded ] = useState( false );
const bootstrap = function () {
const manager = new GooglepayManager( buttonConfig, ppcpConfig );
const ManagerClass = props.isEditing
? GooglepayManagerBlockEditor
: GooglepayManager;
const manager = new ManagerClass( buttonConfig, ppcpConfig );
manager.init();
};
@ -33,6 +38,12 @@ const GooglePayComponent = () => {
setGooglePayLoaded( true );
} );
ppcpConfig.url_params.components += ',googlepay';
if ( props.isEditing ) {
ppcpConfig.data_namespace = dataNamespace;
}
// Load PayPal
loadPaypalScript( ppcpConfig, () => {
setPaypalLoaded( true );