burger-online
68 строк · 2.2 Кб
1import {Button, Input} from '@ya.praktikum/react-developer-burger-ui-components'
2import React from 'react'
3import {useSelector} from 'react-redux'
4import {Link} from 'react-router-dom'
5import PasswordInput from '../../components/UI/password-input/password-input'
6import {useAppDispatch, useForm} from '../../hooks'
7import {fetchLogin} from '../../redux/user/actions'
8import {catchError, userStore} from '../../redux/user/slice'
9import {THandleSubmitForm} from '../../types'
10
11const Login = () => {
12const dispatch = useAppDispatch()
13const {errorStatus} = useSelector(userStore)
14
15const {values, handleChanges} = useForm({
16email: sessionStorage.getItem('email') || '',
17password: '',
18})
19
20const handleSubmitLogin = (e: THandleSubmitForm) => {
21e.preventDefault()
22dispatch(fetchLogin(values))
23}
24
25React.useEffect(() => {
26setTimeout(() => {
27dispatch(catchError(null))
28}, 6000)
29}, [errorStatus, dispatch])
30
31return (
32<section className='wrapper'>
33<form className='content-route' onSubmit={handleSubmitLogin}>
34<h2 className='text text_type_main-medium'>Вход</h2>
35<Input
36autoFocus
37type='email'
38name='email'
39placeholder='E-mail'
40value={values.email}
41onChange={handleChanges}
42/>
43<PasswordInput placeholder='Пароль' value={values.password} onChange={handleChanges} />
44<Button htmlType='submit'>Войти</Button>
45<span
46className={`text text_type_main-default ${errorStatus ? 'text-error-active' : 'text-error'}`}
47>
48{errorStatus?.message}
49</span>
50</form>
51
52<p className='text text_type_main-default text_color_inactive mb-4'>
53Вы — новый пользователь?{' '}
54<Link to='/register' className='route-link text_color_accent'>
55Зарегистрироваться
56</Link>
57</p>
58<p className='text text_type_main-default text_color_inactive'>
59Забыли пароль?{' '}
60<Link to='/forgot-password' className='route-link text_color_accent'>
61Восстановить пароль
62</Link>
63</p>
64</section>
65)
66}
67
68export default Login
69