Skip to content

Commit

Permalink
Add edit option to the duplicate post action
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Apr 19, 2024
1 parent 3dc5cf7 commit 965afb7
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 33 deletions.
62 changes: 52 additions & 10 deletions packages/edit-post/src/components/sidebar/settings-sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
useEffect,
useRef,
} from '@wordpress/element';
import { isRTL, __ } from '@wordpress/i18n';
import { isRTL, __, sprintf } from '@wordpress/i18n';
import { drawerLeft, drawerRight } from '@wordpress/icons';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import {
Expand All @@ -28,6 +28,7 @@ import {
privateApis as editorPrivateApis,
} from '@wordpress/editor';
import { addQueryArgs } from '@wordpress/url';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
Expand All @@ -54,15 +55,6 @@ export const sidebars = {
block: 'edit-post/block',
};

function onActionPerformed( actionId, items ) {
if ( actionId === 'move-to-trash' ) {
const postType = items[ 0 ].type;
document.location.href = addQueryArgs( 'edit.php', {
post_type: postType,
} );
}
}

const SidebarContent = ( { tabName, keyboardShortcut, isEditingTemplate } ) => {
const tabListRef = useRef( null );
// Because `PluginSidebar` renders a `ComplementaryArea`, we
Expand Down Expand Up @@ -96,6 +88,56 @@ const SidebarContent = ( { tabName, keyboardShortcut, isEditingTemplate } ) => {
selectedTabElement?.focus();
}
}, [ tabName ] );
const { createSuccessNotice } = useDispatch( noticesStore );

const onActionPerformed = useCallback(
( actionId, items ) => {
switch ( actionId ) {
case 'move-to-trash':
{
const postType = items[ 0 ].type;
document.location.href = addQueryArgs( 'edit.php', {
post_type: postType,
} );
}
break;
case 'duplicate-post':
{
const newItem = items[ 0 ];
const title =
typeof newItem.title === 'string'
? newItem.title
: newItem.title?.rendered;
createSuccessNotice(
sprintf(
// translators: %s: Title of the created post e.g: "Post 1".
__( '"%s" successfully created.' ),
title
),
{
type: 'snackbar',
id: 'duplicate-post-action',
actions: [
{
label: __( 'Edit' ),
onClick: () => {
const postId = newItem.id;
document.location.href =
addQueryArgs( 'post.php', {
post: postId,
action: 'edit',
} );
},
},
],
}
);
}
break;
}
},
[ createSuccessNotice ]
);

return (
<PluginSidebar
Expand Down
1 change: 1 addition & 0 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ const PAGE_ACTIONS = [
'restore',
'permanently-delete',
'view-post-revisions',
'duplicate-post',
'rename-post',
'move-to-trash',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* WordPress dependencies
*/
import { PanelBody } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import {
PageAttributesPanel,
Expand All @@ -17,6 +17,7 @@ import {
} from '@wordpress/editor';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { useCallback } from '@wordpress/element';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
Expand Down Expand Up @@ -53,20 +54,57 @@ export default function PagePanels() {
renderingMode: getRenderingMode(),
};
}, [] );

const { createSuccessNotice } = useDispatch( noticesStore );
const history = useHistory();
const onActionPerformed = useCallback(
( actionId, items ) => {
if ( actionId === 'move-to-trash' ) {
history.push( {
path: '/' + items[ 0 ].type,
postId: undefined,
postType: undefined,
canvas: 'view',
} );
switch ( actionId ) {
case 'move-to-trash':
{
history.push( {
path: '/' + items[ 0 ].type,
postId: undefined,
postType: undefined,
canvas: 'view',
} );
}
break;
case 'duplicate-post':
{
const newItem = items[ 0 ];
const title =
typeof newItem.title === 'string'
? newItem.title
: newItem.title?.rendered;
createSuccessNotice(
sprintf(
// translators: %s: Title of the created post e.g: "Post 1".
__( '"%s" successfully created.' ),
title
),
{
type: 'snackbar',
id: 'duplicate-post-action',
actions: [
{
label: __( 'Edit' ),
onClick: () => {
history.push( {
path: undefined,
postId: newItem.id,
postType: newItem.type,
canvas: 'edit',
} );
},
},
],
}
);
}
break;
}
},
[ history ]
[ history, createSuccessNotice ]
);

if ( ! hasResolved ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';
import {
__experimentalVStack as VStack,
Expand All @@ -17,6 +17,7 @@ import { safeDecodeURIComponent, filterURLForDisplay } from '@wordpress/url';
import { useEffect, useCallback } from '@wordpress/element';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { privateApis as editorPrivateApis } from '@wordpress/editor';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
Expand All @@ -34,6 +35,7 @@ const { PostActions } = unlock( editorPrivateApis );
export default function SidebarNavigationScreenPage( { backPath } ) {
const { setCanvasMode } = unlock( useDispatch( editSiteStore ) );
const history = useHistory();
const { createSuccessNotice } = useDispatch( noticesStore );
const {
params: { postId },
} = useLocation();
Expand Down Expand Up @@ -83,16 +85,53 @@ export default function SidebarNavigationScreenPage( { backPath } ) {

const onActionPerformed = useCallback(
( actionId, items ) => {
if ( actionId === 'move-to-trash' ) {
history.push( {
path: '/' + items[ 0 ].type,
postId: undefined,
postType: undefined,
canvas: 'view',
} );
switch ( actionId ) {
case 'move-to-trash':
{
history.push( {
path: '/' + items[ 0 ].type,
postId: undefined,
postType: undefined,
canvas: 'view',
} );
}
break;
case 'duplicate-post':
{
const newItem = items[ 0 ];
const title =
typeof newItem.title === 'string'
? newItem.title
: newItem.title?.rendered;
createSuccessNotice(
sprintf(
// translators: %s: Title of the created post e.g: "Post 1".
__( '"%s" successfully created.' ),
title
),
{
type: 'snackbar',
id: 'duplicate-post-action',
actions: [
{
label: __( 'Edit' ),
onClick: () => {
history.push( {
path: undefined,
postId: newItem.id,
postType: newItem.type,
canvas: 'edit',
} );
},
},
],
}
);
}
break;
}
},
[ history ]
[ history, createSuccessNotice ]
);

const featureImageAltText = featuredMediaAltText
Expand Down
7 changes: 4 additions & 3 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,7 @@ export const duplicatePostAction = {
);

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

async function createPage( event ) {
event.preventDefault();
Expand Down Expand Up @@ -558,11 +557,13 @@ export const duplicatePostAction = {
newItem.title?.rendered || title
),
{
id: 'duplicate-post-action',
type: 'snackbar',
}
);

if ( onActionPerformed ) {
onActionPerformed( items );
onActionPerformed( [ newItem ] );
}
} catch ( error ) {
const errorMessage =
Expand Down

0 comments on commit 965afb7

Please sign in to comment.