Pass PayPalCommerceGateway as a dependency

This commit is contained in:
Mészáros Róbert 2020-04-09 12:56:05 +03:00
parent 493ae20d91
commit d31987ed46
5 changed files with 53 additions and 28 deletions

View file

@ -7,36 +7,55 @@ import CheckoutBootstap from './modules/CheckoutBootstap';
import Renderer from './modules/Renderer';
const bootstrap = () => {
const context = PayPalCommerceGateway.context;
const renderer = new Renderer;
const errorHandler = new ErrorHandler();
const defaultConfig = new CartConfig(
PayPalCommerceGateway,
errorHandler,
);
const context = PayPalCommerceGateway.context;
if (context === 'mini-cart') {
const miniCartBootstap = new MiniCartBootstap(renderer, defaultConfig);
const miniCartBootstap = new MiniCartBootstap(
PayPalCommerceGateway,
renderer,
defaultConfig,
);
miniCartBootstap.init();
}
if (context === 'product') {
const singleProductBootstap = new SingleProductBootstap(renderer);
const miniCartBootstap = new MiniCartBootstap(renderer, defaultConfig);
const singleProductBootstap = new SingleProductBootstap(
PayPalCommerceGateway,
renderer,
);
const miniCartBootstap = new MiniCartBootstap(
PayPalCommerceGateway,
renderer,
defaultConfig,
);
singleProductBootstap.init();
miniCartBootstap.init();
}
if (context === 'cart') {
const cartBootstrap = new CartBootstrap(renderer, defaultConfig);
const cartBootstrap = new CartBootstrap(
PayPalCommerceGateway,
renderer,
defaultConfig,
);
cartBootstrap.init();
}
if (context === 'checkout') {
const checkoutBootstap = new CheckoutBootstap(renderer, defaultConfig);
const checkoutBootstap = new CheckoutBootstap(
PayPalCommerceGateway,
renderer,
defaultConfig,
);
checkoutBootstap.init();
}

View file

@ -1,5 +1,6 @@
class CartBootstrap {
constructor(renderer, configurator) {
constructor(gateway, renderer, configurator) {
this.gateway = gateway;
this.renderer = renderer;
this.configurator = configurator;
}
@ -14,13 +15,13 @@ class CartBootstrap {
});
this.renderer.render(
PayPalCommerceGateway.button.wrapper,
this.gateway.button.wrapper,
this.configurator.configuration(),
);
}
shouldRender() {
return document.querySelector(PayPalCommerceGateway.button.wrapper);
return document.querySelector(this.gateway.button.wrapper);
}
}

View file

@ -1,5 +1,6 @@
class CheckoutBootstap {
constructor(renderer, configurator) {
constructor(gateway, renderer, configurator) {
this.gateway = gateway;
this.renderer = renderer;
this.configurator = configurator;
}
@ -9,23 +10,25 @@ class CheckoutBootstap {
return;
}
const buttonWrapper = this.gateway.button.wrapper;
const toggleButtons = () => {
const currentPaymentMethod = jQuery(
'input[name="payment_method"]:checked').val();
if (currentPaymentMethod !== 'ppcp-gateway') {
this.renderer.hideButtons(PayPalCommerceGateway.button.wrapper);
this.renderer.hideButtons(buttonWrapper);
jQuery('#place_order').show();
}
else {
this.renderer.showButtons(PayPalCommerceGateway.button.wrapper);
this.renderer.showButtons(buttonWrapper);
jQuery('#place_order').hide();
}
};
jQuery(document.body).on('updated_checkout', () => {
this.renderer.render(
PayPalCommerceGateway.button.wrapper,
buttonWrapper,
this.configurator.configuration(),
);
toggleButtons();
@ -36,18 +39,17 @@ class CheckoutBootstap {
});
this.renderer.render(
PayPalCommerceGateway.button.wrapper,
buttonWrapper,
this.configurator.configuration(),
);
}
shouldRender() {
if (document.querySelector(
PayPalCommerceGateway.button.cancel_wrapper)) {
if (document.querySelector(this.gateway.button.cancel_wrapper)) {
return false;
}
return document.querySelector(PayPalCommerceGateway.button.wrapper);
return document.querySelector(this.gateway.button.wrapper);
}
}

View file

@ -1,5 +1,6 @@
class MiniCartBootstap {
constructor(renderer, configurator) {
constructor(gateway, renderer, configurator) {
this.gateway = gateway;
this.renderer = renderer;
this.configurator = configurator;
}
@ -15,13 +16,13 @@ class MiniCartBootstap {
});
this.renderer.render(
PayPalCommerceGateway.button.mini_cart_wrapper,
this.gateway.button.mini_cart_wrapper,
this.configurator.configuration(),
);
}
shouldRender() {
return document.querySelector(PayPalCommerceGateway.button.mini_cart_wrapper);
return document.querySelector(this.gateway.button.mini_cart_wrapper);
}
}

View file

@ -3,7 +3,8 @@ import UpdateCart from './UpdateCart';
import SingleProductConfig from './SingleProductConfig';
class SingleProductBootstap {
constructor(renderer) {
constructor(gateway, renderer) {
this.gateway = gateway;
this.renderer = renderer;
}
@ -14,24 +15,25 @@ class SingleProductBootstap {
const errorHandler = new ErrorHandler();
const updateCart = new UpdateCart(
PayPalCommerceGateway.ajax.change_cart.endpoint,
PayPalCommerceGateway.ajax.change_cart.nonce,
this.gateway.ajax.change_cart.endpoint,
this.gateway.ajax.change_cart.nonce,
);
const buttonWrapper = this.gateway.button.wrapper;
const configurator = new SingleProductConfig(
PayPalCommerceGateway,
this.gateway,
updateCart,
() => {
this.renderer.showButtons(PayPalCommerceGateway.button.wrapper);
this.renderer.showButtons(buttonWrapper);
},
() => {
this.renderer.hideButtons(PayPalCommerceGateway.button.wrapper);
this.renderer.hideButtons(buttonWrapper);
},
document.querySelector('form.cart'),
errorHandler,
);
this.renderer.render(
PayPalCommerceGateway.button.wrapper,
buttonWrapper,
configurator.configuration(),
);
}
@ -41,7 +43,7 @@ class SingleProductBootstap {
return false;
}
return document.querySelector(PayPalCommerceGateway.button.wrapper);
return document.querySelector(this.gateway.button.wrapper);
}
}