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

Ensure that the unsaved title is not persisted when reopening the modal #52473

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
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