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

Checkout: Convert useCreatePaymentMethods to TypeScript #50965

Merged
merged 5 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ export default function useCreateAssignablePaymentMethods(
stripe,
shouldUseEbanx: false,
shouldShowTaxFields: true,
activePayButtonText: translate( 'Save card' ),
activePayButtonText: String( translate( 'Save card' ) ),
} );

const payPalMethod = useCreatePayPal( {
labelText:
currentPaymentMethodId === 'paypal-existing'
? translate( 'New PayPal account' )
: translate( 'PayPal' ),
? String( translate( 'New PayPal account' ) )
: String( translate( 'PayPal' ) ),
} );

const storedCards = useSelector( getStoredCards );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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;
Copy link
Contributor

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.

Copy link
Member Author

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.

} ): PaymentMethod | null {
const shouldLoadStripeMethod = ! isStripeLoading && ! stripeLoadingError;
const stripePaymentMethodStore = useMemo( () => createCreditCardPaymentMethodStore(), [] );
const stripeMethod = useMemo(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -230,7 +313,7 @@ function useCreateEps( { isStripeLoading, stripeLoadingError, stripeConfiguratio
);
}

function useCreateNetbanking() {
function useCreateNetbanking(): PaymentMethod {
const paymentMethodStore = useMemo( () => createNetBankingPaymentMethodStore(), [] );
return useMemo(
() =>
Expand All @@ -241,7 +324,7 @@ function useCreateNetbanking() {
);
}

function useCreateIdWallet() {
function useCreateIdWallet(): PaymentMethod {
const paymentMethodStore = useMemo( () => createIdWalletPaymentMethodStore(), [] );
return useMemo(
() =>
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -369,7 +468,7 @@ export default function useCreatePaymentMethods( {

const shouldUseEbanx = Boolean(
responseCart?.allowed_payment_methods?.includes(
translateCheckoutPaymentMethodToWpcomPaymentMethod( 'ebanx' )
translateCheckoutPaymentMethodToWpcomPaymentMethod( 'ebanx' ) ?? ''
)
);
const stripeMethod = useCreateCreditCard( {
Expand All @@ -395,7 +494,6 @@ export default function useCreatePaymentMethods( {

const existingCardMethods = useCreateExistingCards( {
storedCards,
stripeConfiguration,
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 argument is not used, so I'm removing it here. One of the benefits of type safety.

} );

return [
Expand All @@ -416,5 +514,5 @@ export default function useCreatePaymentMethods( {
epsMethod,
wechatMethod,
bancontactMethod,
].filter( Boolean );
].filter( doesValueExist );
Copy link
Member Author

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.

Copy link
Contributor

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 (!!)

}
4 changes: 3 additions & 1 deletion packages/calypso-stripe/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ export interface StripeConfiguration {

export type ReloadStripeConfiguration = () => void;

export type StripeLoadingError = undefined | null | Error;

export interface StripeData {
stripe: null | Stripe;
stripeConfiguration: null | StripeConfiguration;
isStripeLoading: boolean;
stripeLoadingError: undefined | null | Error;
stripeLoadingError: StripeLoadingError;
reloadStripeConfiguration: ReloadStripeConfiguration;
}

Expand Down