Guest User

Untitled

a guest
Oct 30th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. import React from 'react';
  2. import './register.css';
  3. import Axios from 'axios';
  4. import {Link} from 'react-router-dom';
  5.  
  6.  
  7. class RegForm extends React.Component{
  8. constructor (props){
  9. super(props);
  10. this.state={
  11. firstName:'',
  12. lastName:'',
  13. userName:'',
  14. password:'',
  15. cnfmPassword:''
  16. };
  17. }
  18. handleChange = (e) => {
  19. this.setState({
  20. [e.target.name] : e.target.value
  21. })
  22. console.log(e.target.value);
  23. }
  24. formSubmit = (e) =>{
  25. e.preventDefault(); // prevent default form submission behaviour
  26.  
  27. //Sree, please add prompt asking for user to insert matching passwords if the below condition is met
  28.  
  29. if (this.state.password != this.state.cnfmPassword) {
  30. alert("Passwords do not match. Please try again.")
  31.  
  32. return;
  33. }
  34.  
  35. Axios({
  36. method: "post",
  37. url: "/users",
  38. data: {
  39. firstName: this.state.firstName,
  40. lastName:this.state.lastName,
  41. userName: this.state.userName,
  42. password: this.state.password,
  43. },
  44.  
  45. }).then((response) => {
  46. console.log(response);
  47. if(response.data.validated) {
  48. this.setState({
  49. firstName:'',
  50. lastName:'',
  51. userName:'',
  52. password:'',
  53. cnfmPassword:''
  54. })
  55.  
  56. window.location.href ="/package"
  57.  
  58. };
  59. alert(response.data.message);
  60.  
  61.  
  62. }).catch((err) =>{
  63. console.log(err);
  64. })
  65. }
  66.  
  67. render(){
  68. return(
  69. <div className='container row'>
  70. <label><h2> Register</h2></label>
  71. <div className='registration-form'>
  72. <form onSubmit={this.formSubmit} >
  73.  
  74.  
  75. <div className="form-group row">
  76. <label for="firstName" className="col-sm-2 col-form
  77. label">First Name</label>
  78. <div className="col-sm-10">
  79. <input type='text' id='firstName' className='form-control' name='firstName'
  80. placeholder='First Name' onChange={this.handleChange} value={this.state.firstName} required/>
  81. </div>
  82. </div>
  83.  
  84. <div className="form-group row">
  85. <label for="lastName" className="col-sm-2 col-form-label">Last Name</label>
  86. <div className="col-sm-10">
  87. <input type='text' id='lastName' className='form-control' name='lastName'
  88. placeholder='Last Name' onChange={this.handleChange} value={this.state.lastName} required/>
  89. </div>
  90. </div>
  91.  
  92. <div className="form-group row">
  93. <label for="userName" className="col-sm-2 col-form-label">User Name</label>
  94. <div className="col-sm-10">
  95. <input type='text' id='userName' className='form-control' name='userName'
  96. placeholder='User Name' onChange={this.handleChange} value={this.state.userName} required/>
  97. </div>
  98. </div>
  99. <div className="form-group row">
  100. <label for="password" className="col-sm-2 col-form-label">Password</label>
  101. <div className="col-sm-10">
  102. <input type='password' id='password' className='form-control' name='password'
  103. placeholder='Password' onChange={this.handleChange} value={this.state.password} required/>
  104. </div>
  105. </div>
  106. <div className="form-group row">
  107. <label for="cnfmPassword" className="col-sm-2 col-form-label">Confirm Password</label>
  108. <div className="col-sm-10">
  109. <input type='password' id='cnfmPassword' className='form-control' name='cnfmPassword'
  110. placeholder='Confirm Password' onChange={this.handleChange} value={this.state.cnfmPassword} required/>
  111. </div>
  112. </div>
  113. <div className="form-group row">
  114. <input type='submit' id='submit' value='Submit' className='btn btn-primary ' />
  115. </div>
  116.  
  117.  
  118. <p>Already registered? Go to Login! </p>
  119.  
  120.  
  121. </form>
  122. <p><Link to='/'>Login</Link></p>
  123. </div>
  124. </div>
  125. )
  126. }
  127.  
  128. }
  129.  
  130. export default RegForm;
Add Comment
Please, Sign In to add comment