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

Commit beb0286

Browse files
gigituximanish003
authored andcommitted
classicBlock: add defensive type handling (#10475)
1 parent ceb4ed1 commit beb0286

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

assets/js/atomic/utils/register-block-single-product-template.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* External dependencies
33
*/
4+
import { isNumber } from '@woocommerce/types';
45
import {
56
BlockAttributes,
67
BlockConfiguration,
@@ -16,8 +17,10 @@ import { subscribe, select } from '@wordpress/data';
1617
// Creating a local cache to prevent multiple registration tries.
1718
const blocksRegistered = new Set();
1819

19-
function parseTemplateId( templateId: string | undefined ) {
20-
return templateId?.split( '//' )[ 1 ];
20+
function parseTemplateId( templateId: string | number | undefined ) {
21+
// With GB 16.3.0 the return type can be a number: https://github.com/WordPress/gutenberg/issues/53230
22+
const parsedTemplateId = isNumber( templateId ) ? undefined : templateId;
23+
return parsedTemplateId?.split( '//' )[ 1 ];
2124
}
2225

2326
export const registerBlockSingleProductTemplate = ( {
@@ -40,7 +43,11 @@ export const registerBlockSingleProductTemplate = ( {
4043
subscribe( () => {
4144
const previousTemplateId = currentTemplateId;
4245
const store = select( 'core/edit-site' );
43-
currentTemplateId = parseTemplateId( store?.getEditedPostId() );
46+
47+
// With GB 16.3.0 the return type can be a number: https://github.com/WordPress/gutenberg/issues/53230
48+
currentTemplateId = parseTemplateId(
49+
store?.getEditedPostId() as string | number | undefined
50+
);
4451
const hasChangedTemplate = previousTemplateId !== currentTemplateId;
4552
const hasTemplateId = Boolean( currentTemplateId );
4653

assets/js/blocks/classic-template/index.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { store as noticesStore } from '@wordpress/notices';
3131
import { useEntityRecord } from '@wordpress/core-data';
3232
import { debounce } from '@woocommerce/base-utils';
3333
import { woo } from '@woocommerce/icons';
34+
import { isNumber } from '@woocommerce/types';
3435

3536
/**
3637
* Internal dependencies
@@ -417,7 +418,14 @@ let currentTemplateId: string | undefined;
417418
subscribe( () => {
418419
const previousTemplateId = currentTemplateId;
419420
const store = select( 'core/edit-site' );
420-
currentTemplateId = store?.getEditedPostId() as string | undefined;
421+
// With GB 16.3.0 the return type can be a number: https://github.com/WordPress/gutenberg/issues/53230
422+
const editedPostId = store?.getEditedPostId() as
423+
| string
424+
| number
425+
| undefined;
426+
427+
currentTemplateId = isNumber( editedPostId ) ? undefined : editedPostId;
428+
421429
const parsedTemplate = currentTemplateId?.split( '//' )[ 1 ];
422430

423431
if ( parsedTemplate === null || parsedTemplate === undefined ) {

0 commit comments

Comments
 (0)