2023-10-09 11:49:09 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { ALLOWED_COUNTRIES } from '@woocommerce/block-settings';
|
|
|
|
import type {
|
|
|
|
CartShippingAddress,
|
|
|
|
CartBillingAddress,
|
|
|
|
} from '@woocommerce/types';
|
2023-11-14 18:30:23 +00:00
|
|
|
import { AddressFields, AddressField } from '@woocommerce/settings';
|
2023-10-09 11:49:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
const AddressCard = ( {
|
|
|
|
address,
|
|
|
|
onEdit,
|
|
|
|
target,
|
2023-11-14 18:30:23 +00:00
|
|
|
fieldConfig,
|
2023-10-09 11:49:09 +00:00
|
|
|
}: {
|
|
|
|
address: CartShippingAddress | CartBillingAddress;
|
|
|
|
onEdit: () => void;
|
|
|
|
target: string;
|
2023-11-14 18:30:23 +00:00
|
|
|
fieldConfig: Record< keyof AddressFields, Partial< AddressField > >;
|
2023-10-09 11:49:09 +00:00
|
|
|
} ): JSX.Element | null => {
|
|
|
|
return (
|
|
|
|
<div className="wc-block-components-address-card">
|
|
|
|
<address>
|
|
|
|
<span className="wc-block-components-address-card__address-section">
|
|
|
|
{ address.first_name + ' ' + address.last_name }
|
|
|
|
</span>
|
|
|
|
<div className="wc-block-components-address-card__address-section">
|
|
|
|
{ [
|
|
|
|
address.address_1,
|
2023-11-14 18:30:23 +00:00
|
|
|
! fieldConfig.address_2.hidden && address.address_2,
|
2023-10-09 11:49:09 +00:00
|
|
|
address.city,
|
|
|
|
address.state,
|
|
|
|
address.postcode,
|
|
|
|
ALLOWED_COUNTRIES[ address.country ]
|
|
|
|
? ALLOWED_COUNTRIES[ address.country ]
|
|
|
|
: address.country,
|
|
|
|
]
|
|
|
|
.filter( ( field ) => !! field )
|
|
|
|
.map( ( field, index ) => (
|
|
|
|
<span key={ `address-` + index }>{ field }</span>
|
|
|
|
) ) }
|
|
|
|
</div>
|
2023-11-14 18:30:23 +00:00
|
|
|
{ address.phone && ! fieldConfig.phone.hidden ? (
|
2023-10-09 11:49:09 +00:00
|
|
|
<div
|
|
|
|
key={ `address-phone` }
|
|
|
|
className="wc-block-components-address-card__address-section"
|
|
|
|
>
|
|
|
|
{ address.phone }
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
''
|
|
|
|
) }
|
|
|
|
</address>
|
|
|
|
{ onEdit && (
|
|
|
|
<a
|
|
|
|
role="button"
|
|
|
|
href={ '#' + target }
|
|
|
|
className="wc-block-components-address-card__edit"
|
|
|
|
aria-label={ __(
|
|
|
|
'Edit address',
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2023-10-09 11:49:09 +00:00
|
|
|
) }
|
|
|
|
onClick={ ( e ) => {
|
|
|
|
onEdit();
|
|
|
|
e.preventDefault();
|
|
|
|
} }
|
|
|
|
>
|
2023-12-12 22:12:36 +00:00
|
|
|
{ __( 'Edit', 'woocommerce' ) }
|
2023-10-09 11:49:09 +00:00
|
|
|
</a>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AddressCard;
|