Skip to content

Commit

Permalink
DataViews: Update usePostFields to accept postType (WordPress#67380)
Browse files Browse the repository at this point in the history
Co-authored-by: ntsekouras <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: oandregal <[email protected]>
  • Loading branch information
4 people authored and im3dabasia committed Dec 4, 2024
1 parent b101e07 commit 61b73d7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
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,
authorField,
postTypeConfig.supports?.author && authorField,
statusField,
dateField,
slugField,
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

0 comments on commit 61b73d7

Please sign in to comment.