-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Post Type Actions: Unify the list of available actions #61520
Changes from all commits
1c04332
1364cc5
3527dda
4b6b24f
6ac0c80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { edit } from '@wordpress/icons'; | ||
import { useMemo } from '@wordpress/element'; | ||
import { privateApis as routerPrivateApis } from '@wordpress/router'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { unlock } from '../../lock-unlock'; | ||
|
||
const { useHistory } = unlock( routerPrivateApis ); | ||
|
||
export const useEditPostAction = () => { | ||
const history = useHistory(); | ||
return useMemo( | ||
() => ( { | ||
id: 'edit-post', | ||
label: __( 'Edit' ), | ||
isPrimary: true, | ||
icon: edit, | ||
isEligible( { status } ) { | ||
return status !== 'trash'; | ||
}, | ||
callback( items ) { | ||
const post = items[ 0 ]; | ||
history.push( { | ||
postId: post.id, | ||
postType: post.type, | ||
canvas: 'edit', | ||
} ); | ||
}, | ||
} ), | ||
[ history ] | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,6 @@ import { | |
} from '../../utils/constants'; | ||
import { | ||
exportJSONaction, | ||
renameAction, | ||
resetAction, | ||
deleteAction, | ||
duplicatePatternAction, | ||
|
@@ -60,12 +59,13 @@ import usePatterns from './use-patterns'; | |
import PatternsHeader from './header'; | ||
import { useLink } from '../routes/link'; | ||
import { useAddedBy } from '../page-templates/hooks'; | ||
import { useEditPostAction } from '../dataviews-actions'; | ||
|
||
const { ExperimentalBlockEditorProvider, useGlobalStyle } = unlock( | ||
blockEditorPrivateApis | ||
); | ||
const { usePostActions } = unlock( editorPrivateApis ); | ||
const { useHistory, useLocation } = unlock( routerPrivateApis ); | ||
const { useLocation } = unlock( routerPrivateApis ); | ||
|
||
const EMPTY_ARRAY = []; | ||
const defaultConfigPerViewType = { | ||
|
@@ -375,45 +375,29 @@ export default function DataviewsPatterns() { | |
return filterSortAndPaginate( patterns, viewWithoutFilters, fields ); | ||
}, [ patterns, view, fields, type ] ); | ||
|
||
const history = useHistory(); | ||
const onActionPerformed = useCallback( | ||
( actionId, items ) => { | ||
if ( actionId === 'edit-post' ) { | ||
const post = items[ 0 ]; | ||
history.push( { | ||
postId: post.id, | ||
postType: post.type, | ||
categoryId, | ||
categoryType: type, | ||
canvas: 'edit', | ||
} ); | ||
} | ||
}, | ||
[ history, categoryId, type ] | ||
); | ||
const [ editAction, viewRevisionsAction ] = usePostActions( | ||
onActionPerformed, | ||
[ 'edit-post', 'view-post-revisions' ] | ||
); | ||
const templatePartActions = usePostActions( TEMPLATE_PART_POST_TYPE ); | ||
const patternActions = usePostActions( PATTERN_TYPES.user ); | ||
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'm ever more convinced we should drop these constants across patterns, dataviews, etc. It's easy to waste 30 seconds, which become 1.5 minutes with the ensuing distractions, especially when the LSP can't trace the identifier to its type/definition due to the opaque 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. Maybe open a PR and see what folks say about it :) |
||
const editAction = useEditPostAction(); | ||
|
||
const actions = useMemo( () => { | ||
if ( type === TEMPLATE_PART_POST_TYPE ) { | ||
return [ | ||
editAction, | ||
renameAction, | ||
...templatePartActions, | ||
duplicateTemplatePartAction, | ||
viewRevisionsAction, | ||
resetAction, | ||
deleteAction, | ||
]; | ||
].filter( Boolean ); | ||
} | ||
return [ | ||
renameAction, | ||
editAction, | ||
...patternActions, | ||
duplicatePatternAction, | ||
exportJSONaction, | ||
resetAction, | ||
deleteAction, | ||
]; | ||
}, [ type, editAction, viewRevisionsAction ] ); | ||
].filter( Boolean ); | ||
}, [ editAction, type, templatePartActions, patternActions ] ); | ||
const onChangeView = useCallback( | ||
( newView ) => { | ||
if ( newView.type !== view.type ) { | ||
|
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.
All of these actions need to be moved to the editor/post-actions hook rather than being specific to dataviews. I'm not doing that in this PR though.