RegisterPage.jsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { TextField, Typography, IconButton, InputAdornment, Grid, Button } from "@mui/material"
  2. import { useEffect, useState } from "react"
  3. import { actionFullRegister } from "../actions/actionLogin";
  4. import { connect } from "react-redux";
  5. import { Link } from "react-router-dom";
  6. function RegisterPage ({onRegister}) {
  7. const [values, setValues] = useState({
  8. login: '',
  9. firstPassword: '',
  10. secondPassword: '',
  11. nick: '',
  12. showPassword: ''
  13. })
  14. const [errorValues, setErrorValues] = useState({
  15. login: '',
  16. firstPassword: '',
  17. secondPassword: '',
  18. nick: ''
  19. })
  20. useEffect(() => {
  21. if(Object.values(errorValues).every(item => item === false)) {
  22. onRegister(values.login, values.firstPassword, values.nick)
  23. }
  24. }, [errorValues])
  25. function validatePassword(fp,l,n,sp) {
  26. let e = {
  27. login: '',
  28. firstPassword: '',
  29. secondPassword: '',
  30. nick: ''
  31. }
  32. let errorsP = [];
  33. let errorsL = [];
  34. let errorsN = [];
  35. let errorsSP = [];
  36. if (fp.length < 4 || fp.length > 12) {
  37. errorsP.push("Your password must be at least 4 and no more than 12 characters");
  38. }
  39. if (!/[a-z]/.test(fp)) {
  40. errorsP.push("Your password must contain at least one letter.");
  41. }
  42. if (!/[0-9]/.test(fp)) {
  43. errorsP.push("Your password must contain at least one digit.");
  44. }
  45. if (errorsP.length > 0) {
  46. e = {...e, firstPassword: errorsP.join('/')}
  47. } else {
  48. e = {...e, firstPassword: false}
  49. }
  50. if(!/[a-z]/.test(l)){
  51. errorsL.push("Your login must contain at least one letter");
  52. }
  53. if (l.length < 4 || l.length > 8) {
  54. errorsL.push("Your login must be at least 4 and no more than 8 characters");
  55. }
  56. if (errorsL.length > 0){
  57. e = {...e, login: errorsL.join('/')}
  58. } else {
  59. e = {...e, login: false}
  60. }
  61. if(!/[a-z]/.test(n)){
  62. errorsN.push("Your nick must contain at least one letter");
  63. }
  64. if (n.length < 3 || n.length > 15) {
  65. errorsN.push("Your nick must be at least 3 and no more than 15 characters");
  66. }
  67. if (errorsN.length > 0){
  68. e = {...e, nick: errorsN.join('/')}
  69. } else {
  70. e = {...e, nick: false}
  71. }
  72. if(fp !== sp){
  73. errorsSP.push("Password not match")
  74. }
  75. if (errorsSP.length > 0){
  76. e = {...e, secondPassword: errorsSP.join('/')}
  77. } else {
  78. e = {...e, secondPassword: false}
  79. }
  80. setErrorValues({...errorValues, ...e})
  81. }
  82. const handleChange = (prop) => (e) => {
  83. setValues({...values, [prop] : e.target.value})
  84. }
  85. return (
  86. <div className="RegisterPage">
  87. <form className="Form" noValidate autoComplete="off">
  88. <Typography sx={{mb: '20px'}} color='primary' variant='h4'>Registration form</Typography>
  89. <TextField
  90. error={!!errorValues.login}
  91. sx={{width: '100%', mb: (!!errorValues.login) ? '10px' : '30px'}}
  92. required
  93. label="Login"
  94. variant="outlined"
  95. helperText={errorValues.login}
  96. value={values.login}
  97. autoFocus
  98. onChange={handleChange('login')}
  99. />
  100. <TextField
  101. error={!!errorValues.nick}
  102. sx={{width: '100%', mb: !!errorValues.nick ? '10px' : '30px'}}
  103. label="Nick"
  104. helperText={errorValues.nick}
  105. required
  106. variant="outlined"
  107. value={values.nick}
  108. autoFocus
  109. onChange={handleChange('nick')}
  110. />
  111. <TextField
  112. error={!!errorValues.firstPassword}
  113. sx={{width: '100%', mb: !!errorValues.firstPassword ? '10px' : '30px'}}
  114. helperText={errorValues.firstPassword}
  115. required
  116. variant="outlined"
  117. label="Password"
  118. type={'password'}
  119. value={values.firstPassword}
  120. onChange={handleChange('firstPassword')}
  121. />
  122. <TextField
  123. error={!!errorValues.secondPassword}
  124. sx={{width: '100%', mb: !!errorValues.secondPassword ? '10px' : '30px'}}
  125. helperText={errorValues.secondPassword}
  126. required
  127. variant="outlined"
  128. label="Confirm password "
  129. type={'password'}
  130. value={values.secondPassword}
  131. onChange={handleChange('secondPassword')}
  132. />
  133. <Button
  134. onClick={() => { validatePassword(values.firstPassword, values.login, values.nick, values.secondPassword)}}
  135. sx={{width: '200px', mt: '30px'}}
  136. variant="contained">
  137. Submit
  138. </Button><br/>
  139. <Link className='RegisterBtn' to='/login'>Login</Link>
  140. </form>
  141. </div>
  142. )
  143. }
  144. export default connect(null, {onRegister: actionFullRegister})(RegisterPage)