Skip to content
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

Conversation

tyxla
Copy link
Member

@tyxla tyxla commented Apr 6, 2023

Proposed Changes

This PR migrates the partner portal credit card store to use createReduxStore() and register() instead of registerStore().

Part of #74399. A follow-up to #73890.

Second take of #74656, which was reverted in #75371.

Testing Instructions

Pre-merge Checklist

  • Has the general commit checklist been followed? (PCYsg-hS-p2)
  • Have you written new tests for your changes?
  • Have you tested the feature in Simple (P9HQHe-k8-p2), Atomic (P9HQHe-jW-p2), and self-hosted Jetpack sites (PCYsg-g6b-p2)?
  • Have you checked for TypeScript, React or other console errors?
  • Have you used memoizing on expensive computations? More info in Memoizing with create-selector and Using memoizing selectors and Our Approach to Data
  • Have we added the "[Status] String Freeze" label as soon as any new strings were ready for translation (p4TIVU-5Jq-p2)?
  • For changes affecting Jetpack: Have we added the "[Status] Needs Privacy Updates" label if this pull request changes what data or activity we track or use (p4TIVU-ajp-p2)?

@github-actions
Copy link

github-actions bot commented Apr 6, 2023

@matticbot
Copy link
Contributor

matticbot commented Apr 6, 2023

Here is how your PR affects size of JS and CSS bundles shipped to the user's browser:

Sections (~10 bytes added 📈 [gzipped])

name                          parsed_size           gzip_size
jetpack-cloud-partner-portal       -124 B  (-0.0%)      +10 B  (+0.0%)

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.
Gzip Size: Compressed size of the JS and CSS files. This much data needs to be downloaded over network.

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(),
Copy link
Member Author

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.

Comment on lines +28 to +37
const { fields, useAsPrimaryPaymentMethod, errors, incompleteFieldKeys } = useSelect(
( select ) => {
const store = select( creditCardStore );
return {
fields: store.getFields(),
useAsPrimaryPaymentMethod: store.useAsPrimaryPaymentMethod(),
errors: store.getCardDataErrors(),
incompleteFieldKeys: store.getIncompleteFieldKeys(),
};
},
Copy link
Member Author

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 one useSelect() 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 );
Copy link
Member Author

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 = () => {
Copy link
Member Author

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.

Copy link
Contributor

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' );
Copy link
Member Author

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', '' );
Copy link
Member Author

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' );
Copy link
Member Author

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 ]
Copy link
Member Author

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 }
Copy link
Member Author

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.

@tyxla tyxla marked this pull request as ready for review April 6, 2023 11:11
@tyxla tyxla requested review from yashwin and vitozev April 6, 2023 11:11
@matticbot matticbot added the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Apr 6, 2023
@tyxla tyxla self-assigned this Apr 6, 2023
Copy link
Contributor

@yashwin yashwin left a 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 = () => {
Copy link
Contributor

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.

@tyxla tyxla merged commit 072a0d8 into trunk Apr 7, 2023
@tyxla tyxla deleted the revert-75371-revert-74656-refactor/partner-portal-credit-card-store branch April 7, 2023 09:51
@github-actions github-actions bot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Apr 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants