From 4cfbc4a01650a75ee6ea5520ffd6200d34c58f8c Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Wed, 11 Dec 2024 17:53:56 +0100 Subject: [PATCH] Move reactive component to label Trigger refresh if we have a new list --- resources/js/blocks/molliePaymentMethod.js | 68 ++++++++++++++++------ src/Assets/AssetsModule.php | 10 ++++ 2 files changed, 61 insertions(+), 17 deletions(-) diff --git a/resources/js/blocks/molliePaymentMethod.js b/resources/js/blocks/molliePaymentMethod.js index 09c1ee3a..85724cbe 100644 --- a/resources/js/blocks/molliePaymentMethod.js +++ b/resources/js/blocks/molliePaymentMethod.js @@ -1,16 +1,17 @@ let cachedAvailableGateways = {}; -function useMollieAvailableGateways(billing, filters, ajaxUrl, jQuery, item) { - const { billingData: { country }, cartTotal: { value }, currency: { code } } = billing; +function useMollieAvailableGateways(billing, currencyCode, cartTotal, filters, ajaxUrl, jQuery, item) { + const country = billing.country; + const code = currencyCode; + const value = cartTotal; const [localGateways, setLocalGateways] = wp.element.useState(cachedAvailableGateways); wp.element.useEffect(() => { - if (!country) return; + if (!country || !item) return; const currencyCode = code; const cartTotal = value; const currentFilterKey = currencyCode + "-" + filters.paymentLocale + "-" + country; if (cachedAvailableGateways.hasOwnProperty(currentFilterKey)) { - console.log('Using cached available gateways:', cachedAvailableGateways); setLocalGateways(cachedAvailableGateways); return; } @@ -28,31 +29,36 @@ function useMollieAvailableGateways(billing, filters, ajaxUrl, jQuery, item) { success: (response) => { cachedAvailableGateways = { ...cachedAvailableGateways, ...response.data }; setLocalGateways(cachedAvailableGateways); - console.log('Fetched available gateways:', cachedAvailableGateways); + const { extensionCartUpdate } = window.wc.blocksCheckout; + //I just need to trigger a refresh of the checkout page + extensionCartUpdate({ namespace: 'mollie-payments-for-woocommerce', data: {} }); }, error: (jqXHR, textStatus, errorThrown) => { console.warn('Failed to fetch available gateways:', textStatus, errorThrown); }, }); - }, [billing, filters.paymentLocale, ajaxUrl, jQuery, item]); + }, [billing, currencyCode, filters.paymentLocale, ajaxUrl, jQuery, item]); return localGateways; } +// Component that runs the hook but does not render anything. +function MollieGatewayUpdater({ billing, currencyCode, cartTotal, filters, ajaxUrl, jQuery, item }) { + + useMollieAvailableGateways(billing, currencyCode, cartTotal, filters, ajaxUrl, jQuery, item); + return null; +} + let onSubmitLocal let activePaymentMethodLocal let creditCardSelected = new Event("mollie_creditcard_component_selected", {bubbles: true}); const MollieComponent = (props) => { - let {onSubmit, activePaymentMethod, billing, item, useEffect, ajaxUrl, jQuery, emitResponse, eventRegistration, requiredFields, shippingData, isCompanyFieldVisible, isPhoneFieldVisible, filters} = props + let {onSubmit, activePaymentMethod, billing, item, useEffect, ajaxUrl, jQuery, emitResponse, eventRegistration, requiredFields, shippingData, isCompanyFieldVisible, isPhoneFieldVisible} = props const { responseTypes } = emitResponse; const {onPaymentSetup, onCheckoutValidation} = eventRegistration; - const availableGateways = useMollieAvailableGateways( - billing, - filters, - ajaxUrl, - jQuery, - item - ); + if (!item || !item.name) { + return
Loading payment methods...
; + } const [ selectedIssuer, selectIssuer ] = wp.element.useState(''); const [ inputPhone, selectPhone ] = wp.element.useState(''); const [ inputBirthdate, selectBirthdate ] = wp.element.useState(''); @@ -317,9 +323,33 @@ const MollieComponent = (props) => { return

{item.content}

} +const Label = ({ item, filters, ajaxUrl, jQuery }) => { + const cartData = wp.data.useSelect((select) => + select('wc/store/cart').getCartData(), + [] + ); + const cartTotals = wp.data.useSelect( (select) => select('wc/store/cart').getCartTotals(), [ ] ); + const cartTotal = cartTotals?.total_price || 0; + return ( + <> +
+ + + ); +}; + const molliePaymentMethod = (useEffect, ajaxUrl, filters, gatewayData, availableGateways, item, jQuery, requiredFields, isCompanyFieldVisible, isPhoneFieldVisible) =>{ cachedAvailableGateways = availableGateways + document.addEventListener('mollie_components_ready_to_submit', function () { onSubmitLocal() }) @@ -330,7 +360,12 @@ const molliePaymentMethod = (useEffect, ajaxUrl, filters, gatewayData, available } return { name: item.name, - label:
, + label: