Advertisement
Guest User

Untitled

a guest
Jan 14th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. import React from 'react';
  2. import LateralMenu from '../../components/LateralMenu';
  3.  
  4. const style = {
  5. 'font-size':'16px',
  6. }
  7.  
  8. class Users extends React.Component {
  9.  
  10. constructor() {
  11. super();
  12. this.state = {
  13. items: [],
  14. username: '',
  15. email: '',
  16. password: ''
  17. };
  18. console.log(this.state)
  19. }
  20. componentDidMount() {
  21.  
  22. fetch('http://localhost:3000/api/users')
  23. .then(result => result.json())
  24. .then(items => {this.setState({items});
  25. });
  26. }
  27.  
  28. onInputChange (property, e) {
  29. this.setState({
  30. [this.state[property]]: e.target.value
  31. });
  32. }
  33.  
  34. onSubmit (e) {
  35. fetch (`http://localhost:3000/api/users/create/${this.state.username}/${this.state.email}/${this.state.password}`, {
  36. method: 'POST'
  37. })
  38. }
  39.  
  40.  
  41. render() {
  42.  
  43. return (
  44. <div>
  45. <LateralMenu/>
  46. <div className="main">
  47.  
  48. <button type="button" className="btn btn-info btn-lg pull-right" data-toggle="modal" data-target="#myModal">New User</button>
  49.  
  50. <div id="myModal" className="modal fade" role="dialog">
  51. <div className="modal-dialog">
  52.  
  53.  
  54. <div className="modal-content">
  55. <div className="modal-header">
  56. <h4 className="modal-title">Add New User</h4>
  57. </div>
  58. <div className="modal-body">
  59.  
  60.  
  61. <form className="form-horizontal" onSubmit={this.onSubmit.bind(this)} id="create">
  62.  
  63. <p><input type="text" className="form-control" required placeholder="Username" onChange={this.onInputChange.bind(this, 'username')}/></p>
  64. <p><input type="text" className="form-control" required placeholder="Email" onChange={this.onInputChange.bind(this, 'email')}/></p>
  65. <p><input type="text" className="form-control" required placeholder="Password" onChange={this.onInputChange.bind(this, 'password')}/></p>
  66.  
  67.  
  68. </form>
  69.  
  70. </div>
  71. <div className="modal-footer">
  72. <button type="submit" form="create" className="btn btn-success">Create</button>
  73. <button type="button" className="btn btn-default" data-dismiss="modal">Close</button>
  74. </div>
  75. </div>
  76.  
  77. </div>
  78. </div>
  79.  
  80.  
  81. <table className="table table-striped">
  82. <thead>
  83. <tr>
  84. <th>Username</th>
  85. <th>Email</th>
  86. <th>Registered Date</th>
  87. <th>Options</th>
  88. </tr>
  89. </thead>
  90. <tbody>
  91.  
  92. {this.state.items.length ?
  93. this.state.items.map(item=>
  94.  
  95. <tr>
  96. <td>{item.username}</td>
  97. <td><a href={'mailto:' + item.email}>{item.email}</a></td>
  98. <td>{item.date}</td>
  99. <td><a href="#"><b>Edit</b></a> | <a className="red" href="#">Delete</a></td>
  100. </tr>
  101.  
  102. )
  103. : <li>Loading...</li>
  104. }
  105.  
  106. </tbody>
  107. </table>
  108. </div>
  109.  
  110.  
  111.  
  112. </div>
  113.  
  114. );
  115.  
  116. }
  117. }
  118.  
  119. export default Users;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement