Guest User

Untitled

a guest
Jan 17th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. this.props.postUsers(this.state.username,this.state.password,this.state.errors)
  2.  
  3. export function postUsers(username, password, errors) {
  4. let users = {
  5. username,
  6. password,
  7. };
  8. let self = this;
  9. return{
  10. type: "USERS_POST",
  11. payload: axios({
  12. method:'POST',
  13. url: url,
  14. data: users,
  15. contentType: 'application/json',
  16. headers: {
  17. 'Accept': 'application/json',
  18. 'Content-Type': 'application/json',
  19. }
  20. })
  21. .then(success => {
  22. console.log('sucesssssssss', success)
  23. })
  24. .catch(({response}) => {
  25. self.setState({errors: response.data});
  26. })
  27.  
  28. }
  29. }
  30.  
  31. import React, {Component} from 'react';
  32. import { connect } from 'react-redux';
  33. import {bindActionCreators} from 'redux';
  34. import { postUsers } from '../actions/usersAction';
  35.  
  36. class Layout extends Component {
  37. constructor(props){
  38. super(props);
  39. this.state = {
  40. username: '',
  41. password: '',
  42. errors: [],
  43. }
  44. }
  45. onUserUpdate(filed, event){
  46. if (filed === 'username') {
  47. this.setState({
  48. username: event.target.value
  49. });
  50. }
  51. if (filed ==='password') {
  52. this.setState({
  53. password: event.target.value
  54. });
  55. }
  56. }
  57.  
  58. handlePostUsers(e){
  59. e.preventDefault();
  60. this.props.postUsers(this.state.username,this.state.password,this.state.errors)
  61. }
  62. render() {
  63. console.log('this.state.errors',this.state.errors);
  64. return (
  65. <div className="App">
  66. <input name="username" onChange={this.onUserUpdate.bind(this, 'username')}/>
  67. <input name="username" onChange={this.onUserUpdate.bind(this, 'password')}/>
  68. <button onClick={(e) => this.handlePostUsers(e)}>Go ahead</button>
  69. </div>
  70. );
  71. }
  72. }
  73.  
  74. function mapStateToProps(state) {
  75. return {
  76. act: state.users,
  77. };
  78. }
  79.  
  80. function matchDispatchToProps(dispatch) {
  81. return bindActionCreators({postUsers}, dispatch)
  82. }
  83. export default connect(mapStateToProps, matchDispatchToProps)(Layout);
Add Comment
Please, Sign In to add comment