2020-10-05 11:59:20 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
usePaymentMethods,
|
|
|
|
usePaymentMethodInterface,
|
|
|
|
useStoreNotices,
|
|
|
|
useEmitResponse,
|
|
|
|
} from '@woocommerce/base-hooks';
|
|
|
|
import { cloneElement } from '@wordpress/element';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import {
|
|
|
|
useEditorContext,
|
|
|
|
usePaymentMethodDataContext,
|
|
|
|
} from '@woocommerce/base-context';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import Tabs from '../tabs';
|
|
|
|
import PaymentMethodTab from './payment-method-tab';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component used to render all non-saved payment method options.
|
|
|
|
*
|
|
|
|
* @return {*} The rendered component.
|
|
|
|
*/
|
|
|
|
const PaymentMethodOptions = () => {
|
2020-11-20 17:45:12 +00:00
|
|
|
const {
|
|
|
|
setActivePaymentMethod,
|
|
|
|
expressPaymentMethods,
|
|
|
|
} = usePaymentMethodDataContext();
|
2020-10-05 11:59:20 +00:00
|
|
|
const { paymentMethods } = usePaymentMethods();
|
|
|
|
const {
|
|
|
|
activePaymentMethod,
|
|
|
|
...paymentMethodInterface
|
|
|
|
} = usePaymentMethodInterface();
|
2020-11-20 17:45:12 +00:00
|
|
|
const expressPaymentMethodActive = Object.keys(
|
|
|
|
expressPaymentMethods
|
|
|
|
).includes( activePaymentMethod );
|
2020-10-05 11:59:20 +00:00
|
|
|
const { noticeContexts } = useEmitResponse();
|
|
|
|
const { removeNotice } = useStoreNotices();
|
|
|
|
const { isEditor } = useEditorContext();
|
|
|
|
|
2020-11-20 17:45:12 +00:00
|
|
|
return expressPaymentMethodActive ? null : (
|
2020-10-05 11:59:20 +00:00
|
|
|
<Tabs
|
|
|
|
className="wc-block-components-checkout-payment-methods"
|
|
|
|
onSelect={ ( tabName ) => {
|
|
|
|
setActivePaymentMethod( tabName );
|
|
|
|
removeNotice( 'wc-payment-error', noticeContexts.PAYMENTS );
|
|
|
|
} }
|
|
|
|
tabs={ Object.keys( paymentMethods ).map( ( name ) => {
|
|
|
|
const {
|
|
|
|
ariaLabel,
|
|
|
|
edit,
|
|
|
|
content,
|
|
|
|
label,
|
|
|
|
supports,
|
|
|
|
} = paymentMethods[ name ];
|
|
|
|
const component = isEditor ? edit : content;
|
|
|
|
return {
|
|
|
|
name,
|
|
|
|
title:
|
|
|
|
typeof label === 'string'
|
|
|
|
? label
|
|
|
|
: cloneElement( label, {
|
|
|
|
components:
|
|
|
|
paymentMethodInterface.components,
|
|
|
|
} ),
|
|
|
|
ariaLabel,
|
|
|
|
content: (
|
|
|
|
<PaymentMethodTab
|
2021-01-24 13:59:13 +00:00
|
|
|
showSaveOption={ supports.showSaveOption }
|
2020-10-05 11:59:20 +00:00
|
|
|
>
|
|
|
|
{ cloneElement( component, {
|
|
|
|
activePaymentMethod,
|
|
|
|
...paymentMethodInterface,
|
|
|
|
} ) }
|
|
|
|
</PaymentMethodTab>
|
|
|
|
),
|
|
|
|
};
|
|
|
|
} ) }
|
|
|
|
initialTabName={ activePaymentMethod }
|
|
|
|
ariaLabel={ __(
|
|
|
|
'Payment Methods',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
id="wc-block-payment-methods"
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PaymentMethodOptions;
|