From 25098aebd09acdd791a593506c8ab0835f1790aa Mon Sep 17 00:00:00 2001 From: Ernst Widerberg Date: Fri, 15 Oct 2021 12:49:55 +0200 Subject: Add pagination --- src/components/List.js | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'src/components/List.js') diff --git a/src/components/List.js b/src/components/List.js index b93f2ee..558f1ab 100644 --- a/src/components/List.js +++ b/src/components/List.js @@ -1,5 +1,5 @@ import React from "react"; -import { Button } from "semantic-ui-react"; +import { Button, Pagination } from "semantic-ui-react"; import ObjectComponent from "./ObjectComponent"; import SearchForm from "./SearchForm"; @@ -12,19 +12,30 @@ class List extends React.Component { filter: { field: "default-field", value: "" - } + }, + page: 1, + totalPages: 1 }; this.getData = this.getData.bind(this); + this.setPage = this.setPage.bind(this); + this.queryString = this.queryString.bind(this); } componentDidMount() { this.getData(); } + queryString() { + return [ + `limit=${process.env.PER_PAGE}`, + `skip=${this.state.page * process.env.PER_PAGE}` + ].join("&"); + } + // Fetch data from external source, update state getData() { - fetch(`${process.env.COLLECTOR_URL}/sc/v0/get`, { + fetch(`${process.env.COLLECTOR_URL}/sc/v0/get?${this.queryString()}`, { headers: { Authorization: "Basic " + btoa("user1:pw1") } @@ -52,6 +63,13 @@ class List extends React.Component { ); } + setPage(e, { activePage: n }) { + this.setState({ page: n }, () => { + this.getData(); + window.scrollTo(0, 0); + }); + } + render() { return (
@@ -72,6 +90,14 @@ class List extends React.Component { return ; })}
+ ); } -- cgit v1.1