Skip to content

Add X-WP-Total response to getEntityRecords() #12083

Closed

Description

@rwrobe

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.

Activity

  1. added
    [Type] Help Request
    Needs Technical Feedback
    on Nov 20, 2018
  2. designsimply commented on Jan 31, 2019

    @designsimply
    Member

    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?

  3. aduth commented on Mar 20, 2019

    @aduth
    Member

    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.

  4. chrisvanpatten commented on Apr 27, 2019

    @chrisvanpatten
    Contributor

    @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.)

  5. aduth commented on Apr 30, 2019

    @aduth
    Member

    @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).

  6. mediacs commented on Aug 1, 2019

    @mediacs

    @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.

  7. chrisvanpatten commented on Aug 1, 2019

    @chrisvanpatten
    Contributor

    @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.

  8. mediacs commented on Aug 2, 2019

    @mediacs

    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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs Technical Feedback[Type] Help Request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions