2021-09-21 12:38:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { useBlockProps } from '@wordpress/block-editor';
|
|
|
|
import { Placeholder, Button } from 'wordpress-components';
|
|
|
|
import { useExpressPaymentMethods } from '@woocommerce/base-context/hooks';
|
2022-02-01 16:54:38 +00:00
|
|
|
import { Icon, payment } from '@wordpress/icons';
|
2021-09-21 12:38:56 +00:00
|
|
|
import { ADMIN_URL } from '@woocommerce/settings';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import Block from './block';
|
|
|
|
import './editor.scss';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a placeholder in the editor.
|
|
|
|
*/
|
|
|
|
const NoExpressPaymentMethodsPlaceholder = () => {
|
|
|
|
return (
|
|
|
|
<Placeholder
|
2022-02-01 16:54:38 +00:00
|
|
|
icon={ <Icon icon={ payment } /> }
|
2021-09-21 12:38:56 +00:00
|
|
|
label={ __( 'Express Checkout', 'woo-gutenberg-products-block' ) }
|
|
|
|
className="wp-block-woocommerce-checkout-express-payment-block-placeholder"
|
|
|
|
>
|
|
|
|
<span className="wp-block-woocommerce-checkout-express-payment-block-placeholder__description">
|
|
|
|
{ __(
|
|
|
|
"Your store doesn't have any Payment Methods that support the Express Checkout Block. If they are added, they will be shown here.",
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</span>
|
|
|
|
<Button
|
|
|
|
isPrimary
|
|
|
|
href={ `${ ADMIN_URL }admin.php?page=wc-settings&tab=checkout` }
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
className="wp-block-woocommerce-checkout-express-payment-block-placeholder__button"
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
'Configure Payment Methods',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</Button>
|
|
|
|
</Placeholder>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-10-25 15:31:22 +00:00
|
|
|
export const Edit = ( {
|
|
|
|
attributes,
|
|
|
|
}: {
|
|
|
|
attributes: { className: string };
|
|
|
|
} ): JSX.Element | null => {
|
2021-09-21 12:38:56 +00:00
|
|
|
const { paymentMethods, isInitialized } = useExpressPaymentMethods();
|
|
|
|
const hasExpressPaymentMethods = Object.keys( paymentMethods ).length > 0;
|
2021-09-24 13:44:05 +00:00
|
|
|
const blockProps = useBlockProps( {
|
2021-09-21 12:38:56 +00:00
|
|
|
className: classnames( {
|
|
|
|
'wp-block-woocommerce-cart-express-payment-block--has-express-payment-methods': hasExpressPaymentMethods,
|
|
|
|
} ),
|
|
|
|
} );
|
2021-10-25 15:31:22 +00:00
|
|
|
const { className } = attributes;
|
2021-09-21 12:38:56 +00:00
|
|
|
|
|
|
|
if ( ! isInitialized ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div { ...blockProps }>
|
|
|
|
{ hasExpressPaymentMethods ? (
|
2021-10-25 15:31:22 +00:00
|
|
|
<Block className={ className } />
|
2021-09-21 12:38:56 +00:00
|
|
|
) : (
|
|
|
|
<NoExpressPaymentMethodsPlaceholder />
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Save = (): JSX.Element => {
|
|
|
|
return <div { ...useBlockProps.save() } />;
|
|
|
|
};
|