Skip to content

Commit

Permalink
Site Editor: Add rename page action (WordPress#60230)
Browse files Browse the repository at this point in the history
Co-authored-by: aaronrobertshaw <aaronrobertshaw@git.wordpress.org>
Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
Co-authored-by: jameskoster <jameskoster@git.wordpress.org>
  • Loading branch information
4 people authored and cbravobernal committed Apr 9, 2024
1 parent 71d8d60 commit 1145492
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
80 changes: 79 additions & 1 deletion packages/edit-site/src/components/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import { decodeEntities } from '@wordpress/html-entities';
import { store as coreStore } from '@wordpress/core-data';
import { __, _n, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { useMemo } from '@wordpress/element';
import { useMemo, useState } from '@wordpress/element';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import {
Button,
TextControl,
__experimentalText as Text,
__experimentalHStack as HStack,
__experimentalVStack as VStack,
Expand Down Expand Up @@ -411,3 +412,80 @@ export const postRevisionsAction = {
document.location.href = href;
},
};

export const renamePostAction = {
id: 'rename-post',
label: __( 'Rename' ),
isEligible( post ) {
return post.status !== 'trash';
},
RenderModal: ( { items, closeModal } ) => {
const [ item ] = items;
const originalTitle = decodeEntities(
typeof item.title === 'string' ? item.title : item.title.rendered
);
const [ title, setTitle ] = useState( () => originalTitle );
const { editEntityRecord, saveEditedEntityRecord } =
useDispatch( coreStore );
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );

async function onRename( event ) {
event.preventDefault();
try {
await editEntityRecord( 'postType', item.type, item.id, {
title,
} );
// Update state before saving rerenders the list.
setTitle( '' );
closeModal();
// Persist edited entity.
await saveEditedEntityRecord( 'postType', item.type, item.id, {
throwOnError: true,
} );
createSuccessNotice( __( 'Name updated' ), {
type: 'snackbar',
} );
} catch ( error ) {
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: __( 'An error occurred while updating the name' );
createErrorNotice( errorMessage, { type: 'snackbar' } );
}
}

return (
<form onSubmit={ onRename }>
<VStack spacing="5">
<TextControl
__nextHasNoMarginBottom
__next40pxDefaultSize
label={ __( 'Name' ) }
value={ title }
onChange={ setTitle }
required
/>
<HStack justify="right">
<Button
__next40pxDefaultSize
variant="tertiary"
onClick={ () => {
closeModal();
} }
>
{ __( 'Cancel' ) }
</Button>
<Button
__next40pxDefaultSize
variant="primary"
type="submit"
>
{ __( 'Save' ) }
</Button>
</HStack>
</VStack>
</form>
);
},
};
2 changes: 2 additions & 0 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
} from '../../utils/constants';

import {
renamePostAction,
trashPostAction,
usePermanentlyDeletePostAction,
useRestorePostAction,
Expand Down Expand Up @@ -356,6 +357,7 @@ export default function PagePages() {
restorePostAction,
permanentlyDeletePostAction,
postRevisionsAction,
renamePostAction,
trashPostAction,
],
[ permanentlyDeletePostAction, restorePostAction, editPostAction ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,25 @@ export const renameTemplateAction = {
<VStack spacing="5">
<TextControl
__nextHasNoMarginBottom
__next40pxDefaultSize
label={ __( 'Name' ) }
value={ editedTitle }
onChange={ setEditedTitle }
required
/>
<HStack justify="right">
<Button variant="tertiary" onClick={ closeModal }>
<Button
variant="tertiary"
onClick={ closeModal }
__next40pxDefaultSize
>
{ __( 'Cancel' ) }
</Button>
<Button variant="primary" type="submit">
<Button
variant="primary"
type="submit"
__next40pxDefaultSize
>
{ __( 'Save' ) }
</Button>
</HStack>
Expand Down

0 comments on commit 1145492

Please sign in to comment.