From 1cdeb1c70a89ef1936d82e5f82f903c357f7b61a Mon Sep 17 00:00:00 2001 From: Ernst Widerberg Date: Mon, 18 Oct 2021 11:57:00 +0200 Subject: Implement search --- src/components/List.js | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'src/components/List.js') diff --git a/src/components/List.js b/src/components/List.js index e0002ec..6def347 100644 --- a/src/components/List.js +++ b/src/components/List.js @@ -10,27 +10,43 @@ class List extends React.Component { this.state = { objects: [], filter: { - field: "default-field", - value: "" + field: null, + value: null }, page: 1, totalPages: 1 }; + this.filter = this.filter.bind(this); + this.filterString = this.filterString.bind(this); this.getData = this.getData.bind(this); - this.setPage = this.setPage.bind(this); this.queryString = this.queryString.bind(this); + this.setPage = this.setPage.bind(this); } componentDidMount() { this.getData(); } + // + // Helpers + // + + filterString() { + return this.state.filter.field == null || + this.state.filter.value == null + ? null + : this.state.filter.field + "=" + this.state.filter.value; + } + queryString() { return [ `limit=${process.env.PER_PAGE}`, - `skip=${this.state.page * process.env.PER_PAGE}` - ].join("&"); + `skip=${(this.state.page - 1) * process.env.PER_PAGE}`, + this.filterString() + ] + .filter(x => x !== null) + .join("&"); } // Fetch data from external source, update state @@ -59,6 +75,10 @@ class List extends React.Component { .catch(e => this.props.setError(e)); } + // + // Event handlers + // + filter(field, value) { this.setState( { -- cgit v1.1