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
|
* 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 };
|
||||||
|
|
Loading…
Reference in New Issue