diff --git a/packages/edit-site/src/components/actions/index.js b/packages/edit-site/src/components/actions/index.js index 72217fba96a91..7229c0a401138 100644 --- a/packages/edit-site/src/components/actions/index.js +++ b/packages/edit-site/src/components/actions/index.js @@ -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, @@ -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 ( +
+ ); + }, +}; diff --git a/packages/edit-site/src/components/page-pages/index.js b/packages/edit-site/src/components/page-pages/index.js index 1cf1a0b077b6f..7058012539fd3 100644 --- a/packages/edit-site/src/components/page-pages/index.js +++ b/packages/edit-site/src/components/page-pages/index.js @@ -35,6 +35,7 @@ import { } from '../../utils/constants'; import { + renamePostAction, trashPostAction, usePermanentlyDeletePostAction, useRestorePostAction, @@ -356,6 +357,7 @@ export default function PagePages() { restorePostAction, permanentlyDeletePostAction, postRevisionsAction, + renamePostAction, trashPostAction, ], [ permanentlyDeletePostAction, restorePostAction, editPostAction ] diff --git a/packages/edit-site/src/components/page-templates-template-parts/actions.js b/packages/edit-site/src/components/page-templates-template-parts/actions.js index 0e9bbd3f1ab2d..baea6d9c0f3de 100644 --- a/packages/edit-site/src/components/page-templates-template-parts/actions.js +++ b/packages/edit-site/src/components/page-templates-template-parts/actions.js @@ -248,16 +248,25 @@ export const renameTemplateAction = {