Skip to content

Commit

Permalink
Update title in the core data store
Browse files Browse the repository at this point in the history
  • Loading branch information
jeyip committed Sep 17, 2020
1 parent 9e52497 commit be81066
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions packages/edit-site/src/components/header/document-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
__experimentalGetBlockLabel as getBlockLabel,
getBlockType,
} from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';
import { DOWN } from '@wordpress/keycodes';

function useSecondaryText() {
Expand Down Expand Up @@ -46,9 +46,20 @@ export default function DocumentActions( { documentTitle } ) {

const { page } = useSelect( ( select ) => {
const { getPage } = select( 'core/edit-site' );
return { page: getPage() };
const { getEntityRecord } = select( 'core' );
const _page = getPage();
return {
page: getEntityRecord( 'postType', 'page', _page?.context?.postId ),
};
} );

const { editEntityRecord } = useDispatch( 'core' );
const editTitle = ( title ) => {
editEntityRecord( 'postType', 'page', page.id, {
title,
} );
};

return (
<div
className={ classnames( 'edit-site-document-actions', {
Expand Down Expand Up @@ -105,8 +116,17 @@ export default function DocumentActions( { documentTitle } ) {
{ /* TODO: Don't allow input when there is no page context */ }
<input
disabled={ ! page }
placeholder={ 'nice' }
placeholder={ page?.title?.rendered }
style={ { width: '100%' } }
onChange={ ( event ) => {
const REGEXP_NEWLINES = /[\r\n]+/g;
const title = event.target.value.replace(
REGEXP_NEWLINES,
' '
);

editTitle( title );
} }
/>
</div>
) }
Expand Down

0 comments on commit be81066

Please sign in to comment.