Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Remove post actions hook. #61040

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/dataviews/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@wordpress/a11y": "file:../a11y",
"@wordpress/components": "file:../components",
"@wordpress/compose": "file:../compose",
"@wordpress/data": "file:../data",
"@wordpress/element": "file:../element",
"@wordpress/i18n": "file:../i18n",
"@wordpress/icons": "file:../icons",
Expand Down
22 changes: 20 additions & 2 deletions packages/dataviews/src/bulk-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@wordpress/components';
import { __, sprintf, _n } from '@wordpress/i18n';
import { useMemo, useState, useCallback, useEffect } from '@wordpress/element';
import { useRegistry } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -52,6 +53,15 @@ function ActionWithModal( {
const onCloseModal = useCallback( () => {
setActionWithModal( undefined );
}, [ setActionWithModal ] );
const actionWithCallbacks = useMemo( () => {
return {
...action,
onActionPerformed: ( ...args ) => {
onMenuOpenChange( false );
action.onActionPerformed?.( ...args );
},
};
}, [ action, onMenuOpenChange ] );
return (
<Modal
title={ ! hideModalHeader && action.label }
Expand All @@ -62,13 +72,14 @@ function ActionWithModal( {
<RenderModal
items={ eligibleItems }
closeModal={ onCloseModal }
onPerform={ () => onMenuOpenChange( false ) }
action={ actionWithCallbacks }
/>
</Modal>
);
}

function BulkActionItem( { action, selectedItems, setActionWithModal } ) {
const registry = useRegistry();
const eligibleItems = useMemo( () => {
return selectedItems.filter( ( item ) => action.isEligible( item ) );
}, [ action, selectedItems ] );
Expand All @@ -84,7 +95,14 @@ function BulkActionItem( { action, selectedItems, setActionWithModal } ) {
if ( shouldShowModal ) {
setActionWithModal( action );
} else {
await action.callback( eligibleItems );
const returnResult = await action.callback( eligibleItems );
if ( typeof returnResult === 'function' ) {
await returnResult( {
registry,
select: registry.select,
dispatch: registry.dispatch,
} );
}
}
} }
suffix={
Expand Down
30 changes: 28 additions & 2 deletions packages/dataviews/src/item-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { __ } from '@wordpress/i18n';
import { useMemo, useState } from '@wordpress/element';
import { moreVertical } from '@wordpress/icons';
import { useRegistry } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -71,6 +72,7 @@ function ActionWithModal( { action, item, ActionTrigger } ) {
<RenderModal
items={ [ item ] }
closeModal={ () => setIsModalOpen( false ) }
action={ action }
/>
</Modal>
) }
Expand All @@ -79,6 +81,7 @@ function ActionWithModal( { action, item, ActionTrigger } ) {
}

function ActionsDropdownMenuGroup( { actions, item } ) {
const registry = useRegistry();
return (
<DropdownMenuGroup>
{ actions.map( ( action ) => {
Expand All @@ -96,7 +99,18 @@ function ActionsDropdownMenuGroup( { actions, item } ) {
<DropdownMenuItemTrigger
key={ action.id }
action={ action }
onClick={ () => action.callback( [ item ] ) }
onClick={ async () => {
const returnResult = await action.callback( [
item,
] );
if ( typeof returnResult === 'function' ) {
await returnResult( {
registry,
select: registry.select,
dispatch: registry.dispatch,
} );
}
} }
/>
);
} ) }
Expand All @@ -105,6 +119,7 @@ function ActionsDropdownMenuGroup( { actions, item } ) {
}

export default function ItemActions( { item, actions, isCompact } ) {
const registry = useRegistry();
const { primaryActions, eligibleActions } = useMemo( () => {
// If an action is eligible for all items, doesn't need
// to provide the `isEligible` function.
Expand Down Expand Up @@ -148,7 +163,18 @@ export default function ItemActions( { item, actions, isCompact } ) {
<ButtonTrigger
key={ action.id }
action={ action }
onClick={ () => action.callback( [ item ] ) }
onClick={ async () => {
const returnResult = await action.callback( [
item,
] );
if ( typeof returnResult === 'function' ) {
await returnResult( {
registry,
select: registry.select,
dispatch: registry.dispatch,
} );
}
} }
/>
);
} ) }
Expand Down
61 changes: 37 additions & 24 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import AddNewPageModal from '../add-new-page';
import Media from '../media';
import { unlock } from '../../lock-unlock';

const { usePostActions } = unlock( editorPrivateApis );
const { postActions } = unlock( editorPrivateApis );

const { useLocation, useHistory } = unlock( routerPrivateApis );

Expand Down Expand Up @@ -189,15 +189,15 @@ function FeaturedImage( { item, viewType } ) {
);
}

const PAGE_ACTIONS = [
'edit-post',
'view-post',
'restore',
'permanently-delete',
'view-post-revisions',
'rename-post',
'move-to-trash',
];
const {
editPostAction,
viewPostAction,
restorePostAction,
permanentlyDeletePostAction,
postRevisionsAction,
renamePostAction,
trashPostAction,
} = postActions;

export default function PagePages() {
const postType = 'page';
Expand Down Expand Up @@ -350,20 +350,33 @@ export default function PagePages() {
],
[ authors, view.type ]
);
const onActionPerformed = useCallback(
( actionId, items ) => {
if ( actionId === 'edit-post' ) {
const post = items[ 0 ];
history.push( {
postId: post.id,
postType: post.type,
canvas: 'edit',
} );
}
},
[ history ]
);
const actions = usePostActions( onActionPerformed, PAGE_ACTIONS );

const actions = useMemo( () => {
const onEditPostActionPerformed = ( items ) => {
const post = items[ 0 ];
history.push( {
postId: post.id,
postType: post.type,
canvas: 'edit',
} );
};
return [
{
...editPostAction,
onActionPerformed: ( ...args ) => {
onEditPostActionPerformed( ...args );
editPostAction.onActionPerformed?.( ...args );
},
},
viewPostAction,
restorePostAction,
permanentlyDeletePostAction,
postRevisionsAction,
renamePostAction,
trashPostAction,
];
}, [ history ] );

const onChangeView = useCallback(
( newView ) => {
if ( newView.type !== view.type ) {
Expand Down
33 changes: 16 additions & 17 deletions packages/edit-site/src/components/page-patterns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import { useAddedBy } from '../page-templates/hooks';
const { ExperimentalBlockEditorProvider, useGlobalStyle } = unlock(
blockEditorPrivateApis
);
const { usePostActions } = unlock( editorPrivateApis );
const { postActions } = unlock( editorPrivateApis );
const { useHistory } = unlock( routerPrivateApis );

const EMPTY_ARRAY = [];
Expand Down Expand Up @@ -252,6 +252,8 @@ function Title( { item, categoryId } ) {
);
}

const { editPostAction, postRevisionsAction } = postActions;

export default function DataviewsPatterns() {
const {
categoryType,
Expand Down Expand Up @@ -394,9 +396,9 @@ export default function DataviewsPatterns() {
}, [ patterns, view, fields, type ] );

const history = useHistory();
const onActionPerformed = useCallback(
( actionId, items ) => {
if ( actionId === 'edit-post' ) {
const actions = useMemo( () => {
if ( type === TEMPLATE_PART_POST_TYPE ) {
const onEditPostActionPerformed = ( items ) => {
const post = items[ 0 ];
history.push( {
postId: post.id,
Expand All @@ -405,21 +407,18 @@ export default function DataviewsPatterns() {
categoryType: type,
canvas: 'edit',
} );
}
},
[ history, categoryId, type ]
);
const [ editAction, viewRevisionsAction ] = usePostActions(
onActionPerformed,
[ 'edit-post', 'view-post-revisions' ]
);
const actions = useMemo( () => {
if ( type === TEMPLATE_PART_POST_TYPE ) {
};
return [
editAction,
{
...editPostAction,
onActionPerformed: ( ...args ) => {
onEditPostActionPerformed( ...args );
editPostAction.onActionPerformed?.( ...args );
},
},
renameAction,
duplicateTemplatePartAction,
viewRevisionsAction,
postRevisionsAction,
resetAction,
deleteAction,
];
Expand All @@ -431,7 +430,7 @@ export default function DataviewsPatterns() {
resetAction,
deleteAction,
];
}, [ type, editAction, viewRevisionsAction ] );
}, [ type, history, categoryId ] );
const onChangeView = useCallback(
( newView ) => {
if ( newView.type !== view.type ) {
Expand Down
54 changes: 31 additions & 23 deletions packages/edit-site/src/components/page-templates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
import usePatternSettings from '../page-patterns/use-pattern-settings';
import { unlock } from '../../lock-unlock';

const { usePostActions } = unlock( editorPrivateApis );
const { postActions } = unlock( editorPrivateApis );

const { ExperimentalBlockEditorProvider, useGlobalStyle } = unlock(
blockEditorPrivateApis
Expand Down Expand Up @@ -187,13 +187,13 @@ function Preview( { item, viewType } ) {
);
}

const TEMPLATE_ACTIONS = [
'edit-post',
'reset-template',
'rename-template',
'view-post-revisions',
'delete-template',
];
const {
editPostAction,
resetTemplateAction,
renameTemplateAction,
postRevisionsAction,
deleteTemplateAction,
} = postActions;

export default function PageTemplates() {
const { params } = useLocation();
Expand Down Expand Up @@ -335,21 +335,29 @@ export default function PageTemplates() {
return filterSortAndPaginate( records, view, fields );
}, [ records, view, fields ] );

const onActionPerformed = useCallback(
( actionId, items ) => {
if ( actionId === 'edit-post' ) {
const post = items[ 0 ];
history.push( {
postId: post.id,
postType: post.type,
canvas: 'edit',
} );
}
},
[ history ]
);

const actions = usePostActions( onActionPerformed, TEMPLATE_ACTIONS );
const actions = useMemo( () => {
const onEditPostActionPerformed = ( items ) => {
const post = items[ 0 ];
history.push( {
postId: post.id,
postType: post.type,
canvas: 'edit',
} );
};
return [
{
...editPostAction,
onActionPerformed: ( ...args ) => {
onEditPostActionPerformed( ...args );
editPostAction.onActionPerformed?.( ...args );
},
},
resetTemplateAction,
renameTemplateAction,
postRevisionsAction,
deleteTemplateAction,
];
}, [ history ] );

const onChangeView = useCallback(
( newView ) => {
Expand Down
Loading
Loading