-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserListComponent.js
80 lines (70 loc) · 2.64 KB
/
UserListComponent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import React, { Component } from 'react';
import UserServices from '../../services/UserServices';
class UserListComponent extends Component {
constructor(props) {
super(props)
this.state = {
users: []
}
this.addUser = this.addUser.bind(this);
this.editUser = this.editUser.bind(this);
this.deleteUser = this.deleteUser.bind(this);
}
addUser() {
this.props.history.push("/sign-up");
}
editUser(id) {
this.props.history.push(`/update-user/${id}`);
}
deleteUser(id) {
UserServices.deleteUser(id).then( res => {
this.setState({ users: this.state.users.filter(user => user.id !== id)});
});
}
componentDidMount() {
UserServices.getUsers().then((res) => {
this.setState({users: res.data});
});
}
render() {
return (
<div className= "container">
<h2 className="text-center">User List</h2>
<div className="row">
<button className="btn btn-outline-success" onClick={this.addUser}>Add</button>
</div>
<table className = "table table-striped bordered hover variant= dark">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Password</th>
<th>Password2</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{
this.state.users.map(
user =>
<tr key = {user.id}>
<td>{user.firstName}</td>
<td>{user.lastName}</td>
<td>{user.email}</td>
<td>{user.password}</td>
<td>{user.password2}</td>
<td>
<button onClick = { () => this.editUser(user.id)} className="btn btn-info">Update</button>
<button style={{marginLeft: "10px"}} onClick = { () => this.deleteUser(user.id)} className="btn btn-danger">Delete</button>
</td>
</tr>
)
}
</tbody>
</table>
</div>
);
}
}
export default UserListComponent;