-
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
Checkout: Convert useCreatePaymentMethods to TypeScript #50965
Checkout: Convert useCreatePaymentMethods to TypeScript #50965
Conversation
@@ -395,7 +496,6 @@ export default function useCreatePaymentMethods( { | |||
|
|||
const existingCardMethods = useCreateExistingCards( { | |||
storedCards, | |||
stripeConfiguration, |
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 argument is not used, so I'm removing it here. One of the benefits of type safety.
@@ -416,5 +516,5 @@ export default function useCreatePaymentMethods( { | |||
epsMethod, | |||
wechatMethod, | |||
bancontactMethod, | |||
].filter( Boolean ); | |||
].filter( doesValueExist ); |
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.
doesValueExist
(poorly named; it should be something like isValueNotFalsy
) is a TypeScript-safe way to filter out null values.
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 think isTruthy
is the most specific description of how it works (!!
)
Here is how your PR affects size of JS and CSS bundles shipped to the user's browser: Sections (~14 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. Async-loaded Components (~12 bytes added 📈 [gzipped])
React components that are loaded lazily, when a certain part of UI is displayed for the first time. 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. |
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.
Works great! I completed a paypal purchase with no problems.
|
||
export { useCreateExistingCards }; | ||
|
||
export function useCreatePayPal( { labelText = null } = {} ) { | ||
export function useCreatePayPal( { |
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 think this is called from client/me/purchases/manage-purchases/change-payment-method/use-create-assignable-payment-methods.ts
with an object?
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.
Should TranslateResult
from 'i18n-calypso' be added as a type for labelText
?
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.
That's correct. Is this syntax wrong? I found it hard to say "this can accept an optional object and that object can have an optional labelText
property".
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.
Should TranslateResult from 'i18n-calypso' be added as a type for labelText?
Ah, actually I think we should leave it as a string
, but that's a good point. That use should be modified to cast those translate results to a string
. I'll add that in.
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.
Can we move this type of stuff to an assertion function to perform the cast?
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 adjusted things around. How does it look now?
stripe: Stripe | null; | ||
shouldUseEbanx: boolean; | ||
shouldShowTaxFields?: boolean; | ||
activePayButtonText?: string | undefined; |
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.
Some uses of activePayButtonText
in non-ts files aren't cast to string.
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.
Yes, you're right. This is an issue but I'm not super concerned about it here. We'll need to handle those when we convert those files to TS and there will probably be lots of similar issues. I'm not worried because it shouldn't cause any issues as long as the translate()
is just producing a string (most uses of translate()
do) and not a React component. This will be a lot easier when we start switching to react-i18n
which separates the two concepts.
Changes proposed in this Pull Request
#50723 took the first step toward converting payment method creation to TypeScript by converting
useCreateExistingCards
. This PR converts the rest of the functions.Testing instructions
This should not have any effect on checkout as the changes should be almost entirely TypeScript (I've commented on the few actual code changes in the PR if you want to check them). Visit checkout and verify that the expected payment methods are available, particularly the new credit card method and PayPal. If they show up at all, then this should be safe.