-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add pagination support to usePromise
, useCachedPromise
and useFetch
.
#25
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, the only thing missing is the peerDependency update in the package.json (which I assume you'll have to wait until the release?)
ah you're missing the doc updates as well |
yeah we'll need to wait merging this until 1.69.0 is out. But I can update the dependencies already in both the utils and the tests, as well as the version. The bigger leftover is the docs – working on that now. 😸 |
Context
We're working on adding pagination support to
Grid
andList
via a newpagination
prop. To support users in more easily taking advantage of pagination, this PR updatesusePromise
,useCachedPromise
anduseFetch
to have pagination support. This means a couple of things:pagination: { hasMore: boolean, pageSize: number, onLoadMore: () => void }
, which can be directly passed to theList
orGrid
componentChanges
For
usePromise
anduseCachedPromise
the hooks' first argument can be not just a function that returns a promise, but a function that returns a paginated promise. A paginated promise in this case is a function that receives pagination info and returns a promise. It means going from
to
Particular to
useCachedPromise
: when paginating, only the first page for each pagination instance is cached.For
useFetch
the hook's first argument can be not just a
RequestInfo
, but a paginated request info.Similar to above, this means going from:
to
The main difference between
usePromise
/useCachedPromise
anduseFetch
is that the latter requires amapResult
option to be passed, in addition to the paginated request info, in order to tell the hook whether it should try to paginate further, and to have a chance to "process" the data returned by the API, in case e.g. the API returns something like{ success: boolean, results: Item[] }
.Other changes
In order to support these changes, I've had to make a couple more changes:
onData
now receives not just thedata
, but also a second argument: the pagination options.page
, latesthasMore
,lastItem
and the data accumulated from the current pagination instance, it's all stored inusePromise
. However, becauseuseCachedPromise
only caches the first page, we needed a way to figure out what page we're currently at.useCachedPromise
anduseFetch
accept acacheKeySuffix
option, which, as the name implies will be added to the cacheKey thatuseCachedPromise
already generates internally. BecauseuseFetch
builds on top ofuseCachedPromise
, and becauseuseCachedPromise
uses the function & the args to generate the cache key – this wasn't enough to differentiate, when using e.g.useFetch((searchText: string) => async (options) => "https://example.com/?page= " + options.page + "&searchText=" + searchText, [searchText])
, betweensearchText
being empty and not, i.e.:Screen.Recording.2024-03-04.at.15.00.05.mov
Screenshots
usePromise:
Screen.Recording.2024-03-04.at.14.59.01.mov
useCachedPromise:
Screen.Recording.2024-03-04.at.14.59.36.mov
useFetch:
Screen.Recording.2024-03-04.at.15.07.00.mov