Permalink
Browse files

add pagination to user and application list

  • Loading branch information...
peh committed Sep 20, 2018
1 parent 0d12e23 commit 5284ceaeaca50f2ac8997084764d6e56fe6c3e2c
@@ -85,7 +85,7 @@ dependencies {
compile 'org.jodd:jodd-http:3.6.6'
compile 'org.grails.plugins:joda-time:2.0.0'

compile "org.grails.plugins:errbuddy:2.1.0"
compile "org.grails.plugins:errbuddy:2.2.0"
compile "com.uberall.plugins:uber-doc:2.0.4"
compile 'org.grails.plugins:elasticsearch:1.0.0.2'
compile 'org.grails.plugins:jesque-admin:0.6.5'
@@ -2,21 +2,26 @@ import React from "react";
import BaseComponent from "../tools/base-component";
import * as _ from "lodash";
import LoadingHero from "../tools/loading-hero";
import ReactPaginate from "react-paginate";

export default class ApplicationList extends BaseComponent {
constructor(props) {
super(props)
this.state = {
applications: [],
total: 0
}
total: 0,
max: 20,
offset: 0
};
}

this._bindThis('getApplicationList')
setTimeout(this.getApplicationList, 250)
componentDidMount() {
this.getApplicationList();
}

getApplicationList() {
this.getApplicationService().list(0, 10)
let {max, offset} = this.state;
this.getApplicationService().list(offset, max)
.then((json) => {
this.setState(_.assign(this.state, {applications: json.applications, total: json.total}))
})
@@ -27,16 +32,15 @@ export default class ApplicationList extends BaseComponent {

}

componentDidMount() {
this.setInterval(this.getApplicationList, 5000)
}

componentWillUnmount() {
this.stopInterval()
changePage(pageObj) {
let offset = pageObj.selected * this.state.max;
this.setState({...this.state, offset: offset}, () => {
this.getApplicationList()
});
}

render() {
const {applications, total} = this.state;
const {applications, total, max, offset} = this.state;
if (applications.length === 0) {
return <LoadingHero />
}
@@ -70,6 +74,18 @@ export default class ApplicationList extends BaseComponent {
{rows}
</tbody>
</table>
<ReactPaginate
pageCount={Math.ceil(total / max)}
pageRangeDisplayed={4}
marginPagesDisplayed={1}
forceSelected={Math.floor(offset / max)}
onPageChange={::this.changePage}
previousLabel="&laquo;"
nextLabel="&raquo;"
breakLabel={<a href="">...</a>}
activeClassName="active"
containerClassName="pagination"
/>
</section>
)
}
@@ -6,33 +6,49 @@ import LoadingHero from "../tools/loading-hero";
import UserListRow from "./user-list-row";
import WithRole from "../tools/with-role";
import Hero from "../tools/hero";
import ReactPaginate from "react-paginate";

const ROLES_NEEDED = ['ROLE_ROOT', 'ROLE_ADMIN'];
export default class UserList extends BaseComponent {

constructor(props) {
super(props);
this.state = {users: []};
this.state = {
users: [],
total: 0,
max: 20,
offset: 0
};

this._bindThis('getUserList');
setTimeout(this.getUserList, 1000)
}

getUserList() {
this.getUserService().list(50, 0)
let {max, offset} = this.state;
this.getUserService().list(max, offset)
.then((response) => {
this.setState({users: response.users})
let {users, total} = response;
this.setState({...this.state, users: users, total: total})
})
.catch((err) => {
throw(err)
})
}

changePage(pageObj) {
let offset = pageObj.selected * this.state.max;
this.setState({...this.state, offset: offset}, () => {
this.getUserList()
});
}

render() {
if (this.state.users.length === 0) {
return <LoadingHero />
}
var rows = [];
const {total, max, offset} = this.state;
this.state.users.forEach((user) => {
rows.push(<UserListRow user={user} key={user.username} errbuddyApp={this.getApp()}/>);
});
@@ -55,6 +71,18 @@ export default class UserList extends BaseComponent {
</thead>
<tbody>{rows}</tbody>
</table>
<ReactPaginate
pageCount={Math.ceil(total / max)}
pageRangeDisplayed={4}
marginPagesDisplayed={1}
forceSelected={Math.floor(offset / max)}
onPageChange={::this.changePage}
previousLabel="&laquo;"
nextLabel="&raquo;"
breakLabel={<a href="">...</a>}
activeClassName="active"
containerClassName="pagination"
/>
</section>
</WithRole>
);
@@ -18,7 +18,7 @@ class UserController extends AbstractApiController {

def list() {
sanitizeParams()
renderJson([users: User.list(params)])
renderJson([users: User.list(params), total: User.count()])
}

def get() {
@@ -15,8 +15,4 @@ class ErrorControllerSpec extends Specification {
def cleanup() {
}

void "test something"() {
expect:"fix me"
true == false
}
}

0 comments on commit 5284cea

Please sign in to comment.