Advertisement
Guest User

zaza

a guest
Apr 26th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import Form from './Children/Form'
  3. import axios from 'axios'
  4.  
  5. class Registration extends Component {
  6. constructor(props) {
  7. super(props);
  8.  
  9. this.state = {
  10. username: '',
  11. email: '',
  12. password: '',
  13. confirm_password: '',
  14. error: null,
  15. errorMSG: ''
  16. }
  17.  
  18. this.handleInputChange = this.handleInputChange.bind(this);
  19. this.handleSubmit = this.handleSubmit.bind(this);
  20.  
  21. }
  22.  
  23. handleInputChange(e) {
  24.  
  25. this.setState({
  26. [e.target.name]: e.target.value
  27. })
  28.  
  29. }
  30.  
  31. handleSubmit(e) {
  32. e.preventDefault();
  33.  
  34. if (this.state.username.trim().length < 6) {
  35. this.setState({
  36. error: true,
  37. errorMSG: 'მომხმარებლის სახელის სიგრძე მინიმუმ 6 უნდა იყოს'
  38. })
  39. return false
  40. }
  41.  
  42. if (this.state.email.trim().length < 1) {
  43. this.setState({
  44. error: true,
  45. errorMSG: 'გთხოთ შეავსეთ Email-ის ველი'
  46. })
  47. return false
  48.  
  49. }
  50.  
  51. if (this.state.password.trim().length < 8) {
  52. this.setState({
  53. error: true,
  54. errorMSG: 'password character length must be at least 8 character'
  55. })
  56. return false
  57.  
  58. }
  59.  
  60. if (/\d/.test(this.state.password) === false) {
  61. this.setState({
  62. error: true,
  63. errorMSG: 'პაროლი უნდა შეიცავდეს ერთ ციფრს მაინც'
  64. })
  65. return false
  66. }
  67.  
  68. if (this.state.password !== this.state.confirm_password) {
  69. this.setState({
  70. error: true,
  71. errorMSG: 'პაროლები არ ემთხვევა'
  72. })
  73. return false
  74. }
  75.  
  76.  
  77. axios.post('/api/signup', {
  78. username: this.state.username,
  79. email: this.state.email,
  80. password: this.state.password,
  81. confirm_password: this.state.confirm_password
  82. })
  83. .then(res => res.data)
  84. .then(data => {
  85. window.location.replace(data.location)
  86. })
  87. .catch(error => {
  88. if (error.response) {
  89. this.setState({
  90. error: true,
  91. errorMSG: error.response.data.message
  92. })
  93. }
  94. else if (error.request) {
  95. console.log(error.request);
  96. } else {
  97. console.log('Error', error.message);
  98. }
  99.  
  100. })
  101.  
  102.  
  103.  
  104.  
  105. }
  106.  
  107.  
  108.  
  109. render() {
  110. return (
  111. <div>
  112. <Form>
  113. <form id="formId" className='fc' onSubmit={this.handleSubmit}>
  114. <input type="text" value={this.state.username} placeholder="username" autoComplete="off" name="username" onChange={this.handleInputChange} /><br />
  115. <input type="email" placeholder="email" value={this.state.email} autoComplete="off" name="email" onChange={this.handleInputChange} /><br />
  116. <input type="password" placeholder="password" value={this.state.password} autoComplete="off" name="password" onChange={this.handleInputChange} /><br />
  117. <input type="password" placeholder="confirm passowrd" autoComplete="off" value={this.state.confirm_password} name="confirm_password" onChange={this.handleInputChange} /><br />
  118. <button type="submit">Submit</button>
  119. {this.state.error ? this.state.errorMSG : null}
  120. </form>
  121. </Form>
  122. </div>
  123. );
  124. }
  125. }
  126. export default Registration;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement