@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
62 lines (61 loc) • 1.45 kB
JavaScript
/**
* WordPress dependencies
*/
import { useEffect, useMemo } from '@wordpress/element';
import { useEntityRecords } from '@wordpress/core-data';
import { useDispatch, useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import { store as editorStore } from '../../store';
function usePostFields({
postType
}) {
const {
registerPostTypeSchema
} = unlock(useDispatch(editorStore));
useEffect(() => {
registerPostTypeSchema(postType);
}, [registerPostTypeSchema, postType]);
const {
defaultFields
} = useSelect(select => {
const {
getEntityFields
} = unlock(select(editorStore));
return {
defaultFields: getEntityFields('postType', postType)
};
}, [postType]);
const {
records: authors,
isResolving: isLoadingAuthors
} = useEntityRecords('root', 'user', {
per_page: -1
});
const fields = useMemo(() => defaultFields.map(field => {
if (field.id === 'author') {
return {
...field,
elements: authors?.map(({
id,
name
}) => ({
value: id,
label: name
}))
};
}
return field;
}), [authors, defaultFields]);
return {
isLoading: isLoadingAuthors,
fields
};
}
/**
* Hook to get the fields for a post (BasePost or BasePostWithEmbeddedAuthor).
*/
export default usePostFields;
//# sourceMappingURL=index.js.map