mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 10:55:00 +08:00
Refactor handleButtonStatus and shouldEnable methods in buttons bootstraps
This commit is contained in:
parent
87665401e5
commit
f6e625718e
5 changed files with 60 additions and 46 deletions
|
@ -1,6 +1,6 @@
|
||||||
import CartActionHandler from '../ActionHandler/CartActionHandler';
|
import CartActionHandler from '../ActionHandler/CartActionHandler';
|
||||||
|
import BootstrapHelper from "../Helper/BootstrapHelper";
|
||||||
import {setVisible} from "../Helper/Hiding";
|
import {setVisible} from "../Helper/Hiding";
|
||||||
import {disable, enable} from "../Helper/ButtonDisabler";
|
|
||||||
|
|
||||||
class CartBootstrap {
|
class CartBootstrap {
|
||||||
constructor(gateway, renderer, errorHandler) {
|
constructor(gateway, renderer, errorHandler) {
|
||||||
|
@ -48,15 +48,7 @@ class CartBootstrap {
|
||||||
}
|
}
|
||||||
|
|
||||||
handleButtonStatus() {
|
handleButtonStatus() {
|
||||||
if (!this.shouldEnable()) {
|
BootstrapHelper.handleButtonStatus(this);
|
||||||
this.renderer.disableSmartButtons(this.gateway.button.wrapper);
|
|
||||||
disable(this.gateway.button.wrapper);
|
|
||||||
disable(this.gateway.messages.wrapper);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.renderer.enableSmartButtons(this.gateway.button.wrapper);
|
|
||||||
enable(this.gateway.button.wrapper);
|
|
||||||
enable(this.gateway.messages.wrapper);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
shouldRender() {
|
shouldRender() {
|
||||||
|
@ -64,8 +56,7 @@ class CartBootstrap {
|
||||||
}
|
}
|
||||||
|
|
||||||
shouldEnable() {
|
shouldEnable() {
|
||||||
return this.shouldRender()
|
return BootstrapHelper.shouldEnable(this);
|
||||||
&& this.gateway.button.is_disabled !== true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|
|
@ -5,6 +5,7 @@ import {
|
||||||
isSavedCardSelected, ORDER_BUTTON_SELECTOR,
|
isSavedCardSelected, ORDER_BUTTON_SELECTOR,
|
||||||
PaymentMethods
|
PaymentMethods
|
||||||
} from "../Helper/CheckoutMethodState";
|
} from "../Helper/CheckoutMethodState";
|
||||||
|
import BootstrapHelper from "../Helper/BootstrapHelper";
|
||||||
import {disable, enable} from "../Helper/ButtonDisabler";
|
import {disable, enable} from "../Helper/ButtonDisabler";
|
||||||
|
|
||||||
class CheckoutBootstap {
|
class CheckoutBootstap {
|
||||||
|
@ -51,15 +52,7 @@ class CheckoutBootstap {
|
||||||
}
|
}
|
||||||
|
|
||||||
handleButtonStatus() {
|
handleButtonStatus() {
|
||||||
if (!this.shouldEnable()) {
|
BootstrapHelper.handleButtonStatus(this);
|
||||||
this.renderer.disableSmartButtons(this.gateway.button.wrapper);
|
|
||||||
disable(this.gateway.button.wrapper);
|
|
||||||
disable(this.gateway.messages.wrapper);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.renderer.enableSmartButtons(this.gateway.button.wrapper);
|
|
||||||
enable(this.gateway.button.wrapper);
|
|
||||||
enable(this.gateway.messages.wrapper);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
shouldRender() {
|
shouldRender() {
|
||||||
|
@ -71,8 +64,7 @@ class CheckoutBootstap {
|
||||||
}
|
}
|
||||||
|
|
||||||
shouldEnable() {
|
shouldEnable() {
|
||||||
return this.shouldRender()
|
return BootstrapHelper.shouldEnable(this);
|
||||||
&& this.gateway.button.is_disabled !== true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import CartActionHandler from '../ActionHandler/CartActionHandler';
|
import CartActionHandler from '../ActionHandler/CartActionHandler';
|
||||||
import {disable, enable} from "../Helper/ButtonDisabler";
|
import BootstrapHelper from "../Helper/BootstrapHelper";
|
||||||
|
|
||||||
class MiniCartBootstap {
|
class MiniCartBootstap {
|
||||||
constructor(gateway, renderer, errorHandler) {
|
constructor(gateway, renderer, errorHandler) {
|
||||||
|
@ -29,13 +29,10 @@ class MiniCartBootstap {
|
||||||
}
|
}
|
||||||
|
|
||||||
handleButtonStatus() {
|
handleButtonStatus() {
|
||||||
if (!this.shouldEnable()) {
|
BootstrapHelper.handleButtonStatus(this, {
|
||||||
this.renderer.disableSmartButtons(this.gateway.button.mini_cart_wrapper);
|
wrapper: this.gateway.button.mini_cart_wrapper,
|
||||||
disable(this.gateway.button.mini_cart_wrapper);
|
skipMessages: true
|
||||||
return;
|
});
|
||||||
}
|
|
||||||
this.renderer.enableSmartButtons(this.gateway.button.mini_cart_wrapper);
|
|
||||||
enable(this.gateway.button.mini_cart_wrapper);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
shouldRender() {
|
shouldRender() {
|
||||||
|
@ -44,8 +41,9 @@ class MiniCartBootstap {
|
||||||
}
|
}
|
||||||
|
|
||||||
shouldEnable() {
|
shouldEnable() {
|
||||||
return this.shouldRender()
|
return BootstrapHelper.shouldEnable(this, {
|
||||||
&& this.gateway.button.is_mini_cart_disabled !== true;
|
isDisabled: !!this.gateway.button.is_mini_cart_disabled
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
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 {hide, show} from "../Helper/Hiding";
|
||||||
import {disable, enable} from "../Helper/ButtonDisabler";
|
import BootstrapHelper from "../Helper/BootstrapHelper";
|
||||||
|
|
||||||
class SingleProductBootstap {
|
class SingleProductBootstap {
|
||||||
constructor(gateway, renderer, messages, errorHandler) {
|
constructor(gateway, renderer, messages, errorHandler) {
|
||||||
|
@ -39,15 +39,9 @@ class SingleProductBootstap {
|
||||||
}
|
}
|
||||||
|
|
||||||
handleButtonStatus() {
|
handleButtonStatus() {
|
||||||
if (!this.shouldEnable()) {
|
BootstrapHelper.handleButtonStatus(this, {
|
||||||
this.renderer.disableSmartButtons(this.gateway.button.wrapper);
|
formSelector: this.formSelector
|
||||||
disable(this.gateway.button.wrapper, this.formSelector);
|
});
|
||||||
disable(this.gateway.messages.wrapper);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.renderer.enableSmartButtons(this.gateway.button.wrapper);
|
|
||||||
enable(this.gateway.button.wrapper);
|
|
||||||
enable(this.gateway.messages.wrapper);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
|
@ -92,10 +86,9 @@ class SingleProductBootstap {
|
||||||
const form = this.form();
|
const form = this.form();
|
||||||
const addToCartButton = form ? form.querySelector('.single_add_to_cart_button') : null;
|
const addToCartButton = form ? form.querySelector('.single_add_to_cart_button') : null;
|
||||||
|
|
||||||
return this.shouldRender()
|
return BootstrapHelper.shouldEnable(this)
|
||||||
&& !this.priceAmountIsZero()
|
&& !this.priceAmountIsZero()
|
||||||
&& ((null === addToCartButton) || !addToCartButton.classList.contains('disabled'))
|
&& ((null === addToCartButton) || !addToCartButton.classList.contains('disabled'));
|
||||||
&& this.gateway.button.is_disabled !== true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
priceAmount() {
|
priceAmount() {
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
import {disable, enable} from "./ButtonDisabler";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Common Bootstrap methods to avoid code repetition.
|
||||||
|
*/
|
||||||
|
export default class BootstrapHelper {
|
||||||
|
|
||||||
|
static handleButtonStatus(bs, options) {
|
||||||
|
options = options || {};
|
||||||
|
options.wrapper = options.wrapper || bs.gateway.button.wrapper;
|
||||||
|
options.messagesWrapper = options.messagesWrapper || bs.gateway.messages.wrapper;
|
||||||
|
options.skipMessages = options.skipMessages || false;
|
||||||
|
|
||||||
|
if (!bs.shouldEnable()) {
|
||||||
|
bs.renderer.disableSmartButtons(options.wrapper);
|
||||||
|
disable(options.wrapper, options.formSelector || null);
|
||||||
|
|
||||||
|
if (!options.skipMessages) {
|
||||||
|
disable(options.messagesWrapper);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
bs.renderer.enableSmartButtons(options.wrapper);
|
||||||
|
enable(options.wrapper);
|
||||||
|
|
||||||
|
if (!options.skipMessages) {
|
||||||
|
enable(options.messagesWrapper);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static shouldEnable(bs, options) {
|
||||||
|
options = options || {};
|
||||||
|
if (typeof options.isDisabled === 'undefined') {
|
||||||
|
options.isDisabled = bs.gateway.button.is_disabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
return bs.shouldRender()
|
||||||
|
&& options.isDisabled !== true;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue