LoginPage.jsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { actionFullLogin } from '../actions/actionLogin';
  2. import TextField from '@mui/material/TextField';
  3. import { IconButton, InputAdornment, Typography } from '@mui/material';
  4. import {BrowserRouter as Router, Link,} from 'react-router-dom';
  5. import { useState, useEffect } from 'react';
  6. import { Visibility, VisibilityOff } from '@mui/icons-material';
  7. import Button from '@mui/material/Button';
  8. import { connect } from 'react-redux';
  9. function LoginPage({onLogin }){
  10. const [values, setValues] = useState({
  11. login: '',
  12. password: '',
  13. showPassword: false,
  14. })
  15. const [errorValues, setErrorValues] = useState({
  16. login: '',
  17. firstPassword: '',
  18. })
  19. useEffect(() => {
  20. if(Object.values(errorValues).every(item => item === false)) {
  21. onLogin(values.login, values.password)
  22. }
  23. }, [errorValues])
  24. function validatePassword(fp,l) {
  25. let e = {
  26. login: '',
  27. firstPassword: '',
  28. }
  29. let errorsP = [];
  30. let errorsL = [];
  31. if (fp.length < 4 || fp.length > 12) {
  32. errorsP.push("Your password must be at least 4 and no more than 12 characters");
  33. }
  34. if (!/[a-z]/.test(fp)) {
  35. errorsP.push("Your password must contain at least one letter.");
  36. }
  37. if (!/[0-9]/.test(fp)) {
  38. errorsP.push("Your password must contain at least one digit.");
  39. }
  40. if (errorsP.length > 0) {
  41. e = {...e, firstPassword: errorsP.join('/')}
  42. } else {
  43. e = {...e, firstPassword: false}
  44. }
  45. if(!/[a-z]/.test(l)){
  46. errorsL.push("Your login must contain at least one letter");
  47. }
  48. if (l.length < 4 || l.length > 8) {
  49. errorsL.push("Your login must be at least 4 and no more than 8 characters");
  50. }
  51. if (errorsL.length > 0){
  52. e = {...e, login: errorsL.join('/')}
  53. } else {
  54. e = {...e, login: false}
  55. }
  56. setErrorValues({...errorValues, ...e})
  57. }
  58. const handleChange = (prop) => (event) => {
  59. setValues({...values, [prop] : event.target.value})
  60. }
  61. const handleClickShowPassword = () => {
  62. setValues({
  63. ...values,
  64. showPassword: !values.showPassword
  65. })
  66. }
  67. return (
  68. <div style={{height: '100vh', display: 'flex', alignItems: 'center'}}>
  69. <form className='Form' noValidate autoComplete='off'>
  70. <Typography sx={{mb: '30px'}} color='primary' variant='h4'>Login</Typography>
  71. <TextField
  72. error={!!errorValues.login}
  73. sx={{width: '100%', mb: '30px'}}
  74. required
  75. label="Login"
  76. variant="outlined"
  77. helperText={errorValues.login}
  78. value={values.login}
  79. autoFocus
  80. onChange={handleChange('login')}/>
  81. <TextField
  82. error={!!errorValues.firstPassword}
  83. sx={{width: '100%'}}
  84. helperText={errorValues.firstPassword}
  85. required
  86. variant="outlined"
  87. label="Password"
  88. type={values.showPassword ? 'text' : 'password'}
  89. value={values.password}
  90. onChange={handleChange('password')}
  91. InputProps={{
  92. endAdornment: (
  93. <InputAdornment position="end">
  94. <IconButton
  95. aria-label="toggle password visibility"
  96. onClick={handleClickShowPassword}
  97. onMouseDown={(event) => event.preventDefault()}
  98. edge="end"
  99. >
  100. {values.showPassword ? <VisibilityOff/> : <Visibility/>}
  101. </IconButton>
  102. </InputAdornment>
  103. )
  104. }}
  105. />
  106. <Button
  107. onClick={() => {validatePassword( values.password, values.login)}
  108. }
  109. sx={{width: '200px', mt: '30px'}}
  110. variant="contained">
  111. Submit
  112. </Button><br/>
  113. <Link className='RegisterBtn' to='/register'>Registration</Link>
  114. </form>
  115. </div>
  116. )
  117. }
  118. export default connect(null, {onLogin: actionFullLogin})(LoginPage)