2021-04-23 09:42:36 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { defaultAddressFields } from '@woocommerce/settings';
|
|
|
|
import prepareAddressFields from '@woocommerce/base-components/cart-checkout/address-form/prepare-address-fields';
|
|
|
|
|
2020-03-10 10:55:19 +00:00
|
|
|
/**
|
|
|
|
* pluckAddress takes a full address object and returns relevant fields for calculating
|
|
|
|
* shipping, so we can track when one of them change to update rates.
|
|
|
|
*
|
|
|
|
* @param {Object} address An object containing all address information
|
2020-09-20 23:54:08 +00:00
|
|
|
* @param {string} address.country The country.
|
|
|
|
* @param {string} address.state The state.
|
|
|
|
* @param {string} address.city The city.
|
|
|
|
* @param {string} address.postcode The postal code.
|
2020-03-10 10:55:19 +00:00
|
|
|
*
|
|
|
|
* @return {Object} pluckedAddress An object containing shipping address that are needed to fetch an address.
|
|
|
|
*/
|
2020-11-20 15:13:35 +00:00
|
|
|
export const pluckAddress = ( {
|
|
|
|
country = '',
|
|
|
|
state = '',
|
|
|
|
city = '',
|
|
|
|
postcode = '',
|
|
|
|
} ) => ( {
|
2020-12-17 14:52:44 +00:00
|
|
|
country: country.trim(),
|
|
|
|
state: state.trim(),
|
|
|
|
city: city.trim(),
|
2020-11-20 15:13:35 +00:00
|
|
|
postcode: postcode ? postcode.replace( ' ', '' ).toUpperCase() : '',
|
2020-03-10 10:55:19 +00:00
|
|
|
} );
|
2021-04-23 09:42:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets fields to an empty string in an address if they are hidden by the settings in countryLocale.
|
|
|
|
*
|
|
|
|
* @param {Object} address The address to empty fields from.
|
|
|
|
* @return {Object} The address with hidden fields values removed.
|
|
|
|
*/
|
|
|
|
export const emptyHiddenAddressFields = ( address ) => {
|
|
|
|
const fields = Object.keys( defaultAddressFields );
|
|
|
|
const addressFields = prepareAddressFields( fields, {}, address.country );
|
|
|
|
const newAddress = Object.assign( {}, address );
|
|
|
|
addressFields.forEach( ( field ) => {
|
|
|
|
if ( field.hidden ) {
|
|
|
|
newAddress[ field.key ] = '';
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
return newAddress;
|
|
|
|
};
|