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
{{ message }}
Repository navigation
Add X-WP-Total response to getEntityRecords() #12083
Is your feature request related to a problem? Please describe.
When grabbing posts for a post picker modal, it's impossible to know whether you will need a pager, or to populate that pager. All I need is a quick way to get the max post count or page count from the getEntityRecords query.
Describe the solution you'd like
I'm not sure how the result would output, given that the entity record come out as an array. Perhaps, the result of the select will be an object with max post count and the posts as a child.
First please note that I am a beginner when it comes to JavaScript. To solve this, could you do a getEntityRecords query and then do something like entities.length?
I also tried researching this and found that #6780 mentions wanting to introduce a findAll/findQuery and I was wondering if the PR attached to that issue helps you?
While it's not precisely what you requested, as of #10762, the default behavior of fetching with a query containing per_page=-1 will be to automatically paginate results on your behalf. getEntityRecords will always return the data available at a given time, and could be used in combination with subscribe and select( 'core/data' ).isResolving( 'core', 'getEntityRecords', query ); to determine if requests are still pending.
Going to proactively close the issue as resolved, but let's reopen if there's follow-up questions.
@aduth I'm explicitly looking to implement manual pagination over a set of entities — is it fair to say that the only path forward is a custom implementation with apiFetch?
(That is to say — I can paginate over the data by passing around a page value in state, but there's no way to calculate how many entities are coming in, so you can calculate the maximum number of pages.)
@chrisvanpatten Yes, for the moment I think you'd need to do something custom. I could imagine an iteration on core-data to allow data to progressively be made available as individual requests are received, at which point a selector could exist to indicate a total number before the full set is known. But it's unclear whether it's desirable (data suddenly appearing can be jarring) and challenging to implement (considering isResolved state in a multi-page request).
@chrisvanpatten, any luck with the pagination? I think I'm stuck struggeling with the same thing, while trying to add pagination to the latest posts block.
@mediacs You just have to wrap your component with the withState HOC (or possibly useState although I'm less familiar with the hooks implementation) and then just use a button or something to change a page variable which you pass to getEntityRecords as a query arg.
Then ultimately I ended up just doing a check where essentially if ( results.length < queryArgs.posts_per_page ) it would render the "Next" button as deactivated. It's a hack but seemed to work.
Thanks for the reply, I tried to figure out what you are saying ... I hope when I get more progressed in React/Redux I will understand your answer. I feel the HOC / withSelect thing is too complicated for me now .
I think I will first try a different approach like bigbite's example, where there is no withSelect HOC involved: https://bigbite.net/insights/creating-advanced-block-gutenberg/#pagination
Looks like something I can get a grip on...
Activity
designsimply commentedon Jan 31, 2019
First please note that I am a beginner when it comes to JavaScript. To solve this, could you do a
getEntityRecordsquery and then do something likeentities.length?I also tried researching this and found that #6780 mentions wanting to introduce a
findAll/findQueryand I was wondering if the PR attached to that issue helps you?aduth commentedon Mar 20, 2019
While it's not precisely what you requested, as of #10762, the default behavior of fetching with a query containing
per_page=-1will be to automatically paginate results on your behalf.getEntityRecordswill always return the data available at a given time, and could be used in combination withsubscribeandselect( 'core/data' ).isResolving( 'core', 'getEntityRecords', query );to determine if requests are still pending.Going to proactively close the issue as resolved, but let's reopen if there's follow-up questions.
chrisvanpatten commentedon Apr 27, 2019
@aduth I'm explicitly looking to implement manual pagination over a set of entities — is it fair to say that the only path forward is a custom implementation with
apiFetch?(That is to say — I can paginate over the data by passing around a
pagevalue in state, but there's no way to calculate how many entities are coming in, so you can calculate the maximum number of pages.)aduth commentedon Apr 30, 2019
@chrisvanpatten Yes, for the moment I think you'd need to do something custom. I could imagine an iteration on
core-datato allow data to progressively be made available as individual requests are received, at which point a selector could exist to indicate a total number before the full set is known. But it's unclear whether it's desirable (data suddenly appearing can be jarring) and challenging to implement (consideringisResolvedstate in a multi-page request).mediacs commentedon Aug 1, 2019
@chrisvanpatten, any luck with the pagination? I think I'm stuck struggeling with the same thing, while trying to add pagination to the latest posts block.
chrisvanpatten commentedon Aug 1, 2019
@mediacs You just have to wrap your component with the
withStateHOC (or possiblyuseStatealthough I'm less familiar with the hooks implementation) and then just use a button or something to change apagevariable which you pass togetEntityRecordsas a query arg.Then ultimately I ended up just doing a check where essentially
if ( results.length < queryArgs.posts_per_page )it would render the "Next" button as deactivated. It's a hack but seemed to work.mediacs commentedon Aug 2, 2019
Thanks for the reply, I tried to figure out what you are saying ... I hope when I get more progressed in React/Redux I will understand your answer. I feel the HOC / withSelect thing is too complicated for me now .
I think I will first try a different approach like bigbite's example, where there is no withSelect HOC involved:
https://bigbite.net/insights/creating-advanced-block-gutenberg/#pagination
Looks like something I can get a grip on...