Guest User

Untitled

a guest
Sep 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. class Userlist extends Component {
  2. constructor(props) {
  3. super(props)
  4.  
  5. this.state = {
  6. data: this.props.ui.users
  7. }
  8. }
  9.  
  10.  
  11. render() {
  12. console.log(this.props)
  13. return (
  14. <div>
  15. <input type="text"
  16. value={this.props.ui.inputName}
  17. name="username"
  18. onChange={(e) => this.props.uiActions.handleNameChange(e.target.value)}/>
  19. <input type="text"
  20. value={this.props.ui.inputEmail}
  21. name="email"
  22. onChange={(e) => this.props.uiActions.handleEmailChange(e.target.value)}/>
  23. <table>
  24. <thead>
  25. <tr>
  26. <th>LP</th>
  27. <th>USER</th>
  28. <th>E-MAIL</th>
  29. </tr>
  30. </thead>
  31. <tbody>
  32. {this.props.ui.users.map((item, index) => {
  33. return (
  34. <tr key={index}>
  35. <td>{item.id}</td>
  36. <td>{item.name}</td>
  37. <td>{item.email}</td>
  38. </tr>
  39. )
  40. })}
  41. </tbody>
  42. <tfoot>
  43. </tfoot>
  44. <button onClick={() => this.props.uiActions.addUser(this.state.username)}>add</button>
  45. </table>
  46. </div>
  47. )
  48. }
  49. }
  50.  
  51. function mapDispatchToProps(dispatch) {
  52. return {
  53. uiActions: bindActionCreators(UI_ACTIONS, dispatch)
  54. };
  55. }
  56.  
  57. function mapStateToProps(state) {
  58. return {
  59. ui: state.ui
  60. };
  61. }
  62.  
  63. export default connect(mapStateToProps, mapDispatchToProps)(Userlist);
  64.  
  65. const initialState = {
  66. users: [],
  67. name: '',
  68. email: '',
  69. inputName: undefined,
  70. inputEmail: undefined
  71. }
  72.  
  73. export default (state = initialState, action) => {
  74. switch (action.type) {
  75. case UI_ACTIONS.SET_REPOS:
  76. return { ...state, users: action.users };
  77. case UI_ACTIONS.ADD_USER:
  78. return {...state, users: action.users, inputName: '', inputEmail: '' };
  79. case UI_ACTIONS.UPDATE_NAME:
  80. return {...state, name: action.val };
  81. case UI_ACTIONS.UPDATE_EMAIL:
  82. return {...state, email: action.val};
  83. default:
  84. return state;
  85. }
  86. };
Add Comment
Please, Sign In to add comment