2020-01-06 22:28:09 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
usePaymentMethods,
|
2020-03-11 10:50:12 +00:00
|
|
|
usePaymentMethodInterface,
|
2020-01-06 22:28:09 +00:00
|
|
|
} from '@woocommerce/base-hooks';
|
2020-03-10 16:35:30 +00:00
|
|
|
import {
|
|
|
|
useCallback,
|
|
|
|
cloneElement,
|
|
|
|
useRef,
|
|
|
|
useEffect,
|
|
|
|
} from '@wordpress/element';
|
2020-01-06 22:28:09 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-03-10 16:35:30 +00:00
|
|
|
import { useCheckoutContext } from '@woocommerce/base-context';
|
2020-01-06 22:28:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import Tabs from '../tabs';
|
|
|
|
|
|
|
|
const noPaymentMethodTab = () => {
|
|
|
|
const label = __( 'Not Existing', 'woo-gutenberg-products-block' );
|
|
|
|
return {
|
|
|
|
name: label,
|
|
|
|
label,
|
|
|
|
title: () => label,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const createTabs = ( paymentMethods ) => {
|
2020-03-11 10:50:12 +00:00
|
|
|
const paymentMethodIds = Object.keys( paymentMethods );
|
|
|
|
return paymentMethodIds.length > 0
|
|
|
|
? paymentMethodIds.map( ( id ) => {
|
|
|
|
const { label, ariaLabel } = paymentMethods[ id ];
|
2020-01-06 22:28:09 +00:00
|
|
|
return {
|
2020-03-11 10:50:12 +00:00
|
|
|
name: id,
|
2020-01-06 22:28:09 +00:00
|
|
|
title: () => label,
|
|
|
|
ariaLabel,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
: [ noPaymentMethodTab() ];
|
|
|
|
};
|
|
|
|
|
2020-03-10 16:35:30 +00:00
|
|
|
/**
|
|
|
|
* Returns a payment method for the given context.
|
|
|
|
*
|
|
|
|
* @param {string} id The payment method slug to return.
|
|
|
|
* @param {Object} paymentMethods The current registered payment methods
|
|
|
|
* @param {boolean} isEditor Whether in the editor context (true) or not (false).
|
|
|
|
*
|
|
|
|
* @return {Object} The payment method matching the id for the given context.
|
|
|
|
*/
|
|
|
|
const getPaymentMethod = ( id, paymentMethods, isEditor ) => {
|
|
|
|
let paymentMethod = paymentMethods[ id ] || null;
|
|
|
|
if ( paymentMethod ) {
|
|
|
|
paymentMethod = isEditor
|
|
|
|
? paymentMethod.edit
|
|
|
|
: paymentMethod.activeContent;
|
|
|
|
}
|
|
|
|
return paymentMethod;
|
|
|
|
};
|
|
|
|
|
2020-01-06 22:28:09 +00:00
|
|
|
const PaymentMethods = () => {
|
2020-03-10 16:35:30 +00:00
|
|
|
const { isEditor } = useCheckoutContext();
|
2020-01-06 22:28:09 +00:00
|
|
|
const { isInitialized, paymentMethods } = usePaymentMethods();
|
2020-03-10 16:35:30 +00:00
|
|
|
const currentPaymentMethods = useRef( paymentMethods );
|
2020-03-11 10:50:12 +00:00
|
|
|
const {
|
|
|
|
activePaymentMethod,
|
|
|
|
setActivePaymentMethod,
|
|
|
|
...paymentMethodInterface
|
|
|
|
} = usePaymentMethodInterface();
|
|
|
|
const currentPaymentMethodInterface = useRef( paymentMethodInterface );
|
2020-03-10 16:35:30 +00:00
|
|
|
|
|
|
|
// update ref on changes
|
|
|
|
useEffect( () => {
|
|
|
|
currentPaymentMethods.current = paymentMethods;
|
2020-03-11 10:50:12 +00:00
|
|
|
currentPaymentMethodInterface.current = paymentMethodInterface;
|
|
|
|
}, [ paymentMethods, paymentMethodInterface, activePaymentMethod ] );
|
2020-01-06 22:28:09 +00:00
|
|
|
const getRenderedTab = useCallback(
|
|
|
|
() => ( selectedTab ) => {
|
2020-03-10 16:35:30 +00:00
|
|
|
const paymentMethod = getPaymentMethod(
|
|
|
|
selectedTab,
|
|
|
|
currentPaymentMethods.current,
|
|
|
|
isEditor
|
|
|
|
);
|
2020-01-06 22:28:09 +00:00
|
|
|
return paymentMethod
|
|
|
|
? cloneElement( paymentMethod, {
|
2020-03-11 10:50:12 +00:00
|
|
|
activePaymentMethod: paymentMethod.id,
|
|
|
|
...currentPaymentMethodInterface.current,
|
2020-01-06 22:28:09 +00:00
|
|
|
} )
|
|
|
|
: null;
|
|
|
|
},
|
2020-03-11 10:50:12 +00:00
|
|
|
[ isEditor, activePaymentMethod ]
|
2020-01-06 22:28:09 +00:00
|
|
|
);
|
|
|
|
if (
|
|
|
|
! isInitialized ||
|
|
|
|
( Object.keys( paymentMethods ).length === 0 && isInitialized )
|
|
|
|
) {
|
|
|
|
// @todo this can be a placeholder informing the user there are no
|
|
|
|
// payment methods setup?
|
|
|
|
return <div>No Payment Methods Initialized</div>;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Tabs
|
|
|
|
className="wc-component__payment-method-options"
|
|
|
|
onSelect={ ( tabName ) => setActivePaymentMethod( tabName ) }
|
|
|
|
tabs={ createTabs( paymentMethods ) }
|
|
|
|
initialTabName={ activePaymentMethod }
|
|
|
|
ariaLabel={ __(
|
|
|
|
'Payment Methods',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
{ getRenderedTab() }
|
|
|
|
</Tabs>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PaymentMethods;
|