-
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
Changes from all commits
a02c618
510aa06
4485129
424f63e
92efac0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ import { | |
createApplePayMethod, | ||
} from '@automattic/composite-checkout'; | ||
import { useShoppingCart } from '@automattic/shopping-cart'; | ||
import type { StripeConfiguration, Stripe, StripeLoadingError } from '@automattic/calypso-stripe'; | ||
import type { PaymentMethod } from '@automattic/composite-checkout'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -46,10 +48,12 @@ import { createFullCreditsMethod } from '../../payment-methods/full-credits'; | |
import { createFreePaymentMethod } from '../../payment-methods/free-purchase'; | ||
import { translateCheckoutPaymentMethodToWpcomPaymentMethod } from 'calypso/my-sites/checkout/composite-checkout/lib/translate-payment-method-names'; | ||
import useCreateExistingCards from './use-create-existing-cards'; | ||
import doesValueExist from '../../lib/does-value-exist'; | ||
import type { StoredCard } from '../../types/stored-cards'; | ||
|
||
export { useCreateExistingCards }; | ||
|
||
export function useCreatePayPal( { labelText = null } = {} ) { | ||
export function useCreatePayPal( { labelText }: { labelText?: string | null } ): PaymentMethod { | ||
const paypalMethod = useMemo( () => createPayPalMethod( { labelText } ), [ labelText ] ); | ||
return paypalMethod; | ||
} | ||
|
@@ -62,7 +66,15 @@ export function useCreateCreditCard( { | |
shouldUseEbanx, | ||
shouldShowTaxFields = false, | ||
activePayButtonText = undefined, | ||
} ) { | ||
}: { | ||
isStripeLoading: boolean; | ||
stripeLoadingError: StripeLoadingError; | ||
stripeConfiguration: StripeConfiguration | null; | ||
stripe: Stripe | null; | ||
shouldUseEbanx: boolean; | ||
shouldShowTaxFields?: boolean; | ||
activePayButtonText?: string | undefined; | ||
} ): PaymentMethod | null { | ||
const shouldLoadStripeMethod = ! isStripeLoading && ! stripeLoadingError; | ||
const stripePaymentMethodStore = useMemo( () => createCreditCardPaymentMethodStore(), [] ); | ||
const stripeMethod = useMemo( | ||
|
@@ -90,7 +102,17 @@ export function useCreateCreditCard( { | |
return stripeMethod; | ||
} | ||
|
||
function useCreateAlipay( { isStripeLoading, stripeLoadingError, stripeConfiguration, stripe } ) { | ||
function useCreateAlipay( { | ||
isStripeLoading, | ||
stripeLoadingError, | ||
stripeConfiguration, | ||
stripe, | ||
}: { | ||
isStripeLoading: boolean; | ||
stripeLoadingError: StripeLoadingError; | ||
stripeConfiguration: StripeConfiguration | null; | ||
stripe: Stripe | null; | ||
} ): PaymentMethod | null { | ||
const shouldLoad = ! isStripeLoading && ! stripeLoadingError; | ||
const paymentMethodStore = useMemo( () => createAlipayPaymentMethodStore(), [] ); | ||
return useMemo( | ||
|
@@ -106,7 +128,17 @@ function useCreateAlipay( { isStripeLoading, stripeLoadingError, stripeConfigura | |
); | ||
} | ||
|
||
function useCreateP24( { isStripeLoading, stripeLoadingError, stripeConfiguration, stripe } ) { | ||
function useCreateP24( { | ||
isStripeLoading, | ||
stripeLoadingError, | ||
stripeConfiguration, | ||
stripe, | ||
}: { | ||
isStripeLoading: boolean; | ||
stripeLoadingError: StripeLoadingError; | ||
stripeConfiguration: StripeConfiguration | null; | ||
stripe: Stripe | null; | ||
} ): PaymentMethod | null { | ||
const shouldLoad = ! isStripeLoading && ! stripeLoadingError; | ||
const paymentMethodStore = useMemo( () => createP24PaymentMethodStore(), [] ); | ||
return useMemo( | ||
|
@@ -127,7 +159,12 @@ function useCreateBancontact( { | |
stripeLoadingError, | ||
stripeConfiguration, | ||
stripe, | ||
} ) { | ||
}: { | ||
isStripeLoading: boolean; | ||
stripeLoadingError: StripeLoadingError; | ||
stripeConfiguration: StripeConfiguration | null; | ||
stripe: Stripe | null; | ||
} ): PaymentMethod | null { | ||
const shouldLoad = ! isStripeLoading && ! stripeLoadingError; | ||
const paymentMethodStore = useMemo( () => createBancontactPaymentMethodStore(), [] ); | ||
return useMemo( | ||
|
@@ -143,7 +180,17 @@ function useCreateBancontact( { | |
); | ||
} | ||
|
||
function useCreateGiropay( { isStripeLoading, stripeLoadingError, stripeConfiguration, stripe } ) { | ||
function useCreateGiropay( { | ||
isStripeLoading, | ||
stripeLoadingError, | ||
stripeConfiguration, | ||
stripe, | ||
}: { | ||
isStripeLoading: boolean; | ||
stripeLoadingError: StripeLoadingError; | ||
stripeConfiguration: StripeConfiguration | null; | ||
stripe: Stripe | null; | ||
} ): PaymentMethod | null { | ||
const shouldLoad = ! isStripeLoading && ! stripeLoadingError; | ||
const paymentMethodStore = useMemo( () => createGiropayPaymentMethodStore(), [] ); | ||
return useMemo( | ||
|
@@ -165,7 +212,13 @@ function useCreateWeChat( { | |
stripeConfiguration, | ||
stripe, | ||
siteSlug, | ||
} ) { | ||
}: { | ||
isStripeLoading: boolean; | ||
stripeLoadingError: StripeLoadingError; | ||
stripeConfiguration: StripeConfiguration | null; | ||
stripe: Stripe | null; | ||
siteSlug?: string | undefined; | ||
} ): PaymentMethod | null { | ||
const shouldLoad = ! isStripeLoading && ! stripeLoadingError; | ||
const paymentMethodStore = useMemo( () => createWeChatPaymentMethodStore(), [] ); | ||
return useMemo( | ||
|
@@ -182,7 +235,17 @@ function useCreateWeChat( { | |
); | ||
} | ||
|
||
function useCreateIdeal( { isStripeLoading, stripeLoadingError, stripeConfiguration, stripe } ) { | ||
function useCreateIdeal( { | ||
isStripeLoading, | ||
stripeLoadingError, | ||
stripeConfiguration, | ||
stripe, | ||
}: { | ||
isStripeLoading: boolean; | ||
stripeLoadingError: StripeLoadingError; | ||
stripeConfiguration: StripeConfiguration | null; | ||
stripe: Stripe | null; | ||
} ): PaymentMethod | null { | ||
const shouldLoad = ! isStripeLoading && ! stripeLoadingError; | ||
const paymentMethodStore = useMemo( () => createIdealPaymentMethodStore(), [] ); | ||
return useMemo( | ||
|
@@ -198,7 +261,17 @@ function useCreateIdeal( { isStripeLoading, stripeLoadingError, stripeConfigurat | |
); | ||
} | ||
|
||
function useCreateSofort( { isStripeLoading, stripeLoadingError, stripeConfiguration, stripe } ) { | ||
function useCreateSofort( { | ||
isStripeLoading, | ||
stripeLoadingError, | ||
stripeConfiguration, | ||
stripe, | ||
}: { | ||
isStripeLoading: boolean; | ||
stripeLoadingError: StripeLoadingError; | ||
stripeConfiguration: StripeConfiguration | null; | ||
stripe: Stripe | null; | ||
} ): PaymentMethod | null { | ||
const shouldLoad = ! isStripeLoading && ! stripeLoadingError; | ||
const paymentMethodStore = useMemo( () => createSofortPaymentMethodStore(), [] ); | ||
return useMemo( | ||
|
@@ -214,7 +287,17 @@ function useCreateSofort( { isStripeLoading, stripeLoadingError, stripeConfigura | |
); | ||
} | ||
|
||
function useCreateEps( { isStripeLoading, stripeLoadingError, stripeConfiguration, stripe } ) { | ||
function useCreateEps( { | ||
isStripeLoading, | ||
stripeLoadingError, | ||
stripeConfiguration, | ||
stripe, | ||
}: { | ||
isStripeLoading: boolean; | ||
stripeLoadingError: StripeLoadingError; | ||
stripeConfiguration: StripeConfiguration | null; | ||
stripe: Stripe | null; | ||
} ): PaymentMethod | null { | ||
const shouldLoad = ! isStripeLoading && ! stripeLoadingError; | ||
const paymentMethodStore = useMemo( () => createEpsPaymentMethodStore(), [] ); | ||
return useMemo( | ||
|
@@ -230,7 +313,7 @@ function useCreateEps( { isStripeLoading, stripeLoadingError, stripeConfiguratio | |
); | ||
} | ||
|
||
function useCreateNetbanking() { | ||
function useCreateNetbanking(): PaymentMethod { | ||
const paymentMethodStore = useMemo( () => createNetBankingPaymentMethodStore(), [] ); | ||
return useMemo( | ||
() => | ||
|
@@ -241,7 +324,7 @@ function useCreateNetbanking() { | |
); | ||
} | ||
|
||
function useCreateIdWallet() { | ||
function useCreateIdWallet(): PaymentMethod { | ||
const paymentMethodStore = useMemo( () => createIdWalletPaymentMethodStore(), [] ); | ||
return useMemo( | ||
() => | ||
|
@@ -278,7 +361,14 @@ function useCreateApplePay( { | |
stripe, | ||
isApplePayAvailable, | ||
isApplePayLoading, | ||
} ) { | ||
}: { | ||
isStripeLoading: boolean; | ||
stripeLoadingError: StripeLoadingError; | ||
stripeConfiguration: StripeConfiguration | null; | ||
stripe: Stripe | null; | ||
isApplePayAvailable: boolean; | ||
isApplePayLoading: boolean; | ||
} ): PaymentMethod | null { | ||
const isStripeReady = ! isStripeLoading && ! stripeLoadingError && stripe && stripeConfiguration; | ||
|
||
const shouldCreateApplePayMethod = isStripeReady && ! isApplePayLoading && isApplePayAvailable; | ||
|
@@ -299,10 +389,19 @@ export default function useCreatePaymentMethods( { | |
isApplePayLoading, | ||
storedCards, | ||
siteSlug, | ||
} ) { | ||
}: { | ||
isStripeLoading: boolean; | ||
stripeLoadingError: StripeLoadingError; | ||
stripeConfiguration: StripeConfiguration | null; | ||
stripe: Stripe | null; | ||
isApplePayAvailable: boolean; | ||
isApplePayLoading: boolean; | ||
storedCards: StoredCard[]; | ||
siteSlug: string | undefined; | ||
} ): PaymentMethod[] { | ||
const { responseCart } = useShoppingCart(); | ||
|
||
const paypalMethod = useCreatePayPal(); | ||
const paypalMethod = useCreatePayPal( {} ); | ||
|
||
const idealMethod = useCreateIdeal( { | ||
isStripeLoading, | ||
|
@@ -369,7 +468,7 @@ export default function useCreatePaymentMethods( { | |
|
||
const shouldUseEbanx = Boolean( | ||
responseCart?.allowed_payment_methods?.includes( | ||
translateCheckoutPaymentMethodToWpcomPaymentMethod( 'ebanx' ) | ||
translateCheckoutPaymentMethodToWpcomPaymentMethod( 'ebanx' ) ?? '' | ||
) | ||
); | ||
const stripeMethod = useCreateCreditCard( { | ||
|
@@ -395,7 +494,6 @@ export default function useCreatePaymentMethods( { | |
|
||
const existingCardMethods = useCreateExistingCards( { | ||
storedCards, | ||
stripeConfiguration, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} ); | ||
|
||
return [ | ||
|
@@ -416,5 +514,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 commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
} |
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 oftranslate()
do) and not a React component. This will be a lot easier when we start switching toreact-i18n
which separates the two concepts.