Projekt

Obecné

Profil

Stáhnout (3.17 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
            <p>Credentials = admin:password</p>
54

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

    
105
export default Login
(1-1/4)