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

Patterns page: add edit & view revision actions to parts #60659

Merged
merged 3 commits into from
Apr 11, 2024
Merged
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
44 changes: 38 additions & 6 deletions packages/edit-site/src/components/page-patterns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {
} from '@wordpress/icons';
import { usePrevious } from '@wordpress/compose';
import { useEntityRecords } from '@wordpress/core-data';
import { privateApis as editorPrivateApis } from '@wordpress/editor';
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
* Internal dependencies
Expand Down Expand Up @@ -71,6 +73,8 @@ import { useAddedBy } from '../page-templates-template-parts/hooks';
const { ExperimentalBlockEditorProvider, useGlobalStyle } = unlock(
blockEditorPrivateApis
);
const { usePostActions } = unlock( editorPrivateApis );
const { useHistory } = unlock( routerPrivateApis );

const templatePartIcons = { header, footer, uncategorized };
const EMPTY_ARRAY = [];
Expand Down Expand Up @@ -439,17 +443,45 @@ export default function DataviewsPatterns() {
return filterSortAndPaginate( patterns, viewWithoutFilters, fields );
}, [ patterns, view, fields, type ] );

const actions = useMemo(
() => [
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 ]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing deps 'categoryId' and 'type', or was it intentional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, thanks for raising, fix at #60784

);
const [ editAction, viewRevisionsAction ] = usePostActions(
onActionPerformed,
[ 'edit-post', 'view-post-revisions' ]
);
const actions = useMemo( () => {
if ( type === TEMPLATE_PART_POST_TYPE ) {
return [
editAction,
Copy link
Member Author

@oandregal oandregal Apr 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've looked into making the editAction also available to user patterns. I haven't done so in this PR because the edit action eligibility check is post type agnostic and lives in the @wordpress/editor package.

If there's interest I can look into that separately. We'd need to either exclude theme patterns from there or create a specific edit action for user patterns in the site editor package.

renameAction,
duplicateTemplatePartAction,
viewRevisionsAction,
resetAction,
deleteAction,
];
}
return [
renameAction,
duplicatePatternAction,
duplicateTemplatePartAction,
exportJSONaction,
resetAction,
deleteAction,
],
[]
);
];
}, [ type, editAction, viewRevisionsAction ] );
const onChangeView = useCallback(
( newView ) => {
if ( newView.type !== view.type ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const templatePartToPattern = ( templatePart ) => ( {
name: createTemplatePartId( templatePart.theme, templatePart.slug ),
title: decodeEntities( templatePart.title.rendered ),
type: templatePart.type,
_links: templatePart._links,
templatePart,
} );

Expand Down
Loading