Paginator
SourceBase class for a paginator.
API
Constructor
new Paginator(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.
getRequestInit(options)
SourceReturn the options to use with Endpoint.execute
or Endpoint.prepare
. Usually
this just involves setting query
to the query string parameters for the paginator
but can also provide custom headers or different URL resolve arguments.
Parameter | Type | Description | |
---|---|---|---|
* | options | PaginatorRequestOptions | The existing options for the endpoint |
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 |
nextState()
SourceReturn the state for the next page
Does not transition state. To transition state call next
instead.
One of the following:
StateOR
nullpreviousState()
SourceReturn the state for the previous page
Does not transition state. To transition state call previous
instead.
One of the following:
StateOR
nullreplaceStateControllers(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 |
setResponse(response)
SourceThis is called when an Endpoint
has resolved and is passed the response from the
endpoint. This is used to update the relevant paginator state - eg. the total
number of records, next & previous cursors etc.
Parameter | Type | Description | |
---|---|---|---|
* | response | Record | The response as returned by the endpoint this paginator is used with. |
Properties
currentState: State
internalState:
responseIsSet: boolean
True once setResponse has been called and pagination state is known.