Projekt

Obecné

Profil

Stáhnout (4.22 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Fragment, FunctionComponent, useEffect, useState } from 'react'
2
import Dialog, { DialogProps } from '@mui/material/Dialog'
3
import {
4
    Button,
5
    DialogContent,
6
    Link,
7
    TextField,
8
    Typography,
9
} from '@mui/material'
10
import { useFormik } from 'formik'
11
import * as yup from 'yup'
12
import { useDispatch, useSelector } from 'react-redux'
13
import { Link as RouterLink, useNavigate } from 'react-router-dom'
14
import { logIn } from './userThunks'
15
import { RootState } from '../redux/store'
16
import { resetLoggingIn } from './userSlice'
17

    
18
export interface CreateIndexDialogProps {
19
    maxWidth?: DialogProps['maxWidth']
20
}
21

    
22
const LoginDialog: FunctionComponent<CreateIndexDialogProps> = ({
23
    maxWidth,
24
}) => {
25
    const [open, setOpen] = useState(true)
26

    
27
    const dispatch = useDispatch()
28
    const navigate = useNavigate()
29
    dispatch(resetLoggingIn())
30
    const validationSchema = yup.object().shape({
31
        username: yup.string().required('Username is required'),
32
        password: yup.string().required('Password is required'),
33
    })
34

    
35
    const isLoggingIn = useSelector(
36
        (state: RootState) => state.user.isLoggingIn
37
    )
38

    
39
    const formik = useFormik({
40
        initialValues: {
41
            username: '',
42
            password: '',
43
        },
44
        validationSchema,
45
        onSubmit: () => {
46
            dispatch(
47
                logIn({
48
                    username: formik.values.username,
49
                    password: formik.values.password,
50
                })
51
            )
52
        },
53
    })
54

    
55
    const onCancel = () => {
56
        formik.resetForm()
57
        navigate('/')
58
    }
59

    
60
    return (
61
        <Fragment>
62
            <Dialog
63
                open={open}
64
                fullWidth={true}
65
                onClose={onCancel}
66
                maxWidth="md"
67
            >
68
                <Typography sx={{ ml: 2, mt: 2 }} variant="h5" fontWeight="600">
69
                    Login
70
                </Typography>
71
                <DialogContent>
72
                    <form onSubmit={formik.handleSubmit}>
73
                        <TextField
74
                            fullWidth
75
                            label="Name"
76
                            name="username"
77
                            sx={{ mb: 2 }}
78
                            value={formik.values.username}
79
                            onChange={formik.handleChange}
80
                            error={
81
                                Boolean(formik.errors.username) &&
82
                                formik.touched.username
83
                            }
84
                            helperText={
85
                                formik.errors.username &&
86
                                formik.touched.username &&
87
                                formik.errors.username
88
                            }
89
                        />
90
                        <TextField
91
                            fullWidth
92
                            label="Password"
93
                            name="password"
94
                            type="password"
95
                            sx={{ mb: 2 }}
96
                            value={formik.values.password}
97
                            onChange={formik.handleChange}
98
                            error={
99
                                Boolean(formik.errors.password) &&
100
                                formik.touched.password
101
                            }
102
                            helperText={
103
                                formik.errors.password &&
104
                                formik.touched.password &&
105
                                formik.errors.password
106
                            }
107
                        />
108
                        <Fragment>
109
                            <Button
110
                                type="submit"
111
                                variant="contained"
112
                                fullWidth
113
                                disabled={isLoggingIn}
114
                            >
115
                                Log in
116
                            </Button>
117
                        </Fragment>
118
                    </form>
119

    
120
                    <Link component={RouterLink} to="/resetPassword">
121
                        Forgot password?
122
                    </Link>
123
                </DialogContent>
124
            </Dialog>
125
        </Fragment>
126
    )
127
}
128

    
129
export default LoginDialog
(2-2/7)