Skip to content

Commit

Permalink
Post Title: avoid accidental types requests (#60531)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Apr 6, 2024
1 parent 097ba24 commit 2b8519b
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions packages/block-library/src/post-title/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ import {
import { ToggleControl, TextControl, PanelBody } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
import { useEntityProp } from '@wordpress/core-data';

/**
* Internal dependencies
*/
import { useCanEditEntity } from '../utils/hooks';
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';

export default function PostTitleEdit( {
attributes: { level, textAlign, isLink, rel, linkTarget },
Expand All @@ -33,16 +29,26 @@ export default function PostTitleEdit( {
} ) {
const TagName = 'h' + level;
const isDescendentOfQueryLoop = Number.isFinite( queryId );
/**
* Hack: useCanEditEntity may trigger an OPTIONS request to the REST API via the canUser resolver.
* However, when the Post Title is a descendant of a Query Loop block, the title cannot be edited.
* In order to avoid these unnecessary requests, we call the hook without
* the proper data, resulting in returning early without making them.
*/
const userCanEdit = useCanEditEntity(
'postType',
! isDescendentOfQueryLoop && postType,
postId
const userCanEdit = useSelect(
( select ) => {
/**
* useCanEditEntity may trigger an OPTIONS request to the REST API
* via the canUser resolver. However, when the Post Title is a
* descendant of a Query Loop block, the title cannot be edited. In
* order to avoid these unnecessary requests, we call the hook
* without the proper data, resulting in returning early without
* making them.
*/
if ( isDescendentOfQueryLoop ) {
return false;
}
return select( coreStore ).canUserEditEntityRecord(
'postType',
postType,
postId
);
},
[ isDescendentOfQueryLoop, postType, postId ]
);
const [ rawTitle = '', setTitle, fullTitle ] = useEntityProp(
'postType',
Expand Down

0 comments on commit 2b8519b

Please sign in to comment.