Ensure email and phone are persisted through `setBillingData` (https://github.com/woocommerce/woocommerce-blocks/pull/2477)

* Force inclusion of email and phone

* Update deps

* update deps
This commit is contained in:
Mike Jolley 2020-05-13 15:39:26 +01:00 committed by GitHub
parent 9e4755ad04
commit 2395137790
2 changed files with 22 additions and 13 deletions

View File

@ -29,15 +29,12 @@ export const useBillingDataContext = () => {
export const BillingDataProvider = ( { children } ) => {
const [ billingData, setBillingDataState ] = useState( DEFAULT_STATE );
const setBillingData = useCallback(
( newData ) => {
setBillingDataState( ( prevState ) => ( {
...prevState,
...newData,
} ) );
},
[ setBillingDataState ]
);
const setBillingData = useCallback( ( newData ) => {
setBillingDataState( ( prevState ) => ( {
...prevState,
...newData,
} ) );
}, [] );
/**
* @type {BillingDataContext}

View File

@ -58,14 +58,26 @@ export const useCheckoutAddress = () => {
setShippingAddress( shippingFields );
}
const newBillingData = shippingAsBilling
? shippingFields
: billingFields;
// Uses shipping or billing fields depending on shippingAsBilling checkbox, but ensures
// billing only fields are also included.
const newBillingData = {
...( shippingAsBilling ? shippingFields : billingFields ),
email: billingFields.email,
phone: billingFields.phone,
};
if ( ! isEqual( newBillingData, billingData ) ) {
setBillingData( newBillingData );
}
}, [ shippingFields, billingFields, shippingAsBilling ] );
}, [
shippingFields,
billingFields,
shippingAsBilling,
billingData,
shippingAddress,
setBillingData,
setShippingAddress,
] );
/**
* Wrapper for updateBillingFields (from useState) which handles merging.