Apply new namespaced script loading

This commit is contained in:
Philipp Stracker 2024-10-08 14:25:43 +02:00
parent ac98924f52
commit 0d72641ded
No known key found for this signature in database
4 changed files with 79 additions and 51 deletions

View file

@ -1,32 +1,43 @@
/* global paypal */
import ApplePayButton from './ApplepayButton';
import ContextHandlerFactory from './Context/ContextHandlerFactory';
class ApplePayManagerBlockEditor {
constructor( buttonConfig, ppcpConfig ) {
constructor( namespace, buttonConfig, ppcpConfig ) {
this.namespace = namespace;
this.buttonConfig = buttonConfig;
this.ppcpConfig = ppcpConfig;
this.applePayConfig = null;
/*
* On the front-end, the init method is called when a new button context was detected
* via `buttonModuleWatcher`. In the block editor, we do not need to wait for the
* context, but can initialize the button in the next event loop.
*/
setTimeout( () => this.init() );
}
init() {
( async () => {
await this.config();
} )();
}
async config() {
async init() {
try {
this.applePayConfig = await ppcpBlocksEditorPaypalApplepay.Applepay().config();
this.applePayConfig = await window[ this.namespace ]
.Applepay()
.config();
const button = new ApplePayButton(
this.contextHandler = ContextHandlerFactory.create(
this.ppcpConfig.context,
this.buttonConfig,
this.ppcpConfig,
null
);
const button = ApplePayButton.createButton(
this.ppcpConfig.context,
null,
this.buttonConfig,
this.ppcpConfig
this.ppcpConfig,
this.contextHandler
);
button.init( this.applePayConfig );
button.configure( this.applePayConfig );
button.init();
} catch ( error ) {
console.error( 'Failed to initialize Apple Pay:', error );
}