2023-10-09 11:49:09 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2024-03-22 14:14:14 +00:00
|
|
|
import { ALLOWED_COUNTRIES, ALLOWED_STATES } from '@woocommerce/block-settings';
|
|
|
|
import {
|
|
|
|
type CartShippingAddress,
|
|
|
|
type CartBillingAddress,
|
|
|
|
isObject,
|
|
|
|
isString,
|
2023-10-09 11:49:09 +00:00
|
|
|
} from '@woocommerce/types';
|
2024-01-05 17:35:28 +00:00
|
|
|
import { FormFieldsConfig } from '@woocommerce/settings';
|
2024-03-22 14:14:14 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
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;
|
2024-01-05 17:35:28 +00:00
|
|
|
fieldConfig: FormFieldsConfig;
|
2023-10-09 11:49:09 +00:00
|
|
|
} ): JSX.Element | null => {
|
2024-03-22 14:14:14 +00:00
|
|
|
const formattedCountry = isString( ALLOWED_COUNTRIES[ address.country ] )
|
|
|
|
? decodeEntities( ALLOWED_COUNTRIES[ address.country ] )
|
|
|
|
: '';
|
|
|
|
|
|
|
|
const formattedState =
|
|
|
|
isObject( ALLOWED_STATES[ address.country ] ) &&
|
|
|
|
isString( ALLOWED_STATES[ address.country ][ address.state ] )
|
|
|
|
? decodeEntities(
|
|
|
|
ALLOWED_STATES[ address.country ][ address.state ]
|
|
|
|
)
|
|
|
|
: address.state;
|
|
|
|
|
2023-10-09 11:49:09 +00:00
|
|
|
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,
|
2024-03-22 14:14:14 +00:00
|
|
|
formattedState,
|
2023-10-09 11:49:09 +00:00
|
|
|
address.postcode,
|
2024-03-22 14:14:14 +00:00
|
|
|
formattedCountry,
|
2023-10-09 11:49:09 +00:00
|
|
|
]
|
|
|
|
.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"
|
2023-12-12 23:05:20 +00:00
|
|
|
aria-label={ __( 'Edit address', '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;
|