Skip to content

Commit

Permalink
TypeScript migration: Affirm and Afterpay payment details components (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
timur27 authored Jun 18, 2024
1 parent 602e466 commit 156221f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 19 deletions.
4 changes: 4 additions & 0 deletions changelog/dev-migrate-affirm-afterpay-components-to-ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: dev

Migrate Affirm and Afterpay payment method components to TypeScript.
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import React from 'react';

/**
* Internal dependencies.
*/
import Detail from '../detail';
import { PaymentMethodDetails } from 'wcpay/payment-details/types';
import { Charge } from 'wcpay/types/charges';

/**
* Extracts and formats payment method details from a charge.
*
* @param {Object} charge The charge object.
* @return {Object} A flat hash of all necessary values.
* @param {Charge} charge The charge object.
* @return {PaymentMethodDetails} A flat hash of all necessary values.
*/
const formatPaymentMethodDetails = ( charge ) => {
const formatPaymentMethodDetails = ( charge: Charge ): PaymentMethodDetails => {
const { billing_details: billingDetails, payment_method: id } = charge;

const { name, email, formatted_address: formattedAddress } = billingDetails;

return {
Expand All @@ -32,20 +34,36 @@ const formatPaymentMethodDetails = ( charge ) => {
/**
* Placeholders to display while loading.
*/
const paymentMethodPlaceholders = {
const paymentMethodPlaceholders: PaymentMethodDetails = {
id: 'id placeholder',
name: 'name placeholder',
email: 'email placeholder',
formattedAddress: 'address placeholder',
};

const CardDetails = ( { charge = {}, isLoading } ) => {
const details =
/**
* Props interface for AffirmDetails component
*/
interface AffirmDetailsProps {
charge?: Charge;
isLoading: boolean;
}

const AffirmDetails: React.FC< AffirmDetailsProps > = ( {
charge,
isLoading,
} ) => {
const details: PaymentMethodDetails | typeof paymentMethodPlaceholders =
charge && charge.payment_method_details
? formatPaymentMethodDetails( charge )
: paymentMethodPlaceholders;

const { id, name, email, formattedAddress } = details;
const {
id,
name,
email,
formattedAddress,
}: PaymentMethodDetails | typeof paymentMethodPlaceholders = details;

return (
<div className="payment-method-details">
Expand Down Expand Up @@ -78,6 +96,7 @@ const CardDetails = ( { charge = {}, isLoading } ) => {
label={ __( 'Address', 'woocommerce-payments' ) }
>
<span
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={ {
__html: formattedAddress || '–',
} }
Expand All @@ -88,4 +107,4 @@ const CardDetails = ( { charge = {}, isLoading } ) => {
);
};

export default CardDetails;
export default AffirmDetails;
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import React from 'react';

/**
* Internal dependencies.
*/
import Detail from '../detail';
import { Charge } from 'wcpay/types/charges';
import { PaymentMethodDetails } from 'wcpay/payment-details/types';

/**
* Extracts and formats payment method details from a charge.
*
* @param {Object} charge The charge object.
* @return {Object} A flat hash of all necessary values.
* @param {Charge} charge The charge object.
* @return {PaymentMethodDetails} A flat hash of all necessary values.
*/
const formatPaymentMethodDetails = ( charge ) => {
const formatPaymentMethodDetails = ( charge: Charge ): PaymentMethodDetails => {
const { billing_details: billingDetails, payment_method: id } = charge;

const { name, email, formatted_address: formattedAddress } = billingDetails;

return {
Expand All @@ -30,22 +32,33 @@ const formatPaymentMethodDetails = ( charge ) => {
};

/**
* Placeholders to display while loading.
* Placeholder object for loading state
*/
const paymentMethodPlaceholders = {
const paymentMethodPlaceholders: PaymentMethodDetails = {
id: 'id placeholder',
name: 'name placeholder',
email: 'email placeholder',
formattedAddress: 'address placeholder',
};

const CardDetails = ( { charge = {}, isLoading } ) => {
const details =
/**
* Props interface for AfterpayClearpayDetailss component
*/
interface AfterpayClearpayDetailsProps {
charge?: Charge;
isLoading: boolean;
}

const AfterpayClearpayDetails: React.FC< AfterpayClearpayDetailsProps > = ( {
charge,
isLoading,
} ) => {
const details: PaymentMethodDetails =
charge && charge.payment_method_details
? formatPaymentMethodDetails( charge )
: paymentMethodPlaceholders;

const { id, name, email, formattedAddress } = details;
const { id, name, email, formattedAddress }: PaymentMethodDetails = details;

return (
<div className="payment-method-details">
Expand Down Expand Up @@ -77,6 +90,7 @@ const CardDetails = ( { charge = {}, isLoading } ) => {
label={ __( 'Address', 'woocommerce-payments' ) }
>
<span
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={ {
__html: formattedAddress || '–',
} }
Expand All @@ -87,4 +101,4 @@ const CardDetails = ( { charge = {}, isLoading } ) => {
);
};

export default CardDetails;
export default AfterpayClearpayDetails;
10 changes: 10 additions & 0 deletions client/payment-details/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ export const isPaymentIntent = (
export const isCharge = ( data: PaymentIntent | Charge ): data is Charge => {
return ( data as PaymentIntent ).charge === undefined;
};

/*
* Interface for PaymentMethodDetails used in the payment method components on transaction details page.
*/
export interface PaymentMethodDetails {
id: string;
name: null | string;
email: null | string;
formattedAddress?: string;
}

0 comments on commit 156221f

Please sign in to comment.