Quickstart

A quick dive into getting started with Lore

Step 1: Add Pagination Metadata

In this step we'll add pagination information to our collections, so components can tell how many pages of data are available.

You can view the finished code for this step by checking out the pagination.1 branch of the completed project.

Pagination Metadata

In order to implement pagination, we need to know some information about the tweets we're viewing, such as:

  1. How many tweets exist
  2. How many tweets are provided on each page

If we look at the API response from the server, we can see that that information is provided in the response as meta.paginate:

{
  data: [
    {...tweet...},
    {...tweet...}
  ],
  meta: {
    paginate: {
      currentPage: 1,
      nextPage: 2,
      prevPage: null,
      totalPages: 11,
      totalCount: 51,
      perPage: 5
    }
  }
}

What we want to do is make that information available to the application, so we can calculate the number of pages to display.

Add Pagination Data to Collections

To do that open up config/connections.js and find the parse() method for collections. Update that method to look like this:

// config/connections.js
...
  collections: {
    properties: {
      parse: function(response) {
        this.meta = {
          totalCount: response.meta.paginate.totalCount,
          perPage: response.meta.paginate.perPage
        };
        return response.data;
      }
    }
  }
...

Collections in Lore have a special property called meta. It defaults to an empty object, and anything you add to it will show up in the meta property of the data structure for collections.

By adding totalCount and perPage to meta, we've now made that data available to any component that receives a collection.

Visual Check-in

If everything went well, your application should now look like this. Exactly the same :)

Code Changes

Below is a list of files modified during this step.

config/connections.js

// config/connections.js
import auth from '../src/utils/auth';

export default {

  default: {

    apiRoot: 'http://localhost:1337',

    headers: function() {
      return {
        Authorization: `Bearer ${auth.getToken()}`
      };
    },

    collections: {
      properties: {
        parse: function(response) {
          this.meta = {
            totalCount: response.meta.paginate.totalCount,
            perPage: response.meta.paginate.perPage
          };
          return response.data;
        }
      }
    }

  }
};

Next Steps

Next step we're going to paginate the feed.