-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Update bulk header with actions #67743
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,38 +20,57 @@ import { usePostActions } from './actions'; | |
|
||
const { Menu, kebabCase } = unlock( componentsPrivateApis ); | ||
|
||
export default function PostActions( { postType, postId, onActionPerformed } ) { | ||
const [ activeModalAction, setActiveModalAction ] = useState( null ); | ||
const { item, permissions } = useSelect( | ||
function useEditedEntityRecordsWithPermissions( postType, postIds ) { | ||
const { items, permissions } = useSelect( | ||
( select ) => { | ||
const { getEditedEntityRecord, getEntityRecordPermissions } = | ||
unlock( select( coreStore ) ); | ||
return { | ||
item: getEditedEntityRecord( 'postType', postType, postId ), | ||
permissions: getEntityRecordPermissions( | ||
'postType', | ||
postType, | ||
postId | ||
items: postIds.map( ( postId ) => | ||
getEditedEntityRecord( 'postType', postType, postId ) | ||
), | ||
permissions: postIds.map( ( postId ) => | ||
getEntityRecordPermissions( 'postType', postType, postId ) | ||
Comment on lines
+29
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change has a couple of issues:
You can fetch permissions for multiple entities using ScreenshotThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @Mamaduka, will push up a fix for this today. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Followed up in this PR: #67872 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, @louwie17; I'll have a look later today or tomorrow. |
||
), | ||
}; | ||
}, | ||
[ postId, postType ] | ||
[ postIds, postType ] | ||
); | ||
const itemWithPermissions = useMemo( () => { | ||
return { | ||
|
||
return useMemo( () => { | ||
return items.map( ( item, index ) => ( { | ||
...item, | ||
permissions, | ||
}; | ||
}, [ item, permissions ] ); | ||
permissions: permissions[ index ], | ||
} ) ); | ||
}, [ items, permissions ] ); | ||
} | ||
|
||
export default function PostActions( { postType, postId, onActionPerformed } ) { | ||
const [ activeModalAction, setActiveModalAction ] = useState( null ); | ||
const _postIds = useMemo( () => { | ||
if ( Array.isArray( postId ) ) { | ||
return postId; | ||
} | ||
return postId ? [ postId ] : []; | ||
}, [ postId ] ); | ||
|
||
const itemsWithPermissions = useEditedEntityRecordsWithPermissions( | ||
postType, | ||
_postIds | ||
); | ||
const allActions = usePostActions( { postType, onActionPerformed } ); | ||
|
||
const actions = useMemo( () => { | ||
return allActions.filter( ( action ) => { | ||
return ( | ||
! action.isEligible || action.isEligible( itemWithPermissions ) | ||
( ! action.isEligible || | ||
itemsWithPermissions.some( ( itemWithPermissions ) => | ||
action.isEligible( itemWithPermissions ) | ||
) ) && | ||
( itemsWithPermissions.length < 2 || action.supportsBulk ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remark: Some of this logic and the UI component itself to render the actions dropdown... is already present in the DataViews package. It seems like instead of duplicating all of that, we should be exposing a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes that would make sense, as there is a lot of duplication. But it appears this was also done on purpose, given this inline comment:
Should we still go ahead with this, or is there a specific reason these packages can't depend on each other? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why the editor shouldn't be using dataviews package. (Regardless, this should be a follow-up and not block this PR from shipping) |
||
); | ||
} ); | ||
}, [ allActions, itemWithPermissions ] ); | ||
}, [ allActions, itemsWithPermissions ] ); | ||
|
||
return ( | ||
<> | ||
|
@@ -70,14 +89,14 @@ export default function PostActions( { postType, postId, onActionPerformed } ) { | |
> | ||
<ActionsDropdownMenuGroup | ||
actions={ actions } | ||
item={ itemWithPermissions } | ||
items={ itemsWithPermissions } | ||
setActiveModalAction={ setActiveModalAction } | ||
/> | ||
</Menu> | ||
{ !! activeModalAction && ( | ||
<ActionModal | ||
action={ activeModalAction } | ||
items={ [ item ] } | ||
items={ itemsWithPermissions } | ||
closeModal={ () => setActiveModalAction( null ) } | ||
/> | ||
) } | ||
|
@@ -119,7 +138,7 @@ export function ActionModal( { action, items, closeModal } ) { | |
); | ||
} | ||
|
||
function ActionsDropdownMenuGroup( { actions, item, setActiveModalAction } ) { | ||
function ActionsDropdownMenuGroup( { actions, items, setActiveModalAction } ) { | ||
const registry = useRegistry(); | ||
return ( | ||
<Menu.Group> | ||
|
@@ -133,9 +152,9 @@ function ActionsDropdownMenuGroup( { actions, item, setActiveModalAction } ) { | |
setActiveModalAction( action ); | ||
return; | ||
} | ||
action.callback( [ item ], { registry } ); | ||
action.callback( items, { registry } ); | ||
} } | ||
items={ [ item ] } | ||
items={ items } | ||
/> | ||
); | ||
} ) } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved this logic to its own hook. It gets all the edited records and adds permissions.
It's similar to this hook:
gutenberg/packages/core-data/src/hooks/use-entity-records.ts
Line 158 in 1e28ca4
edited
records specifically.Would it be worth moving to the core-data package as well?