2020-01-06 22:28:09 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
useExpressPaymentMethods,
|
2020-03-11 10:50:12 +00:00
|
|
|
usePaymentMethodInterface,
|
2020-01-06 22:28:09 +00:00
|
|
|
} from '@woocommerce/base-hooks';
|
2020-04-09 15:22:34 +00:00
|
|
|
import {
|
|
|
|
cloneElement,
|
|
|
|
isValidElement,
|
|
|
|
useCallback,
|
|
|
|
useRef,
|
|
|
|
} from '@wordpress/element';
|
|
|
|
import {
|
|
|
|
useEditorContext,
|
|
|
|
usePaymentMethodDataContext,
|
|
|
|
} from '@woocommerce/base-context';
|
2020-01-06 22:28:09 +00:00
|
|
|
|
|
|
|
const ExpressPaymentMethods = () => {
|
2020-04-01 09:27:53 +00:00
|
|
|
const { isEditor } = useEditorContext();
|
2020-04-09 15:22:34 +00:00
|
|
|
const {
|
|
|
|
setActivePaymentMethod,
|
|
|
|
activePaymentMethod,
|
|
|
|
setPaymentStatus,
|
|
|
|
} = usePaymentMethodDataContext();
|
2020-03-11 10:50:12 +00:00
|
|
|
const paymentMethodInterface = usePaymentMethodInterface();
|
2020-01-06 22:28:09 +00:00
|
|
|
const { paymentMethods } = useExpressPaymentMethods();
|
2020-04-09 15:22:34 +00:00
|
|
|
const previousActivePaymentMethod = useRef( activePaymentMethod );
|
|
|
|
|
|
|
|
const onExpressPaymentClick = useCallback(
|
|
|
|
( paymentMethodId ) => () => {
|
|
|
|
previousActivePaymentMethod.current = activePaymentMethod;
|
|
|
|
setPaymentStatus().started();
|
|
|
|
setActivePaymentMethod( paymentMethodId );
|
|
|
|
},
|
|
|
|
[ setActivePaymentMethod, setPaymentStatus, activePaymentMethod ]
|
|
|
|
);
|
|
|
|
const onExpressPaymentClose = useCallback( () => {
|
|
|
|
setActivePaymentMethod( previousActivePaymentMethod.current );
|
|
|
|
}, [ setActivePaymentMethod ] );
|
2020-03-11 10:50:12 +00:00
|
|
|
const paymentMethodIds = Object.keys( paymentMethods );
|
2020-01-06 22:28:09 +00:00
|
|
|
const content =
|
2020-03-11 10:50:12 +00:00
|
|
|
paymentMethodIds.length > 0 ? (
|
|
|
|
paymentMethodIds.map( ( id ) => {
|
2020-03-10 16:35:30 +00:00
|
|
|
const expressPaymentMethod = isEditor
|
2020-03-11 10:50:12 +00:00
|
|
|
? paymentMethods[ id ].edit
|
2020-03-30 12:07:49 +00:00
|
|
|
: paymentMethods[ id ].content;
|
2020-03-10 16:35:30 +00:00
|
|
|
return isValidElement( expressPaymentMethod ) ? (
|
2020-03-11 10:50:12 +00:00
|
|
|
<li key={ id } id={ `express-payment-method-${ id }` }>
|
2020-01-06 22:28:09 +00:00
|
|
|
{ cloneElement( expressPaymentMethod, {
|
2020-03-11 10:50:12 +00:00
|
|
|
...paymentMethodInterface,
|
2020-04-09 15:22:34 +00:00
|
|
|
onClick: onExpressPaymentClick( id ),
|
|
|
|
onClose: onExpressPaymentClose,
|
2020-01-06 22:28:09 +00:00
|
|
|
} ) }
|
|
|
|
</li>
|
2020-03-10 16:35:30 +00:00
|
|
|
) : null;
|
2020-01-06 22:28:09 +00:00
|
|
|
} )
|
|
|
|
) : (
|
|
|
|
<li key="noneRegistered">No registered Payment Methods</li>
|
|
|
|
);
|
|
|
|
return (
|
2020-06-05 12:18:16 +00:00
|
|
|
<ul className="wc-block-components-express-checkout-payment-event-buttons">
|
2020-01-06 22:28:09 +00:00
|
|
|
{ content }
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ExpressPaymentMethods;
|