woocommerce/plugins/woocommerce-blocks/assets/js/blocks/page-content-wrapper/index.tsx

89 lines
1.9 KiB
TypeScript
Raw Normal View History

Refactor Cart and Checkout Page Templates (https://github.com/woocommerce/woocommerce-blocks/pull/10773) * Rename checkout template slug * Remove redirect and custom title * Classic shortcode block for checkout * Empty title * WIP placeholder * Change blockified template * Prefix cart and checkout templates with "Page: " * Template migration routine * Apply same treatment to cart template * Notices * Update placeholder text * Classic shortcodes block * Page content wrapper for templates * Update default * Do not save attributes * Update templates * Remove cart classic template * Reverted endpoints for Cart & Checkout templates. This reverts PR 9406 * Migrate page content wrapper. * Removed useless method arg. Minor tweaks. * Skip migration if the theme has a template file for this page. * Removed impossible condition. * Migrate page content wrapper. * Remove TemplateNotice in favour of DefaultNotice * Documentation links in shortcode placeholder * Hide cart and checkout page selector when using block themes * Unused var * Add tests for template changes * Revert changes to classic-template * Allow frontend redirect * Unused file * Bump version for updater * Support x template naming as well as page-x * Need to add item to cart to test checkout * Fix header test by fixing utility for adding to cart * Remove permalink tests * Click body * Wait for content to finish loading * Wait for add to cart response when adding to cart without item name * Wait for save before visiting frontend * Set content instead of inserting block * Perform test in site editor rather than page editor * Prevent notice to set the default cart/checkout page from showing on the site editor. --------- Co-authored-by: Paulo Arromba <17236129+wavvves@users.noreply.github.com> Co-authored-by: Thomas Roberts <thomas.roberts@automattic.com>
2023-09-19 09:58:18 +00:00
/**
* External dependencies
*/
import {
registerBlockType,
InnerBlockTemplate,
BlockAttributes,
} from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';
import { page } from '@wordpress/icons';
import { CHECKOUT_PAGE_ID, CART_PAGE_ID } from '@woocommerce/block-settings';
import { useEffect } from '@wordpress/element';
/**
* Internal dependencies
*/
import metadata from './block.json';
import './editor.scss';
const Edit = ( {
attributes,
setAttributes,
}: {
attributes: BlockAttributes;
setAttributes: ( attrs: BlockAttributes ) => void;
} ) => {
const TEMPLATE: InnerBlockTemplate[] = [
[ 'core/post-title', { align: 'wide' } ],
[ 'core/post-content', { align: 'wide' } ],
];
const blockProps = useBlockProps( {
className: 'wp-block-woocommerce-page-content-wrapper',
} );
useEffect( () => {
if ( ! attributes.postId && attributes.page ) {
let postId = 0;
if ( attributes.page === 'checkout' ) {
postId = CHECKOUT_PAGE_ID;
}
if ( attributes.page === 'cart' ) {
postId = CART_PAGE_ID;
}
if ( postId ) {
setAttributes( { postId, postType: 'page' } );
}
}
}, [ attributes, setAttributes ] );
return (
<div { ...blockProps }>
<InnerBlocks template={ TEMPLATE } />
</div>
);
};
registerBlockType( metadata, {
icon: {
src: page,
},
edit: Edit,
save: () => <InnerBlocks.Content />,
variations: [
{
name: 'checkout-page',
title: __( 'Checkout Page', 'woo-gutenberg-products-block' ),
attributes: {
page: 'checkout',
},
isActive: ( blockAttributes, variationAttributes ) =>
blockAttributes.page === variationAttributes.page,
},
{
name: 'cart-page',
title: __( 'Cart Page', 'woo-gutenberg-products-block' ),
attributes: {
page: 'cart',
},
isActive: ( blockAttributes, variationAttributes ) =>
blockAttributes.page === variationAttributes.page,
},
],
} );