vkrastev97

reg

Nov 1st, 2020
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. import React, { useState, useContext, Fragment } from 'react';
  2. // import { Form, Button } from 'react-bootstrap';
  3. // import { useForm, useFormContext } from 'react-hook-form';
  4. import './Register.css';
  5. import { useHistory } from 'react-router-dom';
  6. // import { ErrorMessage } from '@hookform/error-message';
  7. // import AuthContext from '../Authentication/Authentication';
  8. /* eslint-disable */
  9. const Register = () => {
  10. const history = useHistory();
  11.  
  12. const [isFormValid, setIsFormValid] = useState(false);
  13. const [authForm, setAuthForm] = useState({
  14. username: {
  15. name: 'username',
  16. placeholder: 'Enter name',
  17. value: '',
  18. type: 'text',
  19. validation: {
  20. required: true,
  21. minLength: 6,
  22. maxLength: 20,
  23. },
  24. valid: false,
  25. touched: false,
  26. },
  27. password: {
  28. name: 'password',
  29. placeholder: 'Enter password',
  30. value: '',
  31. type: 'password',
  32. validation: {
  33. required: true,
  34. minLength: 6,
  35. maxLength: 30,
  36. },
  37. valid: false,
  38. touched: false,
  39. },
  40. });
  41.  
  42. const isInputValid = (input, validations) => {
  43. let isValid = true;
  44.  
  45. if (validations.required) {
  46. isValid = isValid && input.length !== 0;
  47. }
  48. if (validations.minLength) {
  49. isValid = isValid && input.length >= validations.minLength;
  50. }
  51. if (validations.maxLength) {
  52. isValid = isValid && input.length <= validations.maxLength;
  53. }
  54.  
  55. return isValid;
  56. };
  57.  
  58. const handleInputChange = (event) => {
  59. const { name, value } = event.target;
  60. const updatedControl = { ...authForm[name] };
  61.  
  62. updatedControl.value = value;
  63. updatedControl.touched = true;
  64. updatedControl.valid = isInputValid(value, updatedControl.validation);
  65.  
  66. const updatedForm = { ...authForm, [name]: updatedControl };
  67. setAuthForm(updatedForm);
  68.  
  69. const formValid = Object.values(updatedForm).every(
  70. (control) => control.valid
  71. );
  72. setIsFormValid(formValid);
  73. };
  74.  
  75. const registerUser = (ev) => {
  76. ev.preventDefault();
  77. const path = `/login`;
  78. history.push(path);
  79. const username = authForm.username.value;
  80. const password = authForm.password.value;
  81. if (username && password) {
  82. fetch('http://localhost:4000/users', {
  83. method: 'POST',
  84. headers: { 'Content-Type': 'application/json' },
  85. body: JSON.stringify({ username, password }),
  86. })
  87. .then((r) => r.json())
  88. .then((data) => {
  89. if (data.message) {
  90. alert(data.message);
  91. } else {
  92. console.log(data);
  93. }
  94. });
  95. }
  96. };
  97.  
  98. const formElements = Object.keys(authForm)
  99. .map((name) => ({ name, config: authForm[name] }))
  100. .map(({ name, config }) => {
  101. const isValidClass = config.valid ? 'valid' : 'invalid';
  102. const isTouchedClass = config.touched ? 'touched' : 'untouched';
  103. const classes = [isValidClass, isTouchedClass].join(' ');
  104.  
  105. return (
  106. <Fragment key={name}>
  107. <label name={name}>{name} </label>
  108. <input
  109. type={config.type}
  110. name={name}
  111. className={classes}
  112. placeholder={config.placeholder}
  113. value={config.value}
  114. onChange={handleInputChange}
  115. />
  116. </Fragment>
  117. );
  118. });
  119.  
  120. return (
  121. <div className='regdiv'>
  122. <h1>Register</h1>
  123.  
  124. <form onSubmit={registerUser}>
  125. {formElements}
  126. <button type='submit' disabled={!isFormValid}>
  127. Submit!
  128. </button>
  129. </form>
  130. </div>
  131. );
  132. };
  133.  
  134. export default Register;
  135.  
Advertisement
Add Comment
Please, Sign In to add comment