Fix: handle undefined templateSlug in Product Collection tracking utils (#47504)

* fix: handle undefined templateSlug in Product Collection tracking utils

The modification introduces a check for templateSlug before attempting to access its properties or use it in further logic. This change addresses a bug that manifested when users tried to edit a synchronized Product Collection block, resulting in a JavaScript error because templateSlug could be undefined.

* Add changefile(s) from automation for the following project(s): woocommerce-blocks

* Update types to better reflect reality

---------

Co-authored-by: github-actions <github-actions@github.com>
Co-authored-by: Karol Manijak <20098064+kmanijak@users.noreply.github.com>
This commit is contained in:
Manish Menaria 2024-05-16 11:17:52 +05:30 committed by GitHub
parent a120b99d7d
commit 1233fb4745
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 1 deletions

View File

@ -29,7 +29,7 @@ const templateSlugToTemplateMap: {
'page-checkout': Locations.CHECKOUT, 'page-checkout': Locations.CHECKOUT,
}; };
export const useTracksLocation = ( templateSlug: string ) => { export const useTracksLocation = ( templateSlug: string | undefined ) => {
const postType = useSelect( ( select ) => { const postType = useSelect( ( select ) => {
// @ts-expect-error Type definitions are missing // @ts-expect-error Type definitions are missing
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/wordpress__blocks/store/selectors.d.ts // https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/wordpress__blocks/store/selectors.d.ts
@ -40,6 +40,10 @@ export const useTracksLocation = ( templateSlug: string ) => {
return postType; return postType;
} }
if ( ! templateSlug ) {
return Locations.OTHER;
}
const template = templateSlugToTemplateMap[ templateSlug ]; const template = templateSlugToTemplateMap[ templateSlug ];
if ( template ) { if ( template ) {

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Fix: handle undefined templateSlug in Product Collection tracking utils