Pagination with json-server and React.js

Viewed 6903

I am trying to paginate JSON data that I am using with JSON server in React. I am currently using splice to break the data up into pages with 10 items per page. To get the data from each splice I have an unordered list where the list items are assigned the data. I actually want to just have pages and a next button rather than have each page listed at the top of the page, but I cant seem to wrap my head around it. I would like to use _page _limit. Any help would be appreciated. Here is the code.

import React, { Component } from 'react';
// added axios to assist with API calls.
import axios from 'axios';

import './App.css';
import Card from './Card.js';
import SearchBar from "./SearchBar.js"
import star from './images/star.svg';
import wars from './images/wars.svg';

class App extends Component {

  constructor(props) {
    super(props);
    this.state = {
      people: []
    }
  }

  componentDidMount() {
    this.getPeopleData();
  }

  getPeopleData(search) {
    axios.get('http://localhost:3008/people?_start=0&_end=9&q=' + search).then((response) => {
      this.setState ({
        people: response.data
      });
    });
  }


  //1. Using axios and decided to use the slice method rather than the _page _limit from https://github.com/typicode/json-server#slice
  limitPerPage (begin, end) {
    axios(`http://localhost:3008/people?_start=${begin}&_end=${end}`).then((response) => {
      this.setState ({
        people: response.data
      });
    });
  }
  
  render() {
    return (
      <div className='content'>
        <div className='logo'>
          <img src={star} alt="star-logo" />
          <span className='interview-text'>The Interview</span>
          <img src={wars} alt="wars-logo" />
        </div>


        <h2>Cards</h2>

        {/* Decided to display the card pages as a list. Each page holds 10 cards */}
        <ul className="card-pagination">
          <li>
            <a onClick={() => this.limitPerPage(0, 9)}>Page 1</a>
          </li>

          <li>
            <a onClick={() => this.limitPerPage(10, 19)}>Page 2</a>
          </li>

          <li>
            <a onClick={() => this.limitPerPage(20, 29)}>Page 3</a>
          </li>

          <li>
            <a onClick={() => this.limitPerPage(30, 39)}>Page 4</a>
          </li>

          <li>
            <a onClick={() => this.limitPerPage(40, 49)}>Page 5</a>
          </li>

          <li>
            <a onClick={() => this.limitPerPage(50, 59)}>Page 6</a>
          </li>

          <li>
            <a onClick={() => this.limitPerPage(60, 69)}>Page 7</a>
          </li>

          <li>
            <a onClick={() => this.limitPerPage(70, 79)}>Page 8</a>
          </li>

          <li>
            <a onClick={() => this.limitPerPage(80, 89)}>Page 9</a>
          </li>
        
        </ul>

        {this.state.people.map((person) => {
          return <Card
                    key={person.id}
                    id={person.id}
                    name={person.name}
                    imageURL={('http://localhost:3008/' + person.image)}
                    birthday={person.birth_year}
                    planetList={this.state.planets}
                    homeWorld={person.homeworld}
                  />
        })}
      </div>
    );
  }
}

export default App;

1 Answers
Related