This commit is contained in:
Luigi Teschio 2023-08-04 16:16:19 +02:00 committed by GitHub
parent 76b82000b3
commit 48493a8fbb
2 changed files with 19 additions and 4 deletions

View File

@ -1,6 +1,7 @@
/**
* External dependencies
*/
import { isNumber } from '@woocommerce/types';
import {
BlockAttributes,
BlockConfiguration,
@ -16,8 +17,10 @@ import { subscribe, select } from '@wordpress/data';
// Creating a local cache to prevent multiple registration tries.
const blocksRegistered = new Set();
function parseTemplateId( templateId: string | undefined ) {
return templateId?.split( '//' )[ 1 ];
function parseTemplateId( templateId: string | number | undefined ) {
// With GB 16.3.0 the return type can be a number: https://github.com/WordPress/gutenberg/issues/53230
const parsedTemplateId = isNumber( templateId ) ? undefined : templateId;
return parsedTemplateId?.split( '//' )[ 1 ];
}
export const registerBlockSingleProductTemplate = ( {
@ -40,7 +43,11 @@ export const registerBlockSingleProductTemplate = ( {
subscribe( () => {
const previousTemplateId = currentTemplateId;
const store = select( 'core/edit-site' );
currentTemplateId = parseTemplateId( store?.getEditedPostId() );
// With GB 16.3.0 the return type can be a number: https://github.com/WordPress/gutenberg/issues/53230
currentTemplateId = parseTemplateId(
store?.getEditedPostId() as string | number | undefined
);
const hasChangedTemplate = previousTemplateId !== currentTemplateId;
const hasTemplateId = Boolean( currentTemplateId );

View File

@ -31,6 +31,7 @@ import { store as noticesStore } from '@wordpress/notices';
import { useEntityRecord } from '@wordpress/core-data';
import { debounce } from '@woocommerce/base-utils';
import { woo } from '@woocommerce/icons';
import { isNumber } from '@woocommerce/types';
/**
* Internal dependencies
@ -417,7 +418,14 @@ let currentTemplateId: string | undefined;
subscribe( () => {
const previousTemplateId = currentTemplateId;
const store = select( 'core/edit-site' );
currentTemplateId = store?.getEditedPostId() as string | undefined;
// With GB 16.3.0 the return type can be a number: https://github.com/WordPress/gutenberg/issues/53230
const editedPostId = store?.getEditedPostId() as
| string
| number
| undefined;
currentTemplateId = isNumber( editedPostId ) ? undefined : editedPostId;
const parsedTemplate = currentTemplateId?.split( '//' )[ 1 ];
if ( parsedTemplate === null || parsedTemplate === undefined ) {