You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The post picker modal opens and runs a search, which fires a REST request to the search endpoint (e.g. /wp-json/wp/v2/search?type=post&subtype%5B0%5D=post&subtype%5B1%5D=page&page=1&_embed=1&exclude=&_locale=user
The user selects a post
The post picker uses usePostById() which will then take the post ID, and run another REST API search to get the post type (e.g. /wp-json/wp/v2/search?include=150&_locale=user)
usePostById() uses usePost(), which calls getEntityRecord(), which will then make a REST API request to get the post object (e.g. /wp-json/wp/v2/posts/150?context=edit&_locale=user).
The preview of the post is rendered, by default with the thumbnail, title, and post type. However, the previewRender prop could be passed to render more data.
In most cases, the data we get back from the search in step 2 provides everything we need throughout the rest of the process, and the requests in step 4 and 5 are unnecessary. At the absolute least, the request in step 5 should be unnecessary during this flow, as the post type is included in the response in step 2.
What can we do to refactor this code to make this process more efficient?
Use Case
Normal use of the post picker component.
The text was updated successfully, but these errors were encountered:
We should be able to cache data returned by the picker modal so that it doesn't need to follow up for more info. usePostById is necessary when rendering the field without first picking it from the modal, because we're only storing the post ID, and we need to get info about the post based on the ID without knowing its type (which the API requires in order to fetch a full object).
Other thoughts:
Can we use just what's returned from the search endpoint generally? That could potentially work for both a situation where the post is selected from the picker and for where we only have the ID - maybe we don't need to fetch the full entity record for a post at all.
If we're using a hook like usePostById we need to obey the rules of hooks, so we may need to get creative with how we're storing information to prevent additional requests.
Description
In the normal flow of the post picker,
/wp-json/wp/v2/search?type=post&subtype%5B0%5D=post&subtype%5B1%5D=page&page=1&_embed=1&exclude=&_locale=user
usePostById()
which will then take the post ID, and run another REST API search to get the post type (e.g./wp-json/wp/v2/search?include=150&_locale=user
)usePostById()
usesusePost()
, which callsgetEntityRecord()
, which will then make a REST API request to get the post object (e.g./wp-json/wp/v2/posts/150?context=edit&_locale=user
).previewRender
prop could be passed to render more data.In most cases, the data we get back from the search in step 2 provides everything we need throughout the rest of the process, and the requests in step 4 and 5 are unnecessary. At the absolute least, the request in step 5 should be unnecessary during this flow, as the post type is included in the response in step 2.
What can we do to refactor this code to make this process more efficient?
Use Case
Normal use of the post picker component.
The text was updated successfully, but these errors were encountered: