Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 6129910f

Přidáno uživatelem Václav Honzík před asi 2 roky(ů)

Show dialog in login page

re #9628

Zobrazit rozdíly:

frontend/src/features/Auth/Login.tsx
1
import { Button, TextField, Typography } from '@mui/material'
2
import { useFormik } from 'formik'
3 1
import { Fragment, useEffect } from 'react'
4
import { useDispatch, useSelector } from 'react-redux'
2
import { useSelector } from 'react-redux'
5 3
import { useNavigate } from 'react-router-dom'
6
import * as yup from 'yup'
7
import { SchemaOf } from 'yup'
8 4
import { RootState } from '../redux/store'
9
import { logIn } from './userThunks'
5
import LoginDialog from './LoginDialog'
10 6

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

  
16 8
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 9
    const userLoggedIn = useSelector(
41 10
        (state: RootState) => state.user.isLoggedIn
42 11
    )
12

  
13
    // Redirect to home if the user is logged in
43 14
    const navigate = useNavigate()
44 15
    useEffect(() => {
45 16
        if (userLoggedIn) {
......
49 20

  
50 21
    return (
51 22
        <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>
23
            <LoginDialog />
100 24
        </Fragment>
101 25
    )
102 26
}

Také k dispozici: Unified diff