index(copy).js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { store } from '../redux';
  2. import { useState } from "react";
  3. import { Link, Redirect, useHistory } from "react-router-dom";
  4. import { actionFullLogin } from "../redux/action";
  5. import { Paper, Box, Typography, Button, Stack, OutlinedInput, IconButton, InputLabel, InputAdornment, FormControl } from "@mui/material";
  6. import { Visibility, VisibilityOff } from '@mui/icons-material';
  7. // поля логин/пароль/кнопка входа
  8. const LoginForm = ({ onLogin }) => {
  9. // отслеживаем введение данных в полях логин/пароль
  10. const [login, setLogin] = useState('')
  11. const [pass, setPass] = useState('')
  12. // включаем,выклюаем отображение пароля
  13. const [showPassword, setShowPassword] = useState(false);
  14. const handleClickShowPassword = () => setShowPassword((show) => !show);
  15. const handleMouseDownPassword = (event) => {
  16. event.preventDefault();
  17. };
  18. return (
  19. <Box sx={{ width: '100%' }}>
  20. <Stack spacing={2}>
  21. <FormControl variant="outlined">
  22. <InputLabel htmlFor="outlined-adornment-password">Имя пользователя</InputLabel>
  23. <OutlinedInput
  24. id="login"
  25. label="Имя пользователя"
  26. type="text"
  27. size="small"
  28. value={login}
  29. onChange={e => setLogin(e.target.value)}
  30. />
  31. </FormControl>
  32. <FormControl variant="outlined">
  33. <InputLabel htmlFor="outlined-adornment-password">Пароль</InputLabel>
  34. <OutlinedInput
  35. id="password"
  36. label="Пароль"
  37. size="small"
  38. type={showPassword ? 'text' : 'password'}
  39. value={pass}
  40. onChange={e => setPass(e.target.value)}
  41. endAdornment={
  42. <InputAdornment position="end">
  43. <IconButton
  44. aria-label="toggle password visibility"
  45. onClick={handleClickShowPassword}
  46. onMouseDown={handleMouseDownPassword}
  47. edge="end"
  48. >
  49. {showPassword ? <VisibilityOff /> : <Visibility />}
  50. </IconButton>
  51. </InputAdornment>
  52. }
  53. />
  54. </FormControl>
  55. <Button
  56. variant="contained"
  57. disabled={((login == '') || (pass == '')) || false}
  58. onClick={() => onLogin(login, pass)}
  59. >
  60. Войти
  61. </Button>
  62. </Stack>
  63. </Box>
  64. )
  65. }
  66. // Вся страница формы
  67. export default function Authorization() {
  68. const history = useHistory();
  69. return (
  70. <Box sx={{
  71. display: 'grid',
  72. height: '80vh',
  73. justifyContent: 'center',
  74. alignItems: 'center'
  75. }}>
  76. <Paper elevation={3}
  77. sx={{
  78. padding: '5px',
  79. width: '400px',
  80. height: '500px',
  81. display: 'grid',
  82. justifyContent: 'center',
  83. alignItems: 'center'
  84. }}>
  85. <Typography
  86. variant="h4"
  87. noWrap
  88. sx={{
  89. display: 'grid',
  90. fontFamily: 'monospace',
  91. fontWeight: 700,
  92. letterSpacing: '.3rem',
  93. color: 'inherit',
  94. textDecoration: 'none',
  95. flexGrow: '1',
  96. marginTop: '20px',
  97. justifyContent: 'center'
  98. }} >
  99. Hipstagram
  100. </Typography>
  101. <Box
  102. sx={{
  103. display: 'grid',
  104. alignSelf: 'start'
  105. }}>
  106. <LoginForm
  107. onLogin={async (login, pass) => {
  108. await store.dispatch(actionFullLogin(login, pass))
  109. if ((Object.keys(store.getState().auth)).length) {
  110. history.push('/')
  111. }
  112. }}
  113. />
  114. </Box>
  115. <Typography
  116. variant="subtitle1"
  117. sx={{
  118. display: 'grid',
  119. alignSelf: 'end',
  120. justifyContent: 'center',
  121. }}>
  122. <Link
  123. color="inherit"
  124. to='/registration'
  125. >
  126. Зарегистрироваться
  127. </Link>
  128. </Typography>
  129. </Paper>
  130. </Box >
  131. )
  132. }