Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Site Editor: Don't use 'useEntityRecord' to only dispatch actions #56076

Merged
merged 1 commit into from Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -3,7 +3,8 @@
*/
import { MenuGroup, MenuItem } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useEntityRecord } from '@wordpress/core-data';
import { useDispatch } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';

/**
* Internal dependencies
Expand All @@ -18,7 +19,7 @@ export default function ResetDefaultTemplate( { onClick } ) {
const currentTemplateSlug = useCurrentTemplateSlug();
const isPostsPage = useIsPostsPage();
const { postType, postId } = useEditedPostContext();
const entity = useEntityRecord( 'postType', postType, postId );
const { editEntityRecord } = useDispatch( coreStore );
// The default template in a post is indicated by an empty string.
if ( ! currentTemplateSlug || isPostsPage ) {
return null;
Expand All @@ -27,7 +28,13 @@ export default function ResetDefaultTemplate( { onClick } ) {
<MenuGroup>
<MenuItem
onClick={ async () => {
entity.edit( { template: '' }, { undoIgnore: true } );
editEntityRecord(
'postType',
postType,
postId,
{ template: '' },
{ undoIgnore: true }
);
onClick();
} }
>
Expand Down
Expand Up @@ -6,7 +6,8 @@ import { decodeEntities } from '@wordpress/html-entities';
import { __experimentalBlockPatternsList as BlockPatternsList } from '@wordpress/block-editor';
import { MenuItem, Modal } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useEntityRecord } from '@wordpress/core-data';
import { useDispatch } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { parse } from '@wordpress/blocks';
import { useAsyncList } from '@wordpress/compose';

Expand All @@ -22,12 +23,18 @@ export default function SwapTemplateButton( { onClick } ) {
setShowModal( false );
}, [] );
const { postType, postId } = useEditedPostContext();
const entitiy = useEntityRecord( 'postType', postType, postId );
const { editEntityRecord } = useDispatch( coreStore );
if ( ! availableTemplates?.length ) {
return null;
}
const onTemplateSelect = async ( template ) => {
entitiy.edit( { template: template.name }, { undoIgnore: true } );
editEntityRecord(
'postType',
postType,
postId,
{ template: template.name },
{ undoIgnore: true }
);
onClose(); // Close the template suggestions modal first.
onClick();
};
Expand Down