2020-02-17 11:12:15 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-03-13 12:02:08 +00:00
|
|
|
import { Button } from '@woocommerce/base-components/cart-checkout';
|
2020-02-17 11:12:15 +00:00
|
|
|
import { useState } from '@wordpress/element';
|
|
|
|
import isShallowEqual from '@wordpress/is-shallow-equal';
|
2020-03-17 11:45:33 +00:00
|
|
|
import { useValidationContext } from '@woocommerce/base-context';
|
2020-02-17 11:12:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2020-03-03 10:46:53 +00:00
|
|
|
import AddressForm from '../address-form';
|
2020-02-17 11:12:15 +00:00
|
|
|
|
2020-03-13 13:41:59 +00:00
|
|
|
const ShippingCalculatorAddress = ( {
|
|
|
|
address: initialAddress,
|
|
|
|
onUpdate,
|
|
|
|
addressFields,
|
|
|
|
} ) => {
|
2020-02-17 11:12:15 +00:00
|
|
|
const [ address, setAddress ] = useState( initialAddress );
|
2020-03-23 11:22:00 +00:00
|
|
|
const {
|
|
|
|
hasValidationErrors,
|
|
|
|
showAllValidationErrors,
|
|
|
|
} = useValidationContext();
|
|
|
|
|
|
|
|
const validateSubmit = () => {
|
|
|
|
showAllValidationErrors();
|
|
|
|
if ( hasValidationErrors() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Make all fields optional except 'country'.
|
|
|
|
const fieldConfig = {};
|
|
|
|
addressFields.forEach( ( field ) => {
|
|
|
|
if ( field === 'country' ) {
|
|
|
|
fieldConfig[ field ] = {
|
|
|
|
...fieldConfig[ field ],
|
|
|
|
errorMessage: __(
|
|
|
|
'Please select a country to calculate rates.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
required: true,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
fieldConfig[ field ] = {
|
|
|
|
...fieldConfig[ field ],
|
|
|
|
required: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} );
|
2020-02-17 11:12:15 +00:00
|
|
|
|
|
|
|
return (
|
2020-02-19 15:10:26 +00:00
|
|
|
<form className="wc-block-shipping-calculator-address">
|
2020-03-03 10:46:53 +00:00
|
|
|
<AddressForm
|
2020-03-13 13:41:59 +00:00
|
|
|
fields={ addressFields }
|
2020-03-23 11:22:00 +00:00
|
|
|
fieldConfig={ fieldConfig }
|
2020-03-03 10:46:53 +00:00
|
|
|
onChange={ setAddress }
|
|
|
|
values={ address }
|
2020-02-17 11:12:15 +00:00
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
className="wc-block-shipping-calculator-address__button"
|
2020-03-23 11:22:00 +00:00
|
|
|
disabled={ isShallowEqual( address, initialAddress ) }
|
2020-03-05 19:54:05 +00:00
|
|
|
onClick={ ( e ) => {
|
|
|
|
e.preventDefault();
|
2020-03-23 11:22:00 +00:00
|
|
|
const isAddressValid = validateSubmit();
|
|
|
|
if ( isAddressValid ) {
|
|
|
|
return onUpdate( address );
|
|
|
|
}
|
2020-03-05 19:54:05 +00:00
|
|
|
} }
|
2020-02-21 16:26:27 +00:00
|
|
|
type="submit"
|
2020-02-17 11:12:15 +00:00
|
|
|
>
|
|
|
|
{ __( 'Update', 'woo-gutenberg-products-block' ) }
|
|
|
|
</Button>
|
2020-02-19 15:10:26 +00:00
|
|
|
</form>
|
2020-02-17 11:12:15 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ShippingCalculatorAddress.propTypes = {
|
2020-03-13 13:41:59 +00:00
|
|
|
address: PropTypes.object.isRequired,
|
2020-02-17 11:12:15 +00:00
|
|
|
onUpdate: PropTypes.func.isRequired,
|
2020-03-13 13:41:59 +00:00
|
|
|
addressFields: PropTypes.array.isRequired,
|
2020-02-17 11:12:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ShippingCalculatorAddress;
|