Skip to content

Commit

Permalink
Try adding rename post action for dataviews
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Mar 27, 2024
1 parent e408f73 commit e9e2028
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
77 changes: 76 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,77 @@ export const postRevisionsAction = {
document.location.href = href;
},
};

export const renamePostAction = {
id: 'rename-post',
label: __( 'Rename' ),
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

0 comments on commit e9e2028

Please sign in to comment.