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

Move usePostFields to wordpress/editor package #67024

Merged
merged 4 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -13,12 +13,12 @@ import { store as coreDataStore } from '@wordpress/core-data';
import { __experimentalVStack as VStack } from '@wordpress/components';
import { useState, useMemo, useEffect } from '@wordpress/element';
import { privateApis as editorPrivateApis } from '@wordpress/editor';
import { usePostFields } from '@wordpress/fields';

/**
* Internal dependencies
*/
import Page from '../page';
import usePostFields from '../post-fields';
import { unlock } from '../../lock-unlock';

const { PostCardPanel } = unlock( editorPrivateApis );
Expand Down
50 changes: 0 additions & 50 deletions packages/edit-site/src/components/post-fields/index.js

This file was deleted.

3 changes: 0 additions & 3 deletions packages/edit-site/src/components/post-fields/style.scss

This file was deleted.

2 changes: 1 addition & 1 deletion packages/edit-site/src/components/post-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { DataViews, filterSortAndPaginate } from '@wordpress/dataviews';
import { privateApis as editorPrivateApis } from '@wordpress/editor';
import { __ } from '@wordpress/i18n';
import { drawerRight } from '@wordpress/icons';
import { usePostFields } from '@wordpress/fields';

/**
* Internal dependencies
Expand All @@ -32,7 +33,6 @@ import AddNewPostModal from '../add-new-post';
import { unlock } from '../../lock-unlock';
import { useEditPostAction } from '../dataviews-actions';
import { usePrevious } from '@wordpress/compose';
import usePostFields from '../post-fields';

const { usePostActions } = unlock( editorPrivateApis );
const { useLocation, useHistory } = unlock( routerPrivateApis );
Expand Down
5 changes: 4 additions & 1 deletion packages/edit-site/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
@import "./components/editor-canvas-container/style.scss";
@import "./components/post-edit/style.scss";
@import "./components/post-list/style.scss";
@import "./components/post-fields/style.scss";
@import "./components/resizable-frame/style.scss";
@import "./hooks/push-changes-to-global-styles/style.scss";
@import "./components/global-styles/font-library-modal/style.scss";
Expand Down Expand Up @@ -94,3 +93,7 @@ body.js.site-editor-php {
}

@include wordpress-admin-schemes();

.components-popover.components-dropdown__content.dataforms-layouts-panel__field-dropdown {
oandregal marked this conversation as resolved.
Show resolved Hide resolved
z-index: z-index(".components-popover.components-dropdown__content.dataforms-layouts-panel__field-dropdown");
}
4 changes: 4 additions & 0 deletions packages/fields/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ Undocumented declaration.

Undocumented declaration.

### usePostFields

Hook to get the fields for a post (BasePost or BasePostWithEmbeddedAuthor).

### viewPost

Undocumented declaration.
Expand Down
1 change: 1 addition & 0 deletions packages/fields/src/fields/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export { default as statusField } from './status';
export { default as commentStatusField } from './comment-status';
export { default as dateField } from './date';
export { default as authorField } from './author';
export { default as usePostFields } from './use-post-fields';
69 changes: 69 additions & 0 deletions packages/fields/src/fields/use-post-fields/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { useEntityRecords } from '@wordpress/core-data';
import type { Field } from '@wordpress/dataviews';

/**
* Internal dependencies
*/
import featuredImageField from '../featured-image';
import slugField from '../slug';
import parentField from '../parent';
import passwordField from '../password';
import statusField from '../status';
import commentStatusField from '../comment-status';
import titleField from '../title';
import dateField from '../date';
import authorField from '../author';
import type { BasePost, BasePostWithEmbeddedAuthor } from '../../types';

type Post = BasePost | BasePostWithEmbeddedAuthor;

interface UsePostFieldsReturn {
isLoading: boolean;
fields: Field< Post >[];
}

interface Author {
id: number;
name: string;
}

function usePostFields(): UsePostFieldsReturn {
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of keeping the list of fields here, should we transform this to a "registration API" like actions and start opening the API in Gutenberg? Separate thing obviously, but just want to start this discussion.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is actually a relevant question for this PR: usePostActions is defined in the editor package; but I'm operating under the assumption that we want both to live in wordpress/fields at some point, that's why I'm moving it here. Is that your thinking as well?

After that, yeah, field registration would be nice. Probably private to Gutenberg for now?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah usePostActions or more precisely for me getEntityActions selector lives in the editor package, mostly because creating new stores is not an easy task and also shouldn't be done at all for bundled packages. So temporarily, the store lives in the editor package. I think we should ultimately do the same for fields.

And when we decide to make things more public (make the fields package non bundled), we should probably move the store to the fields package.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah usePostActions or more precisely for me getEntityActions selector lives in the editor package, mostly because creating new stores is not an easy task and also shouldn't be done at all for bundled packages. So temporarily, the store lives in the editor package. I think we should ultimately do the same for fields.

ok, so this means that, for now, we should move usePostFields to the editor package in preparation for adding extensibility. Later, when fields is public will move both to this package.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's a good plan. Do you think we should also add the postType as param here or in a follow up?

Copy link
Member Author

Choose a reason for hiding this comment

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

Moved to editor at 1b76327

That's a good plan. Do you think we should also add the postType as param here or in a follow up?

It's best to do it when we add support for other post types.

Copy link
Member Author

Choose a reason for hiding this comment

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

#67175 to pull the fields from the registry

const { records: authors, isResolving: isLoadingAuthors } =
useEntityRecords< Author >( 'root', 'user', { per_page: -1 } );

const fields = useMemo(
() =>
[
featuredImageField,
titleField,
{
...authorField,
elements: authors?.map( ( { id, name } ) => ( {
value: id,
label: name,
} ) ),
},
statusField,
dateField,
slugField,
parentField,
commentStatusField,
passwordField,
] as Field< Post >[],
oandregal marked this conversation as resolved.
Show resolved Hide resolved
[ authors ]
);

return {
isLoading: isLoadingAuthors,
fields,
};
}

/**
* Hook to get the fields for a post (BasePost or BasePostWithEmbeddedAuthor).
*/
export default usePostFields;
Loading