Advertisement
Guest User

Register

a guest
Nov 28th, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Register
  2. import React from 'react';
  3. import { Link } from 'react-router-dom';
  4. import { connect } from 'react-redux';
  5.  
  6. import { userActions } from '../_actions';
  7.  
  8. class RegisterPage extends React.Component {
  9.     constructor(props) {
  10.         super(props);
  11.  
  12.         this.state = {
  13.             user: {
  14.                 firstName: '',
  15.                 lastName: '',
  16.                 dateOfBirth: '',
  17.                 username: '',
  18.                 email: '',
  19.                 password: ''
  20.             },
  21.             submitted: false
  22.         };
  23.  
  24.         this.handleChange = this.handleChange.bind(this);
  25.         this.handleSubmit = this.handleSubmit.bind(this);
  26.     }
  27.  
  28.     handleChange(event) {
  29.         const { name, value } = event.target;
  30.         const { user } = this.state;
  31.         this.setState({
  32.             user: {
  33.                 ...user,
  34.                 [name]: value
  35.             }
  36.         });
  37.     }
  38.  
  39.     handleSubmit(event) {
  40.           event.preventDefault();
  41.  
  42.           this.setState({ submitted: true });
  43.           const { user } = this.state;
  44.           const { dispatch } = this.props;
  45.           if (user.firstName && user.lastName && user.username && user.password) {
  46.               dispatch(userActions.register(user));
  47.           }
  48.       }
  49.  
  50.     render() {
  51.         const { registering  } = this.props;
  52.         const { user, submitted } = this.state;
  53.         return (
  54.             <div className="col-md-6 col-md-offset-3">
  55.                 <h2>Register</h2>
  56.                 <form name="form" onSubmit={this.handleSubmit}>
  57.                     <div className={'form-group' + (submitted && !user.firstName ? ' has-error' : '')}>
  58.                         <label htmlFor="firstName">First Name</label>
  59.                         <input type="text" className="form-control" name="firstName" value={user.firstName} onChange={this.handleChange} />
  60.                         {submitted && !user.firstName &&
  61.                             <div className="help-block">First Name is required</div>
  62.                         }
  63.                     </div>
  64.                     <div className={'form-group' + (submitted && !user.lastName ? ' has-error' : '')}>
  65.                         <label htmlFor="lastName">Last Name</label>
  66.                         <input type="text" className="form-control" name="lastName" value={user.lastName} onChange={this.handleChange} />
  67.                         {submitted && !user.lastName &&
  68.                             <div className="help-block">Last Name is required</div>
  69.                         }
  70.                     </div>
  71.  
  72.                     <div className={'form-group' + (submitted && !user.dateOfBirth ? ' has-error' : '')}>
  73.                         <label htmlFor="dateOfBirth">Date of birth</label>
  74.                         <input type="text" className="form-control" name="dateOfBirth" value={user.dateOfBirth} onChange={this.handleChange} />
  75.                         {submitted && !user.dateOfBirth &&
  76.                             <div className="help-block">Date of birth is required</div>
  77.                         }
  78.                     </div>
  79.  
  80.                     <div className={'form-group' + (submitted && !user.username ? ' has-error' : '')}>
  81.                         <label htmlFor="username">Username</label>
  82.                         <input type="text" className="form-control" name="username" value={user.username} onChange={this.handleChange} />
  83.                         {submitted && !user.username &&
  84.                             <div className="help-block">Username is required</div>
  85.                         }
  86.                     </div>
  87.  
  88.                     <div className={'form-group' + (submitted && !user.email ? ' has-error' : '')}>
  89.                         <label htmlFor="email">Email</label>
  90.                         <input type="text" className="form-control" name="email" value={user.email} onChange={this.handleChange} />
  91.                         {submitted && !user.email &&
  92.                             <div className="help-block">Email is required</div>
  93.                         }
  94.                     </div>
  95.  
  96.                     <div className={'form-group' + (submitted && !user.password ? ' has-error' : '')}>
  97.                         <label htmlFor="password">Password</label>
  98.                         <input type="password" className="form-control" name="password" value={user.password} onChange={this.handleChange} />
  99.                         {submitted && !user.password &&
  100.                             <div className="help-block">Password is required</div>
  101.                         }
  102.                     </div>
  103.                     <div className="form-group">
  104.                         <button className="btn btn-primary">Register</button>
  105.                         { registering }
  106.                         <Link to="/login_u" className="btn btn-link">Cancel</Link>
  107.                     </div>
  108.                 </form>
  109.             </div>
  110.         );
  111.     }
  112. }
  113.  
  114. function mapStateToProps(state) {
  115.     const { registering } = state.registration;
  116.     return {
  117.         registering
  118.     };
  119. }
  120.  
  121. export default connect(mapStateToProps)(RegisterPage);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement