Skip to content

Commit

Permalink
Ensure that the unsaved title is not persisted when reopening the mod…
Browse files Browse the repository at this point in the history
…al (#52473)
  • Loading branch information
t-hamano committed Jul 10, 2023
1 parent 05f75a9 commit f448947
Showing 1 changed file with 12 additions and 9 deletions.
Expand Up @@ -17,10 +17,8 @@ import { store as noticesStore } from '@wordpress/notices';
import { decodeEntities } from '@wordpress/html-entities';

export default function RenameMenuItem( { template, onClose } ) {
const [ title, setTitle ] = useState(
decodeEntities( template.title.rendered )
);

const title = decodeEntities( template.title.rendered );
const [ editedTitle, setEditedTitle ] = useState( title );
const [ isModalOpen, setIsModalOpen ] = useState( false );

const {
Expand All @@ -39,11 +37,11 @@ export default function RenameMenuItem( { template, onClose } ) {

try {
await editEntityRecord( 'postType', template.type, template.id, {
title,
title: editedTitle,
} );

// Update state before saving rerenders the list.
setTitle( '' );
setEditedTitle( '' );
setIsModalOpen( false );
onClose();

Expand Down Expand Up @@ -73,7 +71,12 @@ export default function RenameMenuItem( { template, onClose } ) {

return (
<>
<MenuItem onClick={ () => setIsModalOpen( true ) }>
<MenuItem
onClick={ () => {
setIsModalOpen( true );
setEditedTitle( title );
} }
>
{ __( 'Rename' ) }
</MenuItem>
{ isModalOpen && (
Expand All @@ -89,8 +92,8 @@ export default function RenameMenuItem( { template, onClose } ) {
<TextControl
__nextHasNoMarginBottom
label={ __( 'Name' ) }
value={ title }
onChange={ setTitle }
value={ editedTitle }
onChange={ setEditedTitle }
required
/>

Expand Down

1 comment on commit f448947

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in f448947.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5514181653
📝 Reported issues:

Please sign in to comment.