Projekt

Obecné

Profil

Stáhnout (3.13 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Button, TextField, Typography } from '@mui/material'
2
import { useFormik } from 'formik'
3
import { Fragment, useEffect } from 'react'
4
import { useDispatch, useSelector } from 'react-redux'
5
import { useNavigate } from 'react-router-dom'
6
import * as yup from 'yup'
7
import { SchemaOf } from 'yup'
8
import { RootState } from '../redux/store'
9
import { logIn } from './userThunks'
10

    
11
interface LoginFields {
12
    username: string
13
    password: string
14
}
15

    
16
const Login = () => {
17
    const validationSchema: SchemaOf<LoginFields> = yup.object().shape({
18
        username: yup.string().required('Username is required'),
19
        password: yup.string().required('Password is required'),
20
    })
21

    
22
    const dispatch = useDispatch()
23
    const formik = useFormik({
24
        initialValues: {
25
            username: '',
26
            password: '',
27
        },
28
        validationSchema,
29
        onSubmit: () => {
30
            dispatch(
31
                logIn({
32
                    username: formik.values.username,
33
                    password: formik.values.password,
34
                })
35
            )
36
        },
37
    })
38

    
39
    // Redirect to home if the user is logged in
40
    const userLoggedIn = useSelector(
41
        (state: RootState) => state.user.isLoggedIn
42
    )
43
    const navigate = useNavigate()
44
    useEffect(() => {
45
        if (userLoggedIn) {
46
            navigate('/')
47
        }
48
    }, [userLoggedIn, navigate])
49

    
50
    return (
51
        <Fragment>
52
            <Typography variant="h3">Login</Typography>
53

    
54
            <form onSubmit={formik.handleSubmit}>
55
                <TextField
56
                    label="Username"
57
                    name="username"
58
                    fullWidth
59
                    sx={{ mb: 2 }}
60
                    value={formik.values.username}
61
                    onChange={formik.handleChange}
62
                    error={
63
                        Boolean(formik.errors.username) &&
64
                        formik.touched.username
65
                    }
66
                    helperText={
67
                        formik.errors.username &&
68
                        formik.touched.username &&
69
                        formik.errors.username
70
                    }
71
                />
72
                <TextField
73
                    type="password"
74
                    label="Password"
75
                    name="password"
76
                    fullWidth
77
                    value={formik.values.password}
78
                    onChange={formik.handleChange}
79
                    error={
80
                        Boolean(formik.errors.password) &&
81
                        formik.touched.password
82
                    }
83
                    helperText={
84
                        formik.errors.password &&
85
                        formik.touched.password &&
86
                        formik.errors.password
87
                    }
88
                    sx={{ mb: 2 }}
89
                />
90
                <Button
91
                    size="large"
92
                    variant="contained"
93
                    color="primary"
94
                    type="submit"
95
                    fullWidth
96
                >
97
                    Login
98
                </Button>
99
            </form>
100
        </Fragment>
101
    )
102
}
103

    
104
export default Login
(1-1/5)