classicBlock: add defensive type handling (https://github.com/woocommerce/woocommerce-blocks/pull/10475)
This commit is contained in:
parent
76b82000b3
commit
48493a8fbb
|
@ -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 );
|
||||
|
||||
|
|
|
@ -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 ) {
|
||||
|
|
Loading…
Reference in New Issue