mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-08 21:52:55 +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) {
|
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', () => {
|
jQuery(document.body).on('ppcp_cart_rendered ppcp_checkout_rendered', () => {
|
||||||
this.render();
|
this.render();
|
||||||
});
|
});
|
||||||
jQuery(document.body).on('ppcp_script_data_changed', (e, data) => {
|
jQuery(document.body).on('ppcp_script_data_changed', (e, data) => {
|
||||||
this.gateway = data;
|
this.gateway = data;
|
||||||
|
|
||||||
this.render();
|
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) => {
|
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) {
|
if (this.lastAmount !== amount) {
|
||||||
this.lastAmount = amount;
|
this.lastAmount = amount;
|
||||||
|
|
||||||
this.render();
|
this.render();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -34,13 +32,39 @@ class MessagesBootstrap {
|
||||||
this.render();
|
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() {
|
discoverBlocks() {
|
||||||
Array.from(document.querySelectorAll('.ppcp-paylater-message-block')).forEach(blockElement => {
|
return new Promise((resolve) => {
|
||||||
const config = {wrapper: '#' + blockElement.id};
|
const elements = document.querySelectorAll('.ppcp-messages');
|
||||||
if (!blockElement.getAttribute('data-pp-placement')) {
|
if (elements.length === 0) {
|
||||||
config.placement = this.gateway.messages.placement;
|
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