2020-02-19 17:14:41 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
|
|
|
import {
|
|
|
|
SHIPPING_COUNTRIES,
|
|
|
|
SHIPPING_STATES,
|
|
|
|
} from '@woocommerce/block-settings';
|
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows a formatted shipping location.
|
2020-09-20 23:54:08 +00:00
|
|
|
*
|
|
|
|
* @param {Object} props Incoming props for the component.
|
|
|
|
* @param {Object} props.address Incoming address information.
|
2020-02-19 17:14:41 +00:00
|
|
|
*/
|
|
|
|
const ShippingLocation = ( { address } ) => {
|
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;
|
|
|
|
}
|
2020-02-19 17:14:41 +00:00
|
|
|
const formattedCountry =
|
|
|
|
typeof SHIPPING_COUNTRIES[ address.country ] === 'string'
|
|
|
|
? decodeEntities( SHIPPING_COUNTRIES[ address.country ] )
|
|
|
|
: '';
|
|
|
|
|
|
|
|
const formattedState =
|
|
|
|
typeof SHIPPING_STATES[ address.country ] === 'object' &&
|
|
|
|
typeof SHIPPING_STATES[ address.country ][ address.state ] === 'string'
|
|
|
|
? decodeEntities(
|
|
|
|
SHIPPING_STATES[ address.country ][ address.state ]
|
|
|
|
)
|
|
|
|
: 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
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ShippingLocation.propTypes = {
|
|
|
|
address: PropTypes.shape( {
|
|
|
|
city: PropTypes.string,
|
|
|
|
state: PropTypes.string,
|
|
|
|
postcode: PropTypes.string,
|
|
|
|
country: PropTypes.string,
|
|
|
|
} ),
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ShippingLocation;
|