mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Messages Bootstrap: Add a render retry functionality
This commit is contained in:
parent
d1b4d7721e
commit
ab718b6045
1 changed files with 33 additions and 9 deletions
|
@ -11,22 +11,20 @@ class MessagesBootstrap {
|
|||
}
|
||||
}
|
||||
|
||||
init() {
|
||||
async init() {
|
||||
if (this.gateway.messages?.block?.enabled) {
|
||||
this.discoverBlocks();
|
||||
await this.attemptDiscoverBlocks(3); // Try up to 3 times
|
||||
}
|
||||
jQuery(document.body).on('ppcp_cart_rendered ppcp_checkout_rendered', () => {
|
||||
this.render();
|
||||
});
|
||||
jQuery(document.body).on('ppcp_script_data_changed', (e, data) => {
|
||||
this.gateway = data;
|
||||
|
||||
this.render();
|
||||
});
|
||||
jQuery(document.body).on('ppcp_cart_total_updated ppcp_checkout_total_updated ppcp_product_total_updated ppcp_block_cart_total_updated', (e, amount) => {
|
||||
if (this.lastAmount !== amount) {
|
||||
this.lastAmount = amount;
|
||||
|
||||
this.render();
|
||||
}
|
||||
});
|
||||
|
@ -34,13 +32,39 @@ class MessagesBootstrap {
|
|||
this.render();
|
||||
}
|
||||
|
||||
attemptDiscoverBlocks(retries) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.discoverBlocks().then(found => {
|
||||
if (!found && retries > 0) {
|
||||
setTimeout(() => {
|
||||
this.attemptDiscoverBlocks(retries - 1).then(resolve);
|
||||
}, 2000); // Wait 2 seconds before retrying
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
discoverBlocks() {
|
||||
Array.from(document.querySelectorAll('.ppcp-paylater-message-block')).forEach(blockElement => {
|
||||
const config = {wrapper: '#' + blockElement.id};
|
||||
if (!blockElement.getAttribute('data-pp-placement')) {
|
||||
config.placement = this.gateway.messages.placement;
|
||||
return new Promise((resolve) => {
|
||||
const elements = document.querySelectorAll('.ppcp-messages');
|
||||
if (elements.length === 0) {
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
this.renderers.push(new MessageRenderer(config));
|
||||
|
||||
Array.from(elements).forEach(blockElement => {
|
||||
if (!blockElement.id) {
|
||||
blockElement.id = `ppcp-message-${Math.random().toString(36).substr(2, 9)}`; // Ensure each block has a unique ID
|
||||
}
|
||||
const config = {wrapper: '#' + blockElement.id};
|
||||
if (!blockElement.getAttribute('data-pp-placement')) {
|
||||
config.placement = this.gateway.messages.placement;
|
||||
}
|
||||
this.renderers.push(new MessageRenderer(config));
|
||||
});
|
||||
resolve(true);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue