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 authored and tellthemachines committed Jul 11, 2023
1 parent 3b9c88d commit 78d8766
Showing 1 changed file with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
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 { editEntityRecord, saveEditedEntityRecord } =
Expand All @@ -37,11 +35,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 All @@ -68,7 +66,12 @@ export default function RenameMenuItem( { template, onClose } ) {

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

Expand Down

0 comments on commit 78d8766

Please sign in to comment.