Guest User

Untitled

a guest
Nov 25th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. import React, { Component } from 'react'
  2. import { register } from './UserFunctions'
  3.  
  4. class Register extends Component {
  5. constructor() {
  6. super()
  7. this.state = {
  8. first_name: '',
  9. last_name: '',
  10. email: '',
  11. password: '',
  12. }
  13. this.onChange = this.onChange.bind(this)
  14. this.onSubmit = this.onSubmit.bind(this)
  15. }
  16.  
  17. onChange (e) {
  18. this.setState({ [e.target.name]: e.target.value })
  19. }
  20.  
  21. onSubmit (e) {
  22. e.preventDefault()
  23.  
  24. const user = {
  25. first_name: this.state.first_name,
  26. last_name: this.state.last_name,
  27. email: this.state.email,
  28. password: this.state.password
  29. }
  30. const errors = {}
  31. const emailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
  32. errors.email = !user.email.match(emailformat) ?
  33. "Invalid Email" : ""
  34. errors.password = user.password.length < 6 ?
  35. "Password should be more than 6 characters" : ""
  36. console.log(errors)
  37.  
  38. if (errors.email === "" && errors.password === "") {
  39. register(user).then(res => {
  40. this.props.history.push(`/login`)
  41. })
  42. }
  43. }
  44.  
  45. render () {
  46. return (
  47. <div className="container">
  48. <div className="row">
  49. <div className="col-md-6 mt-5 mx-auto">
  50. <form noValidate onSubmit={this.onSubmit}>
  51. <h1 className="h3 mb-3 font-weight-normal">Please sign in</h1>
  52. <div className="form-group">
  53. <label htmlFor="first_name">First Name</label>
  54. <input type="text"
  55. className="form-control"
  56. name="first_name"
  57. placeholder="Enter First Name"
  58. value={this.state.first_name}
  59. onChange={this.onChange} />
  60. </div>
  61. <div className="form-group">
  62. <label htmlFor="last_name">Last Name</label>
  63. <input type="text"
  64. className="form-control"
  65. name="last_name"
  66. placeholder="Enter Last Name"
  67. value={this.state.last_name}
  68. onChange={this.onChange} />
  69. </div>
  70. <div className="form-group">
  71. <label htmlFor="email">Email Address</label>
  72. <input type="email"
  73. className="form-control"
  74. name="email"
  75. placeholder="Enter Email"
  76. value={this.state.email}
  77. onChange={this.onChange} />
  78. </div>
  79. <div className="form-group">
  80. <label htmlFor="password">Password</label>
  81. <input type="password"
  82. className="form-control"
  83. name="password"
  84. placeholder="Enter Password"
  85. value={this.state.password}
  86. onChange={this.onChange} />
  87. </div>
  88. <button type="submit" className="btn btn-lg btn-primary btn-block">
  89. Register
  90. </button>
  91. </form>
  92. </div>
  93. </div>
  94. </div>
  95. )
  96. }
  97. }
  98.  
  99. export default Register
Add Comment
Please, Sign In to add comment