-
Notifications
You must be signed in to change notification settings - Fork 219
Update canMakePayment to receive cart as argument and make it react to changes in billingData. #4776
Conversation
Size Change: +1.37 kB (0%) Total Size: 1.25 MB
ℹ️ View Unchanged
|
…r the effect calling refreshCanMakePayments
The initial approach was to debounce billingData and use this value as a dependency for the useEffect that runs refreshCanMakePayments. But because the depencies array can always change we decided to debounce the callback instead, ensuring this way that callback is not called multiple times: for example when typing a field in the billing address. Debounced was chosen instead of throttle because we want to call refreshCanMakePayments once the change event has stopped, with the final value.
@senadir the PR is ready for review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This checks out, thank you for working on this Raluca!
I left a small comment around spreading cart and some left over code.
The changelog entry also seems to be not part of the quote? you might want to remove the empty line to ensure it's included.
@@ -68,6 +67,7 @@ const usePaymentMethodRegistration = ( | |||
|
|||
useEffect( () => { | |||
canPayArgument.current = { | |||
cart, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about spreading cart here, which would include cartTotals, cartNeedsShipping, paymentRequirements, and also add the rest of items at the same level?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would advise against it, as cart
has a lot of keys, and that would make the signature really confusing. After deprecating the keys you mentioned it would just be cart
and billingData
and that makes it easier to understand where the data comes from.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't having cart as is just complicated one step further? so instead of arg.cartTotals
you need arg.cart.cartTotals
especially seeing that canPayArgument is made of cart related fields only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's also made of billingData
and shipping data
from the CustomerDataContext
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those are also present in Cart, shipping data is shippingAddress
and billing data is billingAddress
.
The only difference here is that billingAddress
reflects what's currently in state, and what's in Cart
reflects what comes from the server, so this might not always be in sync. billingData can still replace billingAddress from cart.
This is not a huge point to block the PR on as moving the other direction (spreading) is easier than reversing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The data is indeed out of sync. This would be an issue for extensions that need to make calculations on the current billingData
or shippingData
.
This is not a huge point to block the PR on as moving the other direction (spreading) is easier than reversing.
👍
beforeEach( async () => { | ||
fetchMock.mockResponse( ( req ) => { | ||
if ( req.url.match( /wc\/store\/cart/ ) ) { | ||
return Promise.resolve( JSON.stringify( previewCart ) ); | ||
} | ||
return Promise.resolve( '' ); | ||
} ); | ||
// need to clear the store resolution state between tests. | ||
await dispatch( storeKey ).invalidateResolutionForStore(); | ||
await dispatch( storeKey ).receiveCart( defaultCartState.cartData ); | ||
} ); | ||
|
||
afterEach( () => { | ||
fetchMock.resetMocks(); | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this added?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a call was being made by useStoreCart
and it needed to be mocked. I improved to mock to make it clearer.
@@ -87,11 +87,13 @@ A callback to determine whether the payment method should be available as an opt | |||
|
|||
``` | |||
canMakePayment( { | |||
cart: Cart, | |||
cartCoupons: CartCoupons, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cartCoupons is no longer exposed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really good catch
@@ -27,6 +28,8 @@ export interface Supports extends SupportsConfiguration { | |||
} | |||
|
|||
export interface CanMakePaymentArgument { | |||
cart: Cart; | |||
cartCoupons: CartResponseCoupons; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also here.
Please take another look, I've addressed your requests |
This PR
cart
object to the argument we pass to thecanMakePayment
callbackbillingData
to the dependencies list of the effect that callsrefresCanMakePayment
and closes canMakePayment isn’t tracking billingData changes #4766.Fixes:
#4670
#4766
A new ticket has been created for deprecating cart-derived values from the
canMakePayment
argument #4809How to test the changes in this Pull Request:
assets/js/base/context/providers/cart-checkout/payment-methods/use-payment-method-registration.ts
and edit for the purpose of testing only:registerPaymentMethodExtensionCallbacks
to the existing import from'@woocommerce/blocks-registry'
Use same address for billing
and write "Alexandra" for First Name in the Billing Address section. Cash on Delivery option should be available as a payment method.Changelog