mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Add hide / show conditions on SingleProduct Buttons for when they shouldn't be rendered.
Refactor MessageRenderer not to reload when it has no changes.
This commit is contained in:
parent
587e065fba
commit
75bf98c174
2 changed files with 44 additions and 15 deletions
|
@ -1,5 +1,6 @@
|
||||||
import UpdateCart from "../Helper/UpdateCart";
|
import UpdateCart from "../Helper/UpdateCart";
|
||||||
import SingleProductActionHandler from "../ActionHandler/SingleProductActionHandler";
|
import SingleProductActionHandler from "../ActionHandler/SingleProductActionHandler";
|
||||||
|
import {hide, show} from "../Helper/Hiding";
|
||||||
import {disable, enable} from "../Helper/ButtonDisabler";
|
import {disable, enable} from "../Helper/ButtonDisabler";
|
||||||
|
|
||||||
class SingleProductBootstap {
|
class SingleProductBootstap {
|
||||||
|
@ -18,10 +19,16 @@ class SingleProductBootstap {
|
||||||
|
|
||||||
handleChange() {
|
handleChange() {
|
||||||
if (!this.shouldRender()) {
|
if (!this.shouldRender()) {
|
||||||
|
hide(this.gateway.button.wrapper, this.formSelector);
|
||||||
|
hide(this.gateway.messages.wrapper);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.render();
|
this.render();
|
||||||
|
|
||||||
|
show(this.gateway.button.wrapper, this.formSelector);
|
||||||
|
show(this.gateway.messages.wrapper);
|
||||||
|
|
||||||
this.handleButtonStatus();
|
this.handleButtonStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ class MessageRenderer {
|
||||||
|
|
||||||
constructor(config) {
|
constructor(config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
|
this.optionsFingerprint = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
@ -9,18 +10,20 @@ class MessageRenderer {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
paypal.Messages({
|
const options = {
|
||||||
amount: this.config.amount,
|
amount: this.config.amount,
|
||||||
placement: this.config.placement,
|
placement: this.config.placement,
|
||||||
style: this.config.style
|
style: this.config.style
|
||||||
}).render(this.config.wrapper);
|
};
|
||||||
|
|
||||||
|
if (this.isOptionsFingerprintEqual(options)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
paypal.Messages(options).render(this.config.wrapper);
|
||||||
|
|
||||||
jQuery(document.body).on('updated_cart_totals', () => {
|
jQuery(document.body).on('updated_cart_totals', () => {
|
||||||
paypal.Messages({
|
paypal.Messages(options).render(this.config.wrapper);
|
||||||
amount: this.config.amount,
|
|
||||||
placement: this.config.placement,
|
|
||||||
style: this.config.style
|
|
||||||
}).render(this.config.wrapper);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,17 +33,36 @@ class MessageRenderer {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const newWrapper = document.createElement('div');
|
const options = {
|
||||||
newWrapper.setAttribute('id', this.config.wrapper.replace('#', ''));
|
|
||||||
|
|
||||||
const sibling = document.querySelector(this.config.wrapper).nextSibling;
|
|
||||||
document.querySelector(this.config.wrapper).parentElement.removeChild(document.querySelector(this.config.wrapper));
|
|
||||||
sibling.parentElement.insertBefore(newWrapper, sibling);
|
|
||||||
paypal.Messages({
|
|
||||||
amount,
|
amount,
|
||||||
placement: this.config.placement,
|
placement: this.config.placement,
|
||||||
style: this.config.style
|
style: this.config.style
|
||||||
}).render(this.config.wrapper);
|
};
|
||||||
|
|
||||||
|
if (this.isOptionsFingerprintEqual(options)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const newWrapper = document.createElement('div');
|
||||||
|
newWrapper.setAttribute('id', this.config.wrapper.replace('#', ''));
|
||||||
|
|
||||||
|
const oldWrapper = document.querySelector(this.config.wrapper);
|
||||||
|
const sibling = oldWrapper.nextSibling;
|
||||||
|
oldWrapper.parentElement.removeChild(oldWrapper);
|
||||||
|
sibling.parentElement.insertBefore(newWrapper, sibling);
|
||||||
|
|
||||||
|
paypal.Messages(options).render(this.config.wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
isOptionsFingerprintEqual(options) {
|
||||||
|
const fingerprint = JSON.stringify(options);
|
||||||
|
|
||||||
|
if (this.optionsFingerprint === fingerprint) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.optionsFingerprint = fingerprint;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
shouldRender() {
|
shouldRender() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue