2021-09-21 12:20:42 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useRef } from '@wordpress/element';
|
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
|
|
|
|
import PageSelector from '@woocommerce/editor-components/page-selector';
|
|
|
|
import { CART_PAGE_ID } from '@woocommerce/block-settings';
|
2021-11-26 14:47:37 +00:00
|
|
|
import Noninteractive from '@woocommerce/base-components/noninteractive';
|
2021-09-21 12:20:42 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import Block from './block';
|
|
|
|
export const Edit = ( {
|
|
|
|
attributes,
|
|
|
|
setAttributes,
|
|
|
|
}: {
|
|
|
|
attributes: {
|
|
|
|
checkoutPageId: number;
|
2021-10-25 15:31:22 +00:00
|
|
|
className: 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();
|
2021-10-25 15:31:22 +00:00
|
|
|
const { checkoutPageId = 0, className } = attributes;
|
2021-09-21 12:20:42 +00:00
|
|
|
const { current: savedCheckoutPageId } = useRef( checkoutPageId );
|
|
|
|
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 }
|
|
|
|
setPageId={ ( id ) =>
|
|
|
|
setAttributes( { checkoutPageId: id } )
|
|
|
|
}
|
|
|
|
labels={ {
|
|
|
|
title: __(
|
|
|
|
'Proceed to Checkout button',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
default: __(
|
|
|
|
'WooCommerce Checkout Page',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</InspectorControls>
|
2021-11-26 14:47:37 +00:00
|
|
|
<Noninteractive>
|
2021-10-25 15:31:22 +00:00
|
|
|
<Block
|
|
|
|
checkoutPageId={ checkoutPageId }
|
|
|
|
className={ className }
|
|
|
|
/>
|
2021-11-26 14:47:37 +00:00
|
|
|
</Noninteractive>
|
2021-09-21 12:20:42 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Save = (): JSX.Element => {
|
|
|
|
return <div { ...useBlockProps.save() } />;
|
|
|
|
};
|