CursorPaginator
SourceCursor based paginator
Expects a nextCursor
, previousCursor
and optional pageSize
key in the response. See
getPaginationState for how to customise this if your backend implementation
differs.
API
Constructor
new CursorPaginator(currentStatePair,internalStatePair)
SourceParameter | Type | Description | |
---|---|---|---|
* | currentStatePair | null | |
* | internalStatePair | null |
Methods
firstState()
SourceReturn the state for the first page
Does not transition state. To transition state call first
instead.
Key | Type | Description |
---|---|---|
cursor | string | |
pageSize | number |
getRequestInit(props)
SourceParameter | Type | Description | |
---|---|---|---|
* | props.options | options | |
* | props.query | any |
Key | Type | Description |
---|---|---|
headers | HeadersInit|Record | Any headers to add to the request. You can unset default headers that might be specified in the default
|
query | Record | Any query request parameters |
urlArgs | Record | Any arguments for the URL |
next()
SourceGo to the next page.
If the last next is not yet known because results haven't been returned this function does nothing.
nextState()
SourceReturn the state for the first page. If the next page isn't yet known (eg. results haven't yet been returned) then null will be returned.
Does not transition state. To transition state call next
instead.
One of the following:
CursorPaginationStateKey | Type | Description |
---|---|---|
cursor | string | |
pageSize | number |
OR
nullpageSizeState(pageSize)
SourceReturn the state for the specified page size
Does not transition state. To transition state call setPageSize
instead.
Parameter | Type | Description | |
---|---|---|---|
* | pageSize | number|null |
Key | Type | Description |
---|---|---|
cursor | string | |
pageSize | number |
previous()
SourceGo to the previous page.
If the previous page is not yet known because results haven't been returned this function does nothing.
previousState()
SourceReturn the state for the previous page. If the previous page isn't yet known (eg. results haven't yet been returned) then null will be returned.
Does not transition state. To transition state call previous
instead.
One of the following:
CursorPaginationStateKey | Type | Description |
---|---|---|
cursor | string | |
pageSize | number |
OR
nullsetResponse(props)
SourceSets the internal data based on response. Expects nextCursor
, previousCursor
and optionally pageSize
to be in
response data.
See getPaginationState for how to customise this if your backend implementation differs.
Parameter | Type | Description | |
---|---|---|---|
* | props.nextCursor | undefined|null|string | |
* | props.pageSize | number | |
* | props.previousCursor | undefined|null|string |
Static Methods
getPaginationState(requestDetails)
SourceExpected pagination state in the shape: { next: null|'http://example.com/?cursor=abc123', previous: null|'http://example.com/?cursor=abc123', results: Array }
Parameter | Type | Description | |
---|---|---|---|
requestDetails.decodedBody | any | The value returned by This may be a sub-key of the data returned by
and
| |
requestDetails.query | Record | Any query string parameters | |
requestDetails.response | Response | The | |
* | requestDetails.url | string | Resolved URL |
requestDetails.urlArgs | Record | Any arguments used to resolve URL |
One of the following:
RecordOR
falseProperties
cursor: string|null
The current cursor. This will be null before the first response is received.
nextCursor: string|null
The next cursor (if any)
pageSize: number|null
The current page size, if known
previousCursor: string|null
THe previous cursor (if any)
Inherited Methods
replaceStateControllers(currentStatePair,internalStatePair)
SourcePaginator receives 2 tuples of a state and state setter pair. This is expected to
match the same interface as useState
in React. The following is a valid simple usage:
const paginator = new Paginator(useState(), useState());
Note that we can also pass the state controllers in via replaceStateControllers
rather
than in the constructor. This is so we can memoize the Paginator
instance which is desirable
when using the paginator as a dependency to React hooks.
As state is passed in and managed external to the class be aware that any data stored
on the class instance will be lost unless written with setCurrentState
or setInternalState
.
This design is a compromise between allowing a clear interface for how paginators should
be defined and allowing the state to be managed externally (eg. using React state).
Parameter | Type | Description | |
---|---|---|---|
* | currentStatePair | any | The state object and setter (eg. from |
* | internalStatePair | any | The state object and setter that is used for internal state. Internal state
is stored separately as it does not need to be restored (eg. if you refresh the page). It is used
to store things like the total number of results or the current cursor. Passing |
Inherited Properties
currentState: CursorPaginationState
internalState:
responseIsSet: boolean
True once setResponse has been called and pagination state is known.