-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: oandregal <[email protected]> Co-authored-by: youknowriad <[email protected]> Co-authored-by: ntsekouras <[email protected]>
- Loading branch information
1 parent
a750125
commit d8e0b13
Showing
9 changed files
with
74 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo } from '@wordpress/element'; | ||
import { useEntityRecords } from '@wordpress/core-data'; | ||
import type { Field } from '@wordpress/dataviews'; | ||
import { | ||
featuredImageField, | ||
slugField, | ||
parentField, | ||
passwordField, | ||
statusField, | ||
commentStatusField, | ||
titleField, | ||
dateField, | ||
authorField, | ||
} from '@wordpress/fields'; | ||
import type { BasePostWithEmbeddedAuthor } from '@wordpress/fields'; | ||
|
||
interface UsePostFieldsReturn { | ||
isLoading: boolean; | ||
fields: Field< BasePostWithEmbeddedAuthor >[]; | ||
} | ||
|
||
interface Author { | ||
id: number; | ||
name: string; | ||
} | ||
|
||
function usePostFields(): UsePostFieldsReturn { | ||
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< BasePostWithEmbeddedAuthor >[], | ||
[ authors ] | ||
); | ||
|
||
return { | ||
isLoading: isLoadingAuthors, | ||
fields, | ||
}; | ||
} | ||
|
||
/** | ||
* Hook to get the fields for a post (BasePost or BasePostWithEmbeddedAuthor). | ||
*/ | ||
export default usePostFields; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './fields'; | ||
export * from './actions'; | ||
export type * from './types'; |