Fix layout template request not being executed after registering a new entity with the new version of GB installed (#47678)

* Fix layout template request not being executed after registering a new entity with the new version of GB installed

* Add changelog file
This commit is contained in:
Maikel Perez 2024-05-21 16:02:02 -04:00 committed by GitHub
parent 363d761eae
commit 30c449e749
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 11 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: fix
Fix layout template request not being executed

View File

@ -1,7 +1,7 @@
/** /**
* External dependencies * External dependencies
*/ */
import { useEffect } from 'react'; import { useEffect, useState } from '@wordpress/element';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore No types for this exist yet, natively (not until 7.0.0). // @ts-ignore No types for this exist yet, natively (not until 7.0.0).
// Including `@types/wordpress__data` as a devDependency causes build issues, // Including `@types/wordpress__data` as a devDependency causes build issues,
@ -13,19 +13,21 @@ import { useEntityRecord } from '@wordpress/core-data';
// Including `@types/wordpress__data` as a devDependency causes build issues, // Including `@types/wordpress__data` as a devDependency causes build issues,
// so just going type-free for now. // so just going type-free for now.
// eslint-disable-next-line @woocommerce/dependency-group // eslint-disable-next-line @woocommerce/dependency-group
import { useDispatch, useSelect, select as WPSelect } from '@wordpress/data'; import { dispatch, select } from '@wordpress/data';
export const useLayoutTemplate = ( layoutTemplateId: string | undefined ) => { export const useLayoutTemplate = ( layoutTemplateId: string | undefined ) => {
const layoutTemplateEntity = useSelect( ( select: typeof WPSelect ) => { const [ isEntityRegistered, setIsEntityRegistered ] = useState( false );
const { getEntityConfig } = select( 'core' );
return getEntityConfig( 'root', 'wcLayoutTemplate' );
} );
const { addEntities } = useDispatch( 'core' );
useEffect( () => { useEffect( () => {
if ( ! layoutTemplateId ) return;
const layoutTemplateEntity = select( 'core' ).getEntityConfig(
'root',
'wcLayoutTemplate'
);
if ( ! layoutTemplateEntity ) { if ( ! layoutTemplateEntity ) {
addEntities( [ dispatch( 'core' ).addEntities( [
{ {
kind: 'root', kind: 'root',
name: 'wcLayoutTemplate', name: 'wcLayoutTemplate',
@ -34,7 +36,9 @@ export const useLayoutTemplate = ( layoutTemplateId: string | undefined ) => {
}, },
] ); ] );
} }
}, [ addEntities, layoutTemplateEntity ] );
setIsEntityRegistered( true );
}, [ layoutTemplateId ] );
const { record: layoutTemplate, isResolving } = useEntityRecord( const { record: layoutTemplate, isResolving } = useEntityRecord(
'root', 'root',
@ -49,7 +53,7 @@ export const useLayoutTemplate = ( layoutTemplateId: string | undefined ) => {
// Note: Until we are using @woocommerce/core-data 6.24.0 (Gutenberg 17.2), // Note: Until we are using @woocommerce/core-data 6.24.0 (Gutenberg 17.2),
// the REST API requests will still be triggered even when the query is disabled due to a regression. // the REST API requests will still be triggered even when the query is disabled due to a regression.
// See: https://github.com/WordPress/gutenberg/pull/56108 // See: https://github.com/WordPress/gutenberg/pull/56108
{ enabled: !! layoutTemplateId } { enabled: isEntityRegistered }
); );
return { layoutTemplate, isResolving }; return { layoutTemplate, isResolving };