Skip to content

Commit

Permalink
renaming getPost to getPostType. Updating docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mogmarsh committed Apr 19, 2023
1 parent 7c620ca commit 41674a6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ results, and a custom function for fetching post data given a post ID.
|----------------|----------------|----------|----------|-----------------------------------------------------------------------------------------------------------------|
| allowedTypes | `[]` | No | array | Array with the post types to select from. Defaults to empty array (all types). |
| className | `''` | No | string | Class name for the post picker container. |
| getPost | | No | function | Function to retrieve the post data for a post ID. Defaults to using getPostById. |
| getPostType | | No | function | Function to retrieve the post type for a post ID. This function must return an object containing a string named `subtype` that is the post type. |
| onReset | | Yes | function | Function to reset the post ID to 0. |
| onUpdate | | Yes | function | Function to set the post ID on post selection. |
| params | `{}` | No | object | Optional key value pairs to append to the search request. Ex: `{ per_page: 20 }`. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import SearchModal from './search-modal';
interface PostPickerProps {
allowedTypes?: string[];
className?: string;
getPost?: (id: number) => object | WP_REST_API_Post;
getPostType?: (id: number) => object | WP_REST_API_Post;
onReset: () => void;
onUpdate: (id: number) => void;
params?: object;
Expand Down Expand Up @@ -45,7 +45,7 @@ const Preview = styled.div`
const PostPicker = ({
allowedTypes,
className,
getPost,
getPostType,
onReset,
onUpdate,
params = {},
Expand All @@ -67,7 +67,7 @@ const PostPicker = ({
);

// @ts-ignore
const post = usePostById(value, getPost) as any as WP_REST_API_Post;
const post = usePostById(value, getPostType) as any as WP_REST_API_Post;

const {
featured_media: featuredMediaId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ const MyBlock = ({
}
};
```

This function must return an object which contains a string named `subtype` that is the post type.
9 changes: 5 additions & 4 deletions packages/block-editor-tools/src/hooks/use-post-by-id/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import usePost from '../use-post';
* looked up from the search endpoint, cached, and then passed to usePost.
*
* @param int postId The ID for the post to return.
* @param function getPost Optional custom function that returns a post object.
* @param function getPostType Optional custom function that returns a post object.
* This object must contain a subtype property.
* @returns {object} An object containing a hasResolved property
* and the returned post object.
*/
const usePostById = (postId, getPost = null) => {
const usePostById = (postId, getPostType = null) => {
const [postTypeCache, setPostTypeCache] = useState({});

useEffect(() => {
if (postId && !postTypeCache[postId]) {
(async () => {
if (getPost) {
const result = await getPost(postId);
if (getPostType) {
const result = await getPostType(postId);
if (!result) {
// eslint-disable-next-line no-console
console.error(`Custom function to get post with ID ${postId} failed.`);
Expand Down

0 comments on commit 41674a6

Please sign in to comment.