-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Data Stores: Refactor partner portal credit card store to createReduxStore()
- take 2
#75377
Data Stores: Refactor partner portal credit card store to createReduxStore()
- take 2
#75377
Conversation
Here is how your PR affects size of JS and CSS bundles shipped to the user's browser: Sections (~10 bytes added 📈 [gzipped])
Sections contain code specific for a given set of routes. Is downloaded and parsed only when a particular route is navigated to. Legend What is parsed and gzip size?Parsed Size: Uncompressed size of the JS and CSS files. This much code needs to be parsed and stored in memory. Generated by performance advisor bot at iscalypsofastyet.com. |
@@ -19,7 +19,7 @@ export default function CreditCardElementField( { | |||
const { formStatus } = useFormStatus(); | |||
const isDisabled = formStatus !== FormStatus.READY; | |||
const { card: cardError } = useSelect( | |||
( select ) => ( select( 'credit-card' ) as CreditCardSelectors ).getCardDataErrors(), | |||
( select ) => select( creditCardStore ).getCardDataErrors(), |
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.
Nothing special about most of the changed instances - we reference the store through the existing store object instead of its name, which is the recommended way in Gutenberg nowadays.
const { fields, useAsPrimaryPaymentMethod, errors, incompleteFieldKeys } = useSelect( | ||
( select ) => { | ||
const store = select( creditCardStore ); | ||
return { | ||
fields: store.getFields(), | ||
useAsPrimaryPaymentMethod: store.useAsPrimaryPaymentMethod(), | ||
errors: store.getCardDataErrors(), | ||
incompleteFieldKeys: store.getIncompleteFieldKeys(), | ||
}; | ||
}, |
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 contains 2 different sets of changes:
- Combining all
creditCardStore
selectors into oneuseSelect()
for brevity and predictability. - Removing some unnecessary types and relying on type inference instead.
@@ -54,30 +53,53 @@ export default function CreditCardSubmitButton( { | |||
return __( 'Please wait…' ); | |||
}, [ formStatus, activeButtonText, __ ] ); | |||
|
|||
const { setCardDataError, setFieldValue, setFieldError } = useDispatch( creditCardStore ); |
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.
We're using useDispatch()
now to get mapped action creators.
@@ -54,30 +53,53 @@ export default function CreditCardSubmitButton( { | |||
return __( 'Please wait…' ); | |||
}, [ formStatus, activeButtonText, __ ] ); | |||
|
|||
const { setCardDataError, setFieldValue, setFieldError } = useDispatch( creditCardStore ); | |||
|
|||
const handleButtonClick = () => { |
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.
We're inlining this function to be able to use the data from the hooks.
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.
Nice. This is way neater now.
const { setCardDataError, setFieldValue, setFieldError } = useDispatch( creditCardStore ); | ||
|
||
const handleButtonClick = () => { | ||
debug( 'validating credit card fields' ); |
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.
We're also inlining the credit card validation function here, that way we can take advantage of the existing store values without having to pass the entire store object. We could keep the function separate, but then we'd still need to pass all the various state data and mapped action creators, which is an extra step of unnecessary complication IMHO.
|
||
if ( ! cardholderName?.value.length ) { | ||
// Touch the field so it displays a validation error | ||
setFieldValue( 'cardholderName', '' ); |
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.
See how natural it is to dispatch actions that way.
jest.mock( '@stripe/stripe-js', () => ( { | ||
loadStripe: () => null, | ||
} ) ); | ||
|
||
jest.mock( '@wordpress/data' ); |
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.
We don't need all those mocks; we can just let @wordpress/data
do its job.
stripe, | ||
stripeConfiguration, | ||
activePayButtonText, | ||
} ) | ||
: null, | ||
[ shouldLoadStripeMethod, store, stripe, stripeConfiguration, activePayButtonText ] |
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.
We no longer need the store since we reference it directly in the component.
@@ -23,7 +20,6 @@ export function createStoredCreditCardMethod( { | |||
activeContent: <CreditCardFields />, | |||
submitButton: ( | |||
<CreditCardSubmitButton | |||
store={ store } |
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.
We no longer need the store since we reference it directly in the component.
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.
LGTM. Thanks for making these changes and fixing the issue reported. I was able to add a new card as a payment method, and all the validations work as expected.
@@ -54,30 +53,53 @@ export default function CreditCardSubmitButton( { | |||
return __( 'Please wait…' ); | |||
}, [ formStatus, activeButtonText, __ ] ); | |||
|
|||
const { setCardDataError, setFieldValue, setFieldError } = useDispatch( creditCardStore ); | |||
|
|||
const handleButtonClick = () => { |
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.
Nice. This is way neater now.
Proposed Changes
This PR migrates the partner portal credit card store to use
createReduxStore()
andregister()
instead ofregisterStore()
.Part of #74399. A follow-up to #73890.
Second take of #74656, which was reverted in #75371.
Testing Instructions
createReduxStore()
" #75371.Pre-merge Checklist