Ensure shopper saved card is used as default payment method (default was being overwritten in some circumstances) (https://github.com/woocommerce/woocommerce-blocks/pull/3131)

* bail out of setting a default payment method if shopper has saved card

* ensure default payment method is only set as active if necessary:
- use functional form of setState; previous state (current payment
  method) is not potentially stale value
- so the default is only set if there really is no active payment method

* remove unnecessary remnant hook dependency 🧟‍♀️
This commit is contained in:
Rua Haszard 2020-09-11 08:48:23 +12:00 committed by GitHub
parent ceb83c2a03
commit e5c9243f73
1 changed files with 13 additions and 16 deletions

View File

@ -341,22 +341,19 @@ export const PaymentMethodDataProvider = ( { children } ) => {
return;
}
setActive( ( currentActivePaymentMethod ) => {
// If there's no active payment method, or the active payment method has
// been removed (e.g. COD vs shipping methods), set one as active.
if (
! activePaymentMethod ||
! paymentMethodKeys.includes( activePaymentMethod )
! currentActivePaymentMethod ||
! paymentMethodKeys.includes( currentActivePaymentMethod )
) {
setActivePaymentMethod(
Object.keys( paymentData.paymentMethods )[ 0 ]
);
dispatch( statusOnly( PRISTINE ) );
return Object.keys( paymentData.paymentMethods )[ 0 ];
}
}, [
activePaymentMethod,
paymentMethodsInitialized,
paymentData.paymentMethods,
setActivePaymentMethod,
] );
return currentActivePaymentMethod;
} );
}, [ paymentMethodsInitialized, paymentData.paymentMethods, setActive ] );
// emit events.
useEffect( () => {