2020-01-06 22:28:09 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-10-05 11:59:20 +00:00
|
|
|
import { usePaymentMethods } from '@woocommerce/base-hooks';
|
2020-10-12 12:43:52 +00:00
|
|
|
import { useCallback, useState } from '@wordpress/element';
|
2020-01-06 22:28:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-03-26 11:11:46 +00:00
|
|
|
import NoPaymentMethods from './no-payment-methods';
|
2020-10-05 11:59:20 +00:00
|
|
|
import PaymentMethodOptions from './payment-method-options';
|
2020-03-26 11:11:46 +00:00
|
|
|
import SavedPaymentMethodOptions from './saved-payment-method-options';
|
2020-03-10 16:35:30 +00:00
|
|
|
|
2020-03-20 16:48:11 +00:00
|
|
|
/**
|
|
|
|
* PaymentMethods component.
|
|
|
|
*
|
|
|
|
* @return {*} The rendered component.
|
|
|
|
*/
|
2020-01-06 22:28:09 +00:00
|
|
|
const PaymentMethods = () => {
|
|
|
|
const { isInitialized, paymentMethods } = usePaymentMethods();
|
2020-10-12 12:43:52 +00:00
|
|
|
const [ showNewPaymentMethods, setShowNewPaymentMethods ] = useState(
|
|
|
|
true
|
|
|
|
);
|
|
|
|
const onChange = useCallback(
|
|
|
|
( token ) => {
|
|
|
|
setShowNewPaymentMethods( token === '0' );
|
|
|
|
},
|
|
|
|
[ setShowNewPaymentMethods ]
|
|
|
|
);
|
2020-03-10 16:35:30 +00:00
|
|
|
|
2020-10-05 11:59:20 +00:00
|
|
|
if ( isInitialized && Object.keys( paymentMethods ).length === 0 ) {
|
2020-03-20 16:48:11 +00:00
|
|
|
return <NoPaymentMethods />;
|
2020-01-06 22:28:09 +00:00
|
|
|
}
|
2020-03-26 11:11:46 +00:00
|
|
|
|
2020-10-12 12:43:52 +00:00
|
|
|
return (
|
2020-03-26 11:11:46 +00:00
|
|
|
<>
|
2020-10-12 12:43:52 +00:00
|
|
|
<SavedPaymentMethodOptions onChange={ onChange } />
|
|
|
|
{ showNewPaymentMethods && <PaymentMethodOptions /> }
|
2020-03-26 11:11:46 +00:00
|
|
|
</>
|
|
|
|
);
|
2020-01-06 22:28:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default PaymentMethods;
|