Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. import React from 'react';
  2. import axios from 'axios';
  3. import Router from 'next/router';
  4. import './Register.scss';
  5.  
  6. export default class Register extends React.Component {
  7.  
  8. state = {
  9. username: '',
  10. email: '',
  11. number: '',
  12. password: '',
  13. error: null,
  14. usernameValidation: true,
  15. emailValidation: true,
  16. numberValidation: true,
  17. passwordValidation: true,
  18. inputType: 'password'
  19. };
  20.  
  21. onSubmit = e => {
  22. const {username, email, number, password} = this.state;
  23.  
  24. axios.post('/register', {
  25. username: username,
  26. email: email,
  27. number: number,
  28. password: password
  29. })
  30. .then( response => {
  31. this.setState({
  32. usernameValidation: true,
  33. emailValidation: true,
  34. numberValidation: true,
  35. passwordValidation: true
  36. });
  37. if (response.data.validation) {
  38. const validation = response.data.validation;
  39. this.setState(validation);
  40. } else {
  41. if(response.data.contacts) {
  42. this.redirect('/dashboard');
  43. } else {
  44. this.redirect('/login/locations');
  45. }
  46. }
  47. })
  48. .catch( error => {
  49. console.log(error);
  50. });
  51.  
  52. e.preventDefault();
  53. };
  54.  
  55. onChange = key => e => {
  56. this.setState({
  57. [key]: e.target.value,
  58. usernameValidation: true,
  59. emailValidation: true,
  60. numberValidation: true,
  61. passwordValidation: true
  62. });
  63. };
  64.  
  65. showHide = e => {
  66. e.preventDefault();
  67. e.stopPropagation();
  68. this.setState({
  69. inputType: this.state.inputType === 'input' ? 'password' : 'input'
  70. })
  71. }
  72.  
  73.  
  74. redirect = url => {
  75. window.location = url;
  76. }
  77.  
  78. nextRouter = url => {
  79. Router.push(url)
  80. }
  81.  
  82. render() {
  83. const {
  84. username,
  85. number,
  86. email,
  87. password,
  88. error,
  89. usernameValidation,
  90. emailValidation,
  91. numberValidation,
  92. passwordValidation
  93. } = this.state;
  94.  
  95.  
  96. return (
  97. <div className="Login">
  98. <div className="Login__auth">
  99. <div className="Login__authHeader">Зарегистрируйтесь и станьте диллером Билюкс</div>
  100. <div className="Login__socialButtons">
  101. <div
  102. className="Login__socialButton _fb"
  103. onClick={() => this.nextRouter('/auth/facebook/callback')}
  104. >
  105. Facebook
  106. </div>
  107. <div
  108. className="Login__socialButton _gg"
  109. onClick={() => this.nextRouter('/auth/google/callback')}
  110. >
  111. Google +
  112. </div>
  113. </div>
  114. <div className="Login__or">или</div>
  115. <form onSubmit={this.onSubmit}
  116. className="Login__inputs"
  117. >
  118. <div className="Login__inputsWrapp">
  119. <label>
  120. Имя и фамилия
  121. <input
  122. className={usernameValidation ? "Login__input" : "Login__input _err"}
  123. type="text" value={username}
  124. onChange={this.onChange('username')}
  125. />
  126. {!usernameValidation && error && <div className='Login__error'>{error}</div>}
  127. </label>
  128. </div>
  129. <div className="Login__inputsWrapp">
  130. <label>
  131. Email
  132. <input
  133. className={emailValidation ? "Login__input" : "Login__input _err"}
  134. type="text" value={email}
  135. onChange={this.onChange('email')}
  136. />
  137. {!emailValidation && error && <div className='Login__error'>{error}</div>}
  138. </label>
  139. </div>
  140. <div className="Login__inputsWrapp">
  141. <label>
  142. Номер телефона
  143. <input
  144. className={numberValidation ? "Login__input" : "Login__input _err"}
  145. type="text" value={number}
  146. onChange={this.onChange('number')}
  147. />
  148. {!numberValidation && error && <div className='Login__error'>{error}</div>}
  149. </label>
  150. </div>
  151. <div className="Login__inputsWrapp">
  152. <label className="Login__passwordLabel">
  153. Пароль
  154. <input
  155. className={passwordValidation ? "Login__input" : "Login__input _err"}
  156. type={this.state.inputType} value={password}
  157. onChange={this.onChange('password')}
  158. />
  159. <span
  160. className= {this.state.inputType === 'input' ? "Login__passwordShow _hide" : "Login__passwordShow"}
  161. onClick={this.showHide}>
  162. </span>
  163. {!passwordValidation && error && <div className='Login__error'>{error}</div>}
  164. </label>
  165. </div>
  166. <button type="submit" className="Login__buttonLogin">Зарегистрироваться</button>
  167. </form>
  168. </div>
  169. <div className="Login__register">
  170. <div className="Login__registerTitle">
  171. Уже есть аккаунт?
  172. </div>
  173. <div
  174. className="Login__buttonLogin"
  175. onClick={() => this.nextRouter('/login')}
  176. >
  177. Войти
  178. </div>
  179. </div>
  180. </div>
  181. );
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement