mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 12:25:15 +08:00
Move Card markup to a separate component
This commit is contained in:
parent
522b27aea0
commit
db449b5d70
4 changed files with 112 additions and 76 deletions
60
modules/ppcp-axo-block/resources/js/components/CreditCard.js
Normal file
60
modules/ppcp-axo-block/resources/js/components/CreditCard.js
Normal file
|
@ -0,0 +1,60 @@
|
|||
import { useMemo } from '@wordpress/element';
|
||||
import { FastlaneWatermark } from './FastlaneWatermark';
|
||||
|
||||
const cardIcons = {
|
||||
VISA: 'visa-light.svg',
|
||||
MASTER_CARD: 'mastercard-light.svg',
|
||||
AMEX: 'amex-light.svg',
|
||||
DISCOVER: 'discover-light.svg',
|
||||
DINERS: 'dinersclub-light.svg',
|
||||
JCB: 'jcb-light.svg',
|
||||
UNIONPAY: 'unionpay-light.svg',
|
||||
};
|
||||
|
||||
export const CreditCard = ( { card, shippingAddress, fastlaneSdk } ) => {
|
||||
const { brand, lastDigits, expiry } = card?.paymentSource?.card ?? {};
|
||||
const { fullName } = shippingAddress?.name ?? {};
|
||||
|
||||
const cardLogo = useMemo( () => {
|
||||
return cardIcons[ brand ] ? (
|
||||
<img
|
||||
className="wc-block-axo-block-card__meta-icon"
|
||||
title={ brand }
|
||||
src={ `${ window.wc_ppcp_axo.icons_directory }${ cardIcons[ brand ] }` }
|
||||
alt={ brand }
|
||||
/>
|
||||
) : (
|
||||
<span>{ brand }</span>
|
||||
);
|
||||
}, [ brand ] );
|
||||
|
||||
const formattedExpiry = expiry
|
||||
? `${ expiry.split( '-' )[ 1 ] }/${ expiry.split( '-' )[ 0 ] }`
|
||||
: '';
|
||||
|
||||
return (
|
||||
<div className="wc-block-checkout-axo-block-card">
|
||||
<div className="wc-block-checkout-axo-block-card__inner">
|
||||
<div className="wc-block-checkout-axo-block-card__content">
|
||||
<div className="wc-block-checkout-axo-block-card__meta">
|
||||
<span className="wc-block-checkout-axo-block-card__meta-digits">
|
||||
{ `**** **** **** ${ lastDigits }` }
|
||||
</span>
|
||||
{ cardLogo }
|
||||
</div>
|
||||
<div className="wc-block-checkout-axo-block-card__meta">
|
||||
<span>{ fullName }</span>
|
||||
<span>{ formattedExpiry }</span>{ ' ' }
|
||||
</div>
|
||||
</div>
|
||||
<div className="wc-block-checkout-axo-block-card__watermark">
|
||||
<FastlaneWatermark
|
||||
fastlaneSdk={ fastlaneSdk }
|
||||
name="wc-block-checkout-axo-card-watermark"
|
||||
includeAdditionalInfo={ false }
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
|
@ -1,14 +1,5 @@
|
|||
import { useEffect, useCallback, useMemo } from '@wordpress/element';
|
||||
|
||||
const cardIcons = {
|
||||
VISA: 'visa-light.svg',
|
||||
MASTER_CARD: 'mastercard-light.svg',
|
||||
AMEX: 'amex-light.svg',
|
||||
DISCOVER: 'discover-light.svg',
|
||||
DINERS: 'dinersclub-light.svg',
|
||||
JCB: 'jcb-light.svg',
|
||||
UNIONPAY: 'unionpay-light.svg',
|
||||
};
|
||||
import { useEffect, useCallback } from '@wordpress/element';
|
||||
import { CreditCard } from './CreditCard';
|
||||
|
||||
export const Payment = ( {
|
||||
fastlaneSdk,
|
||||
|
@ -17,9 +8,6 @@ export const Payment = ( {
|
|||
isGuest,
|
||||
onPaymentLoad,
|
||||
} ) => {
|
||||
const { brand, lastDigits, expiry } = card?.paymentSource?.card ?? {};
|
||||
const { fullName } = shippingAddress?.name ?? {};
|
||||
|
||||
// Memoized Fastlane card rendering
|
||||
const loadPaymentComponent = useCallback( async () => {
|
||||
if ( isGuest ) {
|
||||
|
@ -35,40 +23,14 @@ export const Payment = ( {
|
|||
loadPaymentComponent();
|
||||
}, [ loadPaymentComponent ] );
|
||||
|
||||
// Memoized card logo rendering
|
||||
const cardLogo = useMemo( () => {
|
||||
return cardIcons[ brand ] ? (
|
||||
<img
|
||||
className="wc-block-axo-block-card__meta-icon"
|
||||
title={ brand }
|
||||
src={ `${ window.wc_ppcp_axo.icons_directory }${ cardIcons[ brand ] }` }
|
||||
alt={ brand }
|
||||
/>
|
||||
) : (
|
||||
<span>{ brand }</span>
|
||||
);
|
||||
}, [ brand ] );
|
||||
|
||||
const formattedExpiry = expiry
|
||||
? `${ expiry.split( '-' )[ 1 ] }/${ expiry.split( '-' )[ 0 ] }`
|
||||
: '';
|
||||
|
||||
return isGuest ? (
|
||||
<div id="fastlane-card" key="fastlane-card" />
|
||||
) : (
|
||||
<div key="custom-card" className="wc-block-checkout-axo-block-card">
|
||||
<div className="wc-block-checkout-axo-block-card__meta-container">
|
||||
<div className="wc-block-axo-block-card__meta">
|
||||
<span className="wc-block-axo-block-card__meta__digits">
|
||||
{ `**** **** **** ${ lastDigits }` }
|
||||
</span>
|
||||
{ cardLogo }
|
||||
</div>
|
||||
<div className="wc-block-axo-block-card__meta">
|
||||
<span>{ fullName }</span>
|
||||
<span>{ formattedExpiry }</span>{ ' ' }
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<CreditCard
|
||||
key="custom-card"
|
||||
card={ card }
|
||||
shippingAddress={ shippingAddress }
|
||||
fastlaneSdk={ fastlaneSdk }
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue