Skip to content

Commit

Permalink
Add: Duplicate post action.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Apr 15, 2024
1 parent 06b3537 commit e4ecc68
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
103 changes: 102 additions & 1 deletion packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { addQueryArgs } from '@wordpress/url';
import { useDispatch } from '@wordpress/data';
import { decodeEntities } from '@wordpress/html-entities';
import { store as coreStore } from '@wordpress/core-data';
import { __, _n, sprintf } from '@wordpress/i18n';
import { __, _n, sprintf, _x } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { useMemo, useState } from '@wordpress/element';

Expand Down Expand Up @@ -499,6 +499,106 @@ const renamePostAction = {
},
};

export const duplicatePostAction = {
id: 'duplicate-post',
label: _x( 'Duplicate', 'action label' ),
isEligible( { status } ) {
return status !== 'trash';
},
RenderModal: ( { items, closeModal, onActionPerformed } ) => {
const [ item ] = items;
const [ isCreatingPage, setIsCreatingPage ] = useState( false );
const [ title, setTitle ] = useState( getItemTitle( item ) );

const { saveEntityRecord } = useDispatch( coreStore );
const { createErrorNotice, createSuccessNotice } =
useDispatch( noticesStore );

async function createPage( event ) {
event.preventDefault();

if ( isCreatingPage ) {
return;
}
setIsCreatingPage( true );
try {
const newItem = await saveEntityRecord(
'postType',
item.type,
{
status: 'draft',
title,
slug: title || __( 'No title' ),
author: item.author,
comment_status: item.comment_status,
content: item.content.raw,
excerpt: item.excerpt.raw,
meta: item.meta,
parent: item.parent,
password: item.password,
template: item.template,
featured_media: item.featured_media,
menu_order: item.menu_order,
ping_status: item.ping_status,
},
{ throwOnError: true }
);

createSuccessNotice(
sprintf(
// translators: %s: Title of the created template e.g: "Category".
__( '"%s" successfully created.' ),
newItem.title?.rendered || title
),
{
type: 'snackbar',
}
);
if ( onActionPerformed ) {
onActionPerformed( items );
}
} catch ( error ) {
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: __( 'An error occurred while duplicating the page.' );

createErrorNotice( errorMessage, {
type: 'snackbar',
} );
} finally {
setIsCreatingPage( false );
closeModal();
}
}
return (
<form onSubmit={ createPage }>
<VStack spacing={ 3 }>
<TextControl
label={ __( 'Title' ) }
onChange={ setTitle }
placeholder={ __( 'No title' ) }
value={ title }
/>
<HStack spacing={ 2 } justify="end">
<Button variant="tertiary" onClick={ closeModal }>
{ __( 'Cancel' ) }
</Button>
<Button
variant="primary"
type="submit"
isBusy={ isCreatingPage }
aria-disabled={ isCreatingPage }
>
{ _x( 'Duplicate', 'action label' ) }
</Button>
</HStack>
</VStack>
</form>
);
},
};

const resetTemplateAction = {
id: 'reset-template',
label: __( 'Reset' ),
Expand Down Expand Up @@ -780,6 +880,7 @@ export function usePostActions( onActionPerformed, actionIds = null ) {
deleteTemplateAction,
permanentlyDeletePostAction,
postRevisionsAction,
duplicatePostAction,
renamePostAction,
renameTemplateAction,
trashPostAction,
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/components/post-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const POST_ACTIONS_WHILE_EDITING = [
'view-post-revisions',
'rename-post',
'move-to-trash',
'duplicate-post',
];

export default function PostActions( { onActionPerformed } ) {
Expand Down

0 comments on commit e4ecc68

Please sign in to comment.