2021-09-21 12:20:42 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useRef } from '@wordpress/element';
|
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2022-12-13 14:54:27 +00:00
|
|
|
import Button from '@woocommerce/base-components/button';
|
|
|
|
import {
|
|
|
|
InspectorControls,
|
|
|
|
RichText,
|
|
|
|
useBlockProps,
|
|
|
|
} from '@wordpress/block-editor';
|
2021-09-21 12:20:42 +00:00
|
|
|
import PageSelector from '@woocommerce/editor-components/page-selector';
|
|
|
|
import { CART_PAGE_ID } from '@woocommerce/block-settings';
|
2022-12-13 14:54:27 +00:00
|
|
|
|
2021-09-21 12:20:42 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-12-13 14:54:27 +00:00
|
|
|
import { defaultButtonLabel } from './constants';
|
|
|
|
|
2021-09-21 12:20:42 +00:00
|
|
|
export const Edit = ( {
|
|
|
|
attributes,
|
|
|
|
setAttributes,
|
|
|
|
}: {
|
|
|
|
attributes: {
|
|
|
|
checkoutPageId: number;
|
2021-10-25 15:31:22 +00:00
|
|
|
className: string;
|
2022-12-13 14:54:27 +00:00
|
|
|
buttonLabel: string;
|
2021-09-21 12:20:42 +00:00
|
|
|
};
|
|
|
|
setAttributes: ( attributes: Record< string, unknown > ) => void;
|
|
|
|
} ): JSX.Element => {
|
2021-09-24 13:44:05 +00:00
|
|
|
const blockProps = useBlockProps();
|
2022-12-13 14:54:27 +00:00
|
|
|
const { checkoutPageId = 0, buttonLabel } = attributes;
|
2021-09-21 12:20:42 +00:00
|
|
|
const { current: savedCheckoutPageId } = useRef( checkoutPageId );
|
2022-12-13 14:54:27 +00:00
|
|
|
|
2021-09-21 12:20:42 +00:00
|
|
|
const currentPostId = useSelect(
|
|
|
|
( select ) => {
|
|
|
|
if ( ! savedCheckoutPageId ) {
|
|
|
|
const store = select( 'core/editor' );
|
|
|
|
return store.getCurrentPostId();
|
|
|
|
}
|
|
|
|
return savedCheckoutPageId;
|
|
|
|
},
|
|
|
|
[ savedCheckoutPageId ]
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div { ...blockProps }>
|
|
|
|
<InspectorControls>
|
|
|
|
{ ! (
|
|
|
|
currentPostId === CART_PAGE_ID && savedCheckoutPageId === 0
|
|
|
|
) && (
|
|
|
|
<PageSelector
|
|
|
|
pageId={ checkoutPageId }
|
2022-12-13 14:54:27 +00:00
|
|
|
setPageId={ ( id: number ) =>
|
2021-09-21 12:20:42 +00:00
|
|
|
setAttributes( { checkoutPageId: id } )
|
|
|
|
}
|
|
|
|
labels={ {
|
|
|
|
title: __(
|
|
|
|
'Proceed to Checkout button',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
default: __(
|
|
|
|
'WooCommerce Checkout Page',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</InspectorControls>
|
2022-12-13 14:54:27 +00:00
|
|
|
<Button className="wc-block-cart__submit-button">
|
|
|
|
<RichText
|
|
|
|
multiline={ false }
|
|
|
|
allowedFormats={ [] }
|
|
|
|
value={ buttonLabel }
|
|
|
|
placeholder={ defaultButtonLabel }
|
|
|
|
onChange={ ( content ) => {
|
|
|
|
setAttributes( {
|
|
|
|
buttonLabel: content,
|
|
|
|
} );
|
|
|
|
} }
|
2021-10-25 15:31:22 +00:00
|
|
|
/>
|
2022-12-13 14:54:27 +00:00
|
|
|
</Button>
|
2021-09-21 12:20:42 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Save = (): JSX.Element => {
|
|
|
|
return <div { ...useBlockProps.save() } />;
|
|
|
|
};
|