Advertisement
rhuntington

Child to Parent callback

May 27th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // child component
  2.  
  3. class LoginForm extends Component {
  4.     constructor(props) {
  5.       super(props);
  6.  
  7.       this.onChangeUsername = this.onChangeUsername.bind(this);
  8.       this.onChangePassword = this.onChangePassword.bind(this);
  9.       this.onSubmit = this.onSubmit.bind(this);
  10.  
  11.       this.state = {
  12.         username: '',
  13.         password: '',
  14.         authenticated: false,
  15.       };
  16.     }
  17.     onChangeUsername = (e) => {
  18.       this.setState({username: e.target.value});
  19.     };
  20.  
  21.     onChangePassword = (e) => {
  22.       this.setState({password: e.target.value});
  23.     };
  24.  
  25.     onSubmit(e) {
  26.       const params = {
  27.         username: this.state.username,
  28.         password: this.state.password
  29.       };
  30.       // This is where you need to put your data to pass to the parent. Notice it's a prop. You pass whatever you want to the parent
  31.       which you set here in the child
  32.       this.props.callbackFromParent(params);
  33.       e.preventDefault();
  34.     };
  35.  
  36.  
  37. // parent component
  38. class Login extends Component {
  39.     constructor(props) {
  40.         super(props);
  41.    
  42.         this.state = {
  43.           username: '',
  44.           password: '',
  45.           authenticated: false,
  46.           loginData: {},
  47.           status: 200,
  48.         };
  49.     }
  50.     componentDidMount() {
  51.         fetch('/users/authenticated')
  52.         .then(res => res.text())
  53.         .then(
  54.           (res) => {
  55.             if(res === 'true') {
  56.               this.setState({authenticated: true});
  57.             } else {
  58.               this.setState({authenticated: false, loginData: {} });
  59.             }
  60.           }
  61.         )
  62.         .catch(err => console.log(err));
  63.       }
  64.  
  65.     // callback function to loginFrom child component
  66.     parentCallback = async (dataFromChild) => {
  67.         await this.setState({ loginData: dataFromChild});
  68.         // Axios.post('/users/test', this.state.loginData).then((data) => {
  69.         //   console.log(data);
  70.         // }).catch(err => console.log(err));
  71.         Axios.post('/users/login', this.state.loginData).then((data) => {
  72.             data.status === 200 ? this.setState({ status: 200, authenticated: true }) : console.log('Unauthorized');
  73.         }).catch((err) => {
  74.             err ? this.setState({ status: 401, authenticated: false }) : console.log('No errors');
  75.         });
  76.     }
  77.  
  78.     render() {
  79.         const { authenticated } = this.state;
  80.         const { status } = this.state;
  81.  
  82.         if(authenticated === true) {
  83.             return(
  84.                 <div>
  85.                     <Redirect to="/income"/>
  86.                 </div>
  87.             )
  88.         } else {
  89.             return status === 401 ? <div className="App"><LoginForm callbackFromParent={this.parentCallback}/><br/><span className="errorMessage">Username and password do not match.<br/>Please try again.</span></div> : <div className="App"><LoginForm callbackFromParent={this.parentCallback}/></div>;
  90.         }
  91.     }
  92.  
  93. }
  94. export default Login
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement