|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { connect } from 'react-redux'; |
| 3 | +import { withRouter } from 'react-router'; |
| 4 | +import PageHeader from 'react-bootstrap/lib/PageHeader'; |
| 5 | +import Table from 'react-bootstrap/lib/Table'; |
| 6 | +import Resources from '../../../../constants/Resources'; |
| 7 | +import userAPI from '../../../../api/user'; |
| 8 | +import { pushErrors } from '../../../../actions/errorActions'; |
| 9 | +import { setCrrentPage, setPage } from '../../../../actions/pageActions'; |
| 10 | +import PageLayout from '../../../layouts/AdminPageLayout'; |
| 11 | +import Pagination from '../../../utils/BsPagination'; |
| 12 | + |
| 13 | +class ListPage extends Component { |
| 14 | + constructor() { |
| 15 | + super(); |
| 16 | + this.state = { |
| 17 | + users: [], |
| 18 | + }; |
| 19 | + } |
| 20 | + |
| 21 | + componentDidMount() { |
| 22 | + let { dispatch, location } = this.props; |
| 23 | + dispatch(setCrrentPage(Resources.USER, location.query.page || 1)); |
| 24 | + } |
| 25 | + |
| 26 | + componentDidUpdate(prevProps) { |
| 27 | + let { dispatch, apiEngine, page, router, location } = this.props; |
| 28 | + |
| 29 | + if (prevProps.page.current !== page.current) { |
| 30 | + userAPI(apiEngine) |
| 31 | + .list({ page: page.current }) |
| 32 | + .catch((err) => { |
| 33 | + dispatch(pushErrors(err)); |
| 34 | + throw err; |
| 35 | + }) |
| 36 | + .then((json) => { |
| 37 | + this.setState({ users: json.users }); |
| 38 | + dispatch(setPage(Resources.USER, json.page)); |
| 39 | + router.push({ |
| 40 | + pathname: location.pathname, |
| 41 | + query: { page: json.page.current }, |
| 42 | + }); |
| 43 | + }); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + render() { |
| 48 | + let { users } = this.state; |
| 49 | + |
| 50 | + return ( |
| 51 | + <PageLayout> |
| 52 | + <PageHeader>User List</PageHeader> |
| 53 | + <Table striped bordered> |
| 54 | + <thead> |
| 55 | + <tr> |
| 56 | + <th>ID</th> |
| 57 | + <th>Name</th> |
| 58 | + <th>Email</th> |
| 59 | + <th>Role</th> |
| 60 | + <th>Created At</th> |
| 61 | + </tr> |
| 62 | + </thead> |
| 63 | + <tbody> |
| 64 | + {users.map((user) => ( |
| 65 | + <tr key={user._id}> |
| 66 | + <td>{user._id}</td> |
| 67 | + <td>{user.name}</td> |
| 68 | + <td>{user.email.value}</td> |
| 69 | + <td>{user.role}</td> |
| 70 | + <td>{user.createdAt}</td> |
| 71 | + </tr> |
| 72 | + ))} |
| 73 | + </tbody> |
| 74 | + </Table> |
| 75 | + <Pagination resourceName={Resources.USER} /> |
| 76 | + </PageLayout> |
| 77 | + ); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +export default withRouter(connect(state => ({ |
| 82 | + apiEngine: state.apiEngine, |
| 83 | + page: state.pages[Resources.USER] || {}, |
| 84 | +}))(ListPage)); |
0 commit comments