Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.36 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { signup, checkUsernameAvailability, checkEmailAvailability } from '../../util/APIUtils';
  3. import './Signup.css';
  4. import { Link } from 'react-router-dom';
  5. import {
  6. NAME_MIN_LENGTH, NAME_MAX_LENGTH,
  7. USERNAME_MIN_LENGTH, USERNAME_MAX_LENGTH,
  8. EMAIL_MAX_LENGTH,
  9. PASSWORD_MIN_LENGTH, PASSWORD_MAX_LENGTH
  10. } from '../../constants';
  11.  
  12. import { Form, Input, Button, notification } from 'antd';
  13. const FormItem = Form.Item;
  14.  
  15. class Signup extends Component {
  16. constructor(props) {
  17. super(props);
  18. this.state = {
  19. name: {
  20. value: ''
  21. },
  22. username: {
  23. value: ''
  24. },
  25. email: {
  26. value: ''
  27. },
  28. password: {
  29. value: ''
  30. }
  31. }
  32. this.handleInputChange = this.handleInputChange.bind(this);
  33. this.handleSubmit = this.handleSubmit.bind(this);
  34. this.validateUsernameAvailability = this.validateUsernameAvailability.bind(this);
  35. this.validateEmailAvailability = this.validateEmailAvailability.bind(this);
  36. this.isFormInvalid = this.isFormInvalid.bind(this);
  37. }
  38.  
  39. handleInputChange(event, validationFun) {
  40. const target = event.target;
  41. const inputName = target.name;
  42. const inputValue = target.value;
  43.  
  44. this.setState({
  45. [inputName] : {
  46. value: inputValue,
  47. ...validationFun(inputValue)
  48. }
  49. });
  50. }
  51.  
  52. handleSubmit(event) {
  53. event.preventDefault();
  54.  
  55. const signupRequest = {
  56. name: this.state.name.value,
  57. email: this.state.email.value,
  58. username: this.state.username.value,
  59. password: this.state.password.value
  60. };
  61. signup(signupRequest)
  62. .then(response => {
  63. notification.success({
  64. message: 'Demo Drop',
  65. description: "Je bent succesvol geregistreerd!",
  66. });
  67. this.props.history.push("/login");
  68. }).catch(error => {
  69. notification.error({
  70. message: 'Demo Drop',
  71. description: error.message || 'Sorry! Er is iets misgegaan. Probeer het opnieuw.'
  72. });
  73. });
  74. }
  75.  
  76. isFormInvalid() {
  77. return !(this.state.name.validateStatus === 'success' &&
  78. this.state.username.validateStatus === 'success' &&
  79. this.state.email.validateStatus === 'success' &&
  80. this.state.password.validateStatus === 'success'
  81. );
  82. }
  83.  
  84. render() {
  85. return (
  86. <div className="signup-container">
  87. <h1 className="page-title">Registreren</h1>
  88. <div className="signup-content">
  89. <Form onSubmit={this.handleSubmit} className="signup-form">
  90. <FormItem
  91. validateStatus={this.state.name.validateStatus}
  92. help={this.state.name.errorMsg}>
  93. <Input
  94. size="large"
  95. name="name"
  96. autoComplete="off"
  97. placeholder="Volledige naam"
  98. value={this.state.name.value}
  99. onChange={(event) => this.handleInputChange(event, this.validateName)} />
  100. </FormItem>
  101. <FormItem
  102. hasFeedback
  103. validateStatus={this.state.username.validateStatus}
  104. help={this.state.username.errorMsg}>
  105. <Input
  106. size="large"
  107. name="username"
  108. autoComplete="off"
  109. placeholder="Gebruikersnaam"
  110. value={this.state.username.value}
  111. onBlur={this.validateUsernameAvailability}
  112. onChange={(event) => this.handleInputChange(event, this.validateUsername)}/>
  113. </FormItem>
  114. <FormItem
  115. hasFeedback
  116. validateStatus={this.state.email.validateStatus}
  117. help={this.state.email.errorMsg}>
  118. <Input
  119. size="large"
  120. name="email"
  121. type="email"
  122. autoComplete="off"
  123. placeholder="E-mailadres"
  124. value={this.state.email.value}
  125. onBlur={this.validateEmailAvailability}
  126. onChange={(event) => this.handleInputChange(event, this.validateEmail)}/>
  127.  
  128. </FormItem>
  129. <FormItem
  130. validateStatus={this.state.password.validateStatus}
  131. help={this.state.password.errorMsg}>
  132. <Input
  133. size="large"
  134. name="password"
  135. type="password"
  136. autoComplete="off"
  137. placeholder="Wachtwoord moet 6 tot 20 tekens bevatten"
  138. value={this.state.password.value}
  139.  
  140. onChange={(event) => this.handleInputChange(event, this.validatePassword)}/>
  141. </FormItem>
  142. <FormItem>
  143. <Button type="primary"
  144. htmlType="submit"
  145. size="large"
  146. className="signup-form-button"
  147. disabled={this.isFormInvalid()}>registreer</Button>
  148. Al geregistreerd? <Link to="/login">Login!</Link>
  149. </FormItem>
  150. </Form>
  151. </div>
  152. </div>
  153. );
  154. }
  155.  
  156.  
  157. // Validatie functies
  158.  
  159.  
  160. validateName = (name) => {
  161. if(name.length < NAME_MIN_LENGTH) {
  162. return {
  163. validateStatus: 'error',
  164. errorMsg: `Naam te kort (Minimaal ${NAME_MIN_LENGTH} karakters invoeren.)`
  165. }
  166. } else if (name.length > NAME_MAX_LENGTH) {
  167. return {
  168. validationStatus: 'error',
  169. errorMsg: `Naam is te lang (Maximaal ${NAME_MAX_LENGTH} karakters toegestaan.)`
  170. }
  171. } else {
  172. return {
  173. validateStatus: 'success',
  174. errorMsg: null,
  175. };
  176. }
  177. };
  178.  
  179. validateEmail = (email) => {
  180. if (!email) {
  181.  
  182. return {
  183. validateStatus: 'error',
  184. errorMsg: 'Email-veld mag niet leeg zijn.'
  185. }
  186. }
  187.  
  188. const EMAIL_REGEX = RegExp('[^@ ]+@[^@ ]+\\.[^@ ]+');
  189.  
  190. if (!EMAIL_REGEX.test(email)) {
  191. return {
  192. validateStatus: 'error',
  193. errorMsg: 'Email niet valide'
  194. }
  195. }
  196.  
  197.  
  198. if (email.length > EMAIL_MAX_LENGTH) {
  199. return {
  200. validateStatus: 'error',
  201. errorMsg: `Email te lang (Maximaal ${EMAIL_MAX_LENGTH} karakters toegestaan)`
  202. }
  203. }
  204.  
  205. return {
  206. validateStatus: null,
  207. errorMsg: null
  208. }
  209.  
  210. };
  211.  
  212. validateUsername = (username) => {
  213. if (username.length < USERNAME_MIN_LENGTH) {
  214. return {
  215. validateStatus: 'error',
  216. errorMsg: `Gebruikersnaam is te kort (Minimaal ${USERNAME_MIN_LENGTH} karakters.)`
  217. }
  218. } else if (username.length > USERNAME_MAX_LENGTH) {
  219. return {
  220. validationStatus: 'error',
  221. errorMsg: `Gebruikersnaam is te lang (Maximaal ${USERNAME_MAX_LENGTH} karakters toegestaan.)`
  222. }
  223. } else {
  224. return {
  225. validateStatus: null,
  226. errorMsg: null
  227. }
  228. }
  229. };
  230.  
  231. validateUsernameAvailability() {
  232. // First check for client side errors in username
  233. const usernameValue = this.state.username.value;
  234. const usernameValidation = this.validateUsername(usernameValue);
  235.  
  236. if (usernameValidation.validateStatus === 'error') {
  237. this.setState({
  238. username: {
  239. value: usernameValue,
  240. ...usernameValidation
  241. }
  242. });
  243. return;
  244. }
  245.  
  246. this.setState({
  247. username: {
  248. value: usernameValue,
  249. validateStatus: 'validating',
  250. errorMsg: null
  251. }
  252. });
  253.  
  254. checkUsernameAvailability(usernameValue)
  255. .then(response => {
  256. if (response.available) {
  257. this.setState({
  258. username: {
  259. value: usernameValue,
  260. validateStatus: 'success',
  261. errorMsg: null
  262. }
  263. });
  264. } else {
  265. this.setState({
  266. username: {
  267. value: usernameValue,
  268. validateStatus: 'error',
  269. errorMsg: 'Deze gebruikersnaam is al in gebruik'
  270. }
  271. });
  272. }
  273. }).catch(error => {
  274. this.setState({
  275. username: {
  276. value: usernameValue,
  277. validateStatus: 'success',
  278. errorMsg: null
  279. }
  280. });
  281. });
  282. };
  283.  
  284.  
  285. validateEmailAvailability() {
  286. const emailValue = this.state.email.value;
  287. const emailValidation = this.validateEmail(emailValue);
  288.  
  289.  
  290. if (emailValidation.validateStatus === 'error') {
  291.  
  292. this.setState({
  293. email: {
  294. value: emailValue,
  295. ...emailValidation
  296. }
  297. });
  298. return;
  299. }
  300.  
  301. this.setState({
  302. email: {
  303. value: emailValue,
  304. validateStatus: 'validating',
  305. errorMsg: null
  306. }
  307. });
  308.  
  309. checkEmailAvailability(emailValue)
  310.  
  311. .then(response => {
  312. if (response.available) {
  313. this.setState({
  314. email: {
  315. value: emailValue,
  316. validateStatus: 'success',
  317. errorMsg: null
  318. }
  319. });
  320. } else {
  321. this.setState({
  322. email: {
  323. value: emailValue,
  324. validateStatus: 'error',
  325. errorMsg: 'Dit e-mailadres is al in gebruik.'
  326. }
  327. });
  328. }
  329. }).catch(error => {
  330. this.setState({
  331. email: {
  332. value: emailValue,
  333. validateStatus: 'success',
  334. errorMsg: null
  335. }
  336. });
  337. });
  338. }
  339.  
  340. validatePassword = (password) => {
  341.  
  342. if (password.length < PASSWORD_MIN_LENGTH) {
  343. return {
  344. validateStatus: 'error',
  345. errorMsg: `Wachtwoord is te kort! (Minimaal ${PASSWORD_MIN_LENGTH} karakters lang.)`
  346.  
  347. }
  348. } else if (password.length > PASSWORD_MAX_LENGTH) {
  349. return {
  350. validationStatus: 'error',
  351.  
  352. errorMsg: `Wachtwoord is te lang (Maximaal ${PASSWORD_MAX_LENGTH} karakters lang.)`
  353. }
  354. } else {
  355. return {
  356. validateStatus: 'success',
  357. errorMsg: null,
  358. };
  359. }
  360. }
  361.  
  362. }
  363.  
  364. export default Signup;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement