2020-02-19 17:14:41 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2022-10-06 14:48:52 +00:00
|
|
|
import { ShippingAddress, getSetting } from '@woocommerce/settings';
|
2020-02-19 17:14:41 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
|
|
|
|
2021-05-10 09:03:30 +00:00
|
|
|
interface ShippingLocationProps {
|
2022-10-06 14:48:52 +00:00
|
|
|
address: ShippingAddress;
|
2021-05-10 09:03:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 17:14:41 +00:00
|
|
|
/**
|
|
|
|
* Shows a formatted shipping location.
|
2020-09-20 23:54:08 +00:00
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {Object} props Incoming props for the component.
|
2020-09-20 23:54:08 +00:00
|
|
|
* @param {Object} props.address Incoming address information.
|
2020-02-19 17:14:41 +00:00
|
|
|
*/
|
2021-05-10 09:03:30 +00:00
|
|
|
const ShippingLocation = ( {
|
|
|
|
address,
|
|
|
|
}: ShippingLocationProps ): JSX.Element | null => {
|
2020-03-05 19:54:05 +00:00
|
|
|
// we bail early if we don't have an address.
|
|
|
|
if ( Object.values( address ).length === 0 ) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-05-10 09:03:30 +00:00
|
|
|
const shippingCountries = getSetting( 'shippingCountries', {} ) as Record<
|
|
|
|
string,
|
|
|
|
string
|
|
|
|
>;
|
|
|
|
const shippingStates = getSetting( 'shippingStates', {} ) as Record<
|
|
|
|
string,
|
|
|
|
Record< string, string >
|
|
|
|
>;
|
2020-02-19 17:14:41 +00:00
|
|
|
const formattedCountry =
|
2021-04-22 11:37:27 +00:00
|
|
|
typeof shippingCountries[ address.country ] === 'string'
|
|
|
|
? decodeEntities( shippingCountries[ address.country ] )
|
2020-02-19 17:14:41 +00:00
|
|
|
: '';
|
|
|
|
|
|
|
|
const formattedState =
|
2021-04-22 11:37:27 +00:00
|
|
|
typeof shippingStates[ address.country ] === 'object' &&
|
|
|
|
typeof shippingStates[ address.country ][ address.state ] === 'string'
|
2020-02-19 17:14:41 +00:00
|
|
|
? decodeEntities(
|
2021-04-22 11:37:27 +00:00
|
|
|
shippingStates[ address.country ][ address.state ]
|
2020-02-19 17:14:41 +00:00
|
|
|
)
|
|
|
|
: address.state;
|
|
|
|
|
|
|
|
const addressParts = [];
|
|
|
|
|
|
|
|
addressParts.push( address.postcode.toUpperCase() );
|
|
|
|
addressParts.push( address.city );
|
|
|
|
addressParts.push( formattedState );
|
|
|
|
addressParts.push( formattedCountry );
|
|
|
|
|
|
|
|
const formattedLocation = addressParts.filter( Boolean ).join( ', ' );
|
|
|
|
|
2021-03-16 09:57:04 +00:00
|
|
|
if ( ! formattedLocation ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-02-19 17:14:41 +00:00
|
|
|
return (
|
2021-03-16 09:57:04 +00:00
|
|
|
<span className="wc-block-components-shipping-address">
|
|
|
|
{ sprintf(
|
|
|
|
/* translators: %s location. */
|
|
|
|
__( 'Shipping to %s', 'woo-gutenberg-products-block' ),
|
|
|
|
formattedLocation
|
|
|
|
) + ' ' }
|
|
|
|
</span>
|
2020-02-19 17:14:41 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ShippingLocation;
|