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

DataViews: Update usePostFields to accept postType #67380

Merged
merged 2 commits into from
Nov 28, 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
2 changes: 1 addition & 1 deletion packages/edit-site/src/components/post-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function PostEditForm( { postType, postId } ) {
);
const [ multiEdits, setMultiEdits ] = useState( {} );
const { editEntityRecord } = useDispatch( coreDataStore );
const { fields: _fields } = usePostFields();
const { fields: _fields } = usePostFields( { postType } );
const fields = useMemo(
() =>
_fields?.map( ( field ) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/edit-site/src/components/post-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ export default function PostList( { postType } ) {
return found?.filters ?? [];
};

const { isLoading: isLoadingFields, fields: _fields } = usePostFields();
const { isLoading: isLoadingFields, fields: _fields } = usePostFields( {
postType,
} );
const fields = useMemo( () => {
const activeViewFilters = getActiveViewFilters(
defaultViews,
Expand Down
8 changes: 5 additions & 3 deletions packages/editor/src/components/post-fields/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ interface Author {
name: string;
}

function usePostFields(): UsePostFieldsReturn {
const postType = 'page'; // TODO: this could be page or post (experimental).

function usePostFields( {
postType,
}: {
postType: string;
} ): UsePostFieldsReturn {
const { registerPostTypeSchema } = unlock( useDispatch( editorStore ) );
useEffect( () => {
registerPostTypeSchema( postType );
Expand Down
21 changes: 10 additions & 11 deletions packages/editor/src/dataviews/store/private-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export const registerPostTypeSchema =

const actions = [
postTypeConfig.viewable ? viewPost : undefined,
!! postTypeConfig?.supports?.revisions
!! postTypeConfig.supports?.revisions
? viewPostRevisions
: undefined,
// @ts-ignore
Expand All @@ -148,7 +148,7 @@ export const registerPostTypeSchema =
? duplicatePattern
: undefined,
postTypeConfig.supports?.title ? renamePost : undefined,
postTypeConfig?.supports?.[ 'page-attributes' ]
postTypeConfig.supports?.[ 'page-attributes' ]
? reorderPage
: undefined,
postTypeConfig.slug === 'wp_block' ? exportPattern : undefined,
Expand All @@ -157,25 +157,24 @@ export const registerPostTypeSchema =
deletePost,
trashPost,
permanentlyDeletePost,
];
].filter( Boolean );

const fields = [
featuredImageField,
postTypeConfig.supports?.thumbnail &&
currentTheme?.[ 'theme-supports' ]?.[ 'post-thumbnails' ] &&
featuredImageField,
titleField,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I intentionally didn't add the post type supports for title here, as I need to investigate better how core lists (wp-admin) handles it and what we should do here.

Additionally it relates to the issue about layout fields I mentioned in the PR's description. Some layouts declare this as primary field.

Copy link
Member

@oandregal oandregal Nov 28, 2024

Choose a reason for hiding this comment

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

Can you add a TODO in the code so we don't forget? Let's ship this.

authorField,
postTypeConfig.supports?.author && authorField,
statusField,
dateField,
slugField,
Copy link
Contributor

Choose a reason for hiding this comment

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

This one has a support key too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The slug?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, you can check the different <PostTypeSupportCheck calls we have to find these.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm.. the docs for supports don't say that.. Are you sure there is such support?

I saw we have a <PostTypeSupportCheck supportKeys="slug"> check, but I don't think it ever worked properly - unless there was such support in the past. Also I think the above code might not being used anywhere by quickly adding a breakpoint there. I'll check more to see if we can remove that code.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you're right :)

parentField,
commentStatusField,
postTypeConfig.supports?.[ 'page-attributes' ] && parentField,
postTypeConfig.supports?.comments && commentStatusField,
passwordField,
];
].filter( Boolean );

registry.batch( () => {
actions.forEach( ( action ) => {
if ( ! action ) {
return;
}
unlock( registry.dispatch( editorStore ) ).registerEntityAction(
'postType',
postType,
Expand Down
3 changes: 3 additions & 0 deletions packages/editor/src/dataviews/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ export interface PostType {
'page-attributes'?: boolean;
title?: boolean;
revisions?: boolean;
thumbnail?: boolean;
comments?: boolean;
author?: boolean;
};
}

Expand Down
Loading