Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

WooCommerce Classic Template block: Fix error on clearing customizations on Woo Templates #9759

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 62 additions & 9 deletions assets/js/blocks/classic-template/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getBlockType,
registerBlockType,
unregisterBlockType,
parse,
} from '@wordpress/blocks';
import type { BlockEditProps } from '@wordpress/blocks';
import { WC_BLOCKS_IMAGE_URL } from '@woocommerce/block-settings';
Expand All @@ -18,10 +19,17 @@ import {
import { Button, Placeholder, Popover } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { box, Icon } from '@wordpress/icons';
import { useDispatch, subscribe, useSelect, select } from '@wordpress/data';
import {
useDispatch,
subscribe,
useSelect,
select,
dispatch,
} from '@wordpress/data';
import { useEffect, useState } from '@wordpress/element';
import { store as noticesStore } from '@wordpress/notices';
import { useEntityRecord } from '@wordpress/core-data';
import { debounce } from '@woocommerce/base-utils';

/**
* Internal dependencies
Expand Down Expand Up @@ -333,6 +341,44 @@ const registerClassicTemplateBlock = ( {
} );
};

/**
* Attempts to recover the Classic Template block if it fails to render on the Single Product template
* due to the user resetting customizations without refreshing the page.
*
* When the Classic Template block fails to render, it is replaced by the 'core/missing' block, which
* displays an error message stating that the WooCommerce Classic template block is unsupported.
*
* This function replaces the 'core/missing' block with the original Classic Template block that failed
* to render, allowing the block to be displayed correctly.
*
thealexandrelara marked this conversation as resolved.
Show resolved Hide resolved
* @see {@link https://github.com/woocommerce/woocommerce-blocks/issues/9637|Issue: Block error is displayed on clearing customizations for Woo Templates}
*
*/
const tryToRecoverClassicTemplateBlockWhenItFailsToRender = debounce( () => {
const blocks = select( 'core/block-editor' ).getBlocks();
const blocksIncludingInnerBlocks = blocks.flatMap( ( block ) => [
block,
...block.innerBlocks,
] );
const classicTemplateThatFailedToRender = blocksIncludingInnerBlocks.find(
( block ) =>
block.name === 'core/missing' &&
block.attributes.originalName === BLOCK_SLUG
);

if ( classicTemplateThatFailedToRender ) {
const blockToReplaceClassicTemplateBlockThatFailedToRender = parse(
classicTemplateThatFailedToRender.attributes.originalContent
);
if ( blockToReplaceClassicTemplateBlockThatFailedToRender ) {
dispatch( 'core/block-editor' ).replaceBlock(
classicTemplateThatFailedToRender.clientId,
blockToReplaceClassicTemplateBlockThatFailedToRender
);
}
}
}, 100 );

// @todo Refactor when there will be possible to show a block according on a template/post with a Gutenberg API. https://github.com/WordPress/gutenberg/pull/41718

let currentTemplateId: string | undefined;
Expand All @@ -341,21 +387,28 @@ subscribe( () => {
const previousTemplateId = currentTemplateId;
const store = select( 'core/edit-site' );
currentTemplateId = store?.getEditedPostId() as string | undefined;

if ( previousTemplateId === currentTemplateId ) {
return;
}

const parsedTemplate = currentTemplateId?.split( '//' )[ 1 ];

if ( parsedTemplate === null || parsedTemplate === undefined ) {
return;
}

const block = getBlockType( BLOCK_SLUG );
const isBlockRegistered = Boolean( block );

if (
isBlockRegistered &&
hasTemplateSupportForClassicTemplateBlock( parsedTemplate, TEMPLATES )
) {
tryToRecoverClassicTemplateBlockWhenItFailsToRender();
}

if ( previousTemplateId === currentTemplateId ) {
return;
}

if (
block !== undefined &&
isBlockRegistered &&
( ! hasTemplateSupportForClassicTemplateBlock(
parsedTemplate,
TEMPLATES
Expand All @@ -371,12 +424,12 @@ subscribe( () => {
}

if (
block === undefined &&
! isBlockRegistered &&
hasTemplateSupportForClassicTemplateBlock( parsedTemplate, TEMPLATES )
) {
registerClassicTemplateBlock( {
template: parsedTemplate,
inserter: true,
} );
}
} );
}, 'core/blocks-editor' );