2021-06-16 12:44:40 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useShallowEqual } from '@woocommerce/base-hooks';
|
2021-09-15 16:36:02 +00:00
|
|
|
import type {
|
|
|
|
PaymentMethods,
|
|
|
|
ExpressPaymentMethods,
|
|
|
|
} from '@woocommerce/type-defs/payments';
|
2021-06-16 12:44:40 +00:00
|
|
|
|
2020-01-06 22:28:09 +00:00
|
|
|
/**
|
2021-04-08 12:31:12 +00:00
|
|
|
* Internal dependencies
|
2020-01-06 22:28:09 +00:00
|
|
|
*/
|
2021-04-08 12:31:12 +00:00
|
|
|
import { usePaymentMethodDataContext } from '../../providers/cart-checkout/payment-methods';
|
2020-01-06 22:28:09 +00:00
|
|
|
|
2021-09-15 16:36:02 +00:00
|
|
|
interface PaymentMethodState {
|
|
|
|
paymentMethods: PaymentMethods;
|
|
|
|
isInitialized: boolean;
|
|
|
|
}
|
|
|
|
interface ExpressPaymentMethodState {
|
|
|
|
paymentMethods: ExpressPaymentMethods;
|
|
|
|
isInitialized: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const usePaymentMethodState = (
|
|
|
|
express = false
|
|
|
|
): PaymentMethodState | ExpressPaymentMethodState => {
|
2020-03-26 11:11:46 +00:00
|
|
|
const {
|
|
|
|
paymentMethods,
|
|
|
|
expressPaymentMethods,
|
|
|
|
paymentMethodsInitialized,
|
|
|
|
expressPaymentMethodsInitialized,
|
|
|
|
} = usePaymentMethodDataContext();
|
2021-06-16 12:44:40 +00:00
|
|
|
|
|
|
|
const currentPaymentMethods = useShallowEqual( paymentMethods );
|
|
|
|
const currentExpressPaymentMethods = useShallowEqual(
|
|
|
|
expressPaymentMethods
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
paymentMethods: express
|
|
|
|
? currentExpressPaymentMethods
|
|
|
|
: currentPaymentMethods,
|
|
|
|
isInitialized: express
|
|
|
|
? expressPaymentMethodsInitialized
|
|
|
|
: paymentMethodsInitialized,
|
|
|
|
};
|
2020-01-06 22:28:09 +00:00
|
|
|
};
|
|
|
|
|
2021-09-15 16:36:02 +00:00
|
|
|
export const usePaymentMethods = ():
|
|
|
|
| PaymentMethodState
|
|
|
|
| ExpressPaymentMethodState => usePaymentMethodState( false );
|
|
|
|
export const useExpressPaymentMethods = (): ExpressPaymentMethodState =>
|
|
|
|
usePaymentMethodState( true );
|