The last topic that we’ll cover in this tutorial is pagination. You’ll implement a simple pagination approach so that users can view the links in smaller chunks rather than having an incredibly long list of
Once more, you first need to prepare the Angular components for this new functionality. In fact, we’ll slightly adjust the current routing setup. Here’s the idea: The
LinkListComponent will be used for two different use cases (and routes). The first one is to display the 10 top voted links. Its second use case is to present new links in a list separated into multiple pages that the user can navigate through.
You added two new routes:
/new/:page. The second one reads the value for
page from the URL so that this information is available inside the component that’s rendered, here that’s
The root route
'' now redirects to the first page of the route where new posts are displayed.
You need to update the
HeaderComponent to show the new
We need to add quite a bit of logic to the
LinkListComponent to account for the two different responsibilities that it now has.
The query now accepts arguments that we’ll use to implement pagination and ordering.
skip defines the offset where the query will start. If you passed a value of, e.g.
10 to this argument, it means that the first 10 items of the list will not be included in the response.
first then defines the limit, or how many elements; you want to load from that list. Say, you’re passing the
first, you’ll receive items 10 to 15 from the list.
You need to update the references to this query in the
Let’s retake a close look to understand what’s going on:
pageParams$ observable based on the
this.route.paramMap where we retrieve all the
params and map it to get only the
page param, then we parse in
path$ observable that we create from
SegmentUrl converted in
variables in the
first$ used to calculate the chunk of links that you retrieve.
skip$, the second variable that will enable us to perform the chunk of links that you retrieve
orderBy$ that will include the ordering attribute
createdAt_DESC for the
new page to make sure the newest links are displayed first. The ordering for the
/top route will be calculated manually based on the number of votes for each link.
getQuery function that will receive the variables (the values of
orderBy$ ) in parameter, set it in the options and returns the
valueChanges. Note, that we also perform the
orderBy$, get their values and create an object having the property first, skip, orderBy
orderBy$ to provide it to the
getQuery function. Due the fact that
getQuery returns an
Observable<ApolloQueryResult<AllLinkQueryResponse>>, we will get an
Observable<Observable<ApolloQueryResult<AllLinkQueryResponse>>> if we use the
.map operator. Therefore, we use
switchMap to “flatten” the
Observable<Observable<ApolloQueryResult<AllLinkQueryResponse>>> to an
allLinks and the
You also need to define the
LINKS_PER_PAGE constant and then import it into the
LinkListComponent as well as the
You also need to map
Next, you need functionality for the user to switch between the pages. First, add two
button elements to the bottom of the
LinkListComponent that can be used to navigate back and forth.
Since you added
pageNumber as one of the
hn-link-item, you now need to add it to the
Since the setup is slightly more complicated now, you are going to calculate the list of links to be rendered in a separate method.
isNewPage, you’ll simply return all the links returned by the query. That’s logical since here you don’t have to make any manual modifications to the list that is to be rendered. If the user loaded the component from the
/top route, you’ll sort the list according to the number of votes and return the top 10 links. This is accomplished through an
orderedLinks computed property which you will implement next.
You will make use of the lodash library within the
You also need to add a
count property to the
Next, you’ll implement the functionality for the Previous- and Next-buttons.
The implementation of these is straightforward. You’re retrieving the current page from the URL and implementing a sanity check to make sure that it makes sense to paginate back or forth. Then you merely calculate the next page and tell the router where to navigate next. The router will then reload the component with a new
page in the URL that will be used to calculate the right chunk of links to load. Hop on over to the running app and use the new buttons to paginate through your list of links!
Through the changes that we made to the
ALL_LINKS_QUERY, you’ll notice that the
update functions of your mutations don’t work anymore. That’s because
readQuery now also expects to get passed the same variables that we defined before.
readQueryessentially works in the same way as the
querymethod on the
ApolloClientthat you used to implement the search. However, instead of making a call to the server, it will simply resolve the query against the local store! If a query was fetched from the server with variables,
readQueryalso needs to know the variables to make sure it can deliver the right information from the cache.
All that’s happening here is the computation of the variables depending on whether the user currently is on the
Finally, you also need to adjust the implementation of
update when new links are created.
You have now added a simple pagination system to the app, allowing users to load links in small chunks instead of loading them all up front.