Skip to content

Commit

Permalink
Site Editor: Don't use 'useEntityRecord' to only dispatch actions (#5…
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka authored Nov 13, 2023
1 parent e1cb5e9 commit 20004d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 20004d5

Please sign in to comment.