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:
parent
363d761eae
commit
30c449e749
|
@ -0,0 +1,4 @@
|
|||
Significance: minor
|
||||
Type: fix
|
||||
|
||||
Fix layout template request not being executed
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { useEffect } from 'react';
|
||||
import { useEffect, useState } from '@wordpress/element';
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore No types for this exist yet, natively (not until 7.0.0).
|
||||
// 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,
|
||||
// so just going type-free for now.
|
||||
// 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 ) => {
|
||||
const layoutTemplateEntity = useSelect( ( select: typeof WPSelect ) => {
|
||||
const { getEntityConfig } = select( 'core' );
|
||||
return getEntityConfig( 'root', 'wcLayoutTemplate' );
|
||||
} );
|
||||
|
||||
const { addEntities } = useDispatch( 'core' );
|
||||
const [ isEntityRegistered, setIsEntityRegistered ] = useState( false );
|
||||
|
||||
useEffect( () => {
|
||||
if ( ! layoutTemplateId ) return;
|
||||
|
||||
const layoutTemplateEntity = select( 'core' ).getEntityConfig(
|
||||
'root',
|
||||
'wcLayoutTemplate'
|
||||
);
|
||||
|
||||
if ( ! layoutTemplateEntity ) {
|
||||
addEntities( [
|
||||
dispatch( 'core' ).addEntities( [
|
||||
{
|
||||
kind: 'root',
|
||||
name: 'wcLayoutTemplate',
|
||||
|
@ -34,7 +36,9 @@ export const useLayoutTemplate = ( layoutTemplateId: string | undefined ) => {
|
|||
},
|
||||
] );
|
||||
}
|
||||
}, [ addEntities, layoutTemplateEntity ] );
|
||||
|
||||
setIsEntityRegistered( true );
|
||||
}, [ layoutTemplateId ] );
|
||||
|
||||
const { record: layoutTemplate, isResolving } = useEntityRecord(
|
||||
'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),
|
||||
// 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
|
||||
{ enabled: !! layoutTemplateId }
|
||||
{ enabled: isEntityRegistered }
|
||||
);
|
||||
|
||||
return { layoutTemplate, isResolving };
|
||||
|
|
Loading…
Reference in New Issue