Refactor handleButtonStatus and shouldEnable methods in buttons bootstraps

This commit is contained in:
Pedro Silva 2023-07-04 14:43:55 +01:00
parent 87665401e5
commit f6e625718e
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
5 changed files with 60 additions and 46 deletions

View file

@ -1,6 +1,6 @@
import CartActionHandler from '../ActionHandler/CartActionHandler';
import BootstrapHelper from "../Helper/BootstrapHelper";
import {setVisible} from "../Helper/Hiding";
import {disable, enable} from "../Helper/ButtonDisabler";
class CartBootstrap {
constructor(gateway, renderer, errorHandler) {
@ -48,15 +48,7 @@ class CartBootstrap {
}
handleButtonStatus() {
if (!this.shouldEnable()) {
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);
BootstrapHelper.handleButtonStatus(this);
}
shouldRender() {
@ -64,8 +56,7 @@ class CartBootstrap {
}
shouldEnable() {
return this.shouldRender()
&& this.gateway.button.is_disabled !== true;
return BootstrapHelper.shouldEnable(this);
}
render() {

View file

@ -5,6 +5,7 @@ import {
isSavedCardSelected, ORDER_BUTTON_SELECTOR,
PaymentMethods
} from "../Helper/CheckoutMethodState";
import BootstrapHelper from "../Helper/BootstrapHelper";
import {disable, enable} from "../Helper/ButtonDisabler";
class CheckoutBootstap {
@ -51,15 +52,7 @@ class CheckoutBootstap {
}
handleButtonStatus() {
if (!this.shouldEnable()) {
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);
BootstrapHelper.handleButtonStatus(this);
}
shouldRender() {
@ -71,8 +64,7 @@ class CheckoutBootstap {
}
shouldEnable() {
return this.shouldRender()
&& this.gateway.button.is_disabled !== true;
return BootstrapHelper.shouldEnable(this);
}
render() {

View file

@ -1,5 +1,5 @@
import CartActionHandler from '../ActionHandler/CartActionHandler';
import {disable, enable} from "../Helper/ButtonDisabler";
import BootstrapHelper from "../Helper/BootstrapHelper";
class MiniCartBootstap {
constructor(gateway, renderer, errorHandler) {
@ -29,13 +29,10 @@ class MiniCartBootstap {
}
handleButtonStatus() {
if (!this.shouldEnable()) {
this.renderer.disableSmartButtons(this.gateway.button.mini_cart_wrapper);
disable(this.gateway.button.mini_cart_wrapper);
return;
}
this.renderer.enableSmartButtons(this.gateway.button.mini_cart_wrapper);
enable(this.gateway.button.mini_cart_wrapper);
BootstrapHelper.handleButtonStatus(this, {
wrapper: this.gateway.button.mini_cart_wrapper,
skipMessages: true
});
}
shouldRender() {
@ -44,8 +41,9 @@ class MiniCartBootstap {
}
shouldEnable() {
return this.shouldRender()
&& this.gateway.button.is_mini_cart_disabled !== true;
return BootstrapHelper.shouldEnable(this, {
isDisabled: !!this.gateway.button.is_mini_cart_disabled
});
}
render() {

View file

@ -1,7 +1,7 @@
import UpdateCart from "../Helper/UpdateCart";
import SingleProductActionHandler from "../ActionHandler/SingleProductActionHandler";
import {hide, show} from "../Helper/Hiding";
import {disable, enable} from "../Helper/ButtonDisabler";
import BootstrapHelper from "../Helper/BootstrapHelper";
class SingleProductBootstap {
constructor(gateway, renderer, messages, errorHandler) {
@ -39,15 +39,9 @@ class SingleProductBootstap {
}
handleButtonStatus() {
if (!this.shouldEnable()) {
this.renderer.disableSmartButtons(this.gateway.button.wrapper);
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);
BootstrapHelper.handleButtonStatus(this, {
formSelector: this.formSelector
});
}
init() {
@ -92,10 +86,9 @@ class SingleProductBootstap {
const form = this.form();
const addToCartButton = form ? form.querySelector('.single_add_to_cart_button') : null;
return this.shouldRender()
return BootstrapHelper.shouldEnable(this)
&& !this.priceAmountIsZero()
&& ((null === addToCartButton) || !addToCartButton.classList.contains('disabled'))
&& this.gateway.button.is_disabled !== true;
&& ((null === addToCartButton) || !addToCartButton.classList.contains('disabled'));
}
priceAmount() {

View file

@ -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;
}
}