1 |
6129910f
|
Vaclav Honzik
|
import { Fragment, FunctionComponent, useEffect, useState } from 'react'
|
2 |
4f42fa52
|
Václav Honzík
|
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 |
6129910f
|
Vaclav Honzik
|
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 |
f2733d24
|
Vaclav Honzik
|
import { Box } from '@mui/system'
|
18 |
4f42fa52
|
Václav Honzík
|
|
19 |
f2733d24
|
Vaclav Honzik
|
export interface LoginDialogProps {
|
20 |
4f42fa52
|
Václav Honzík
|
maxWidth?: DialogProps['maxWidth']
|
21 |
|
|
}
|
22 |
|
|
|
23 |
f2733d24
|
Vaclav Honzik
|
const LoginDialog: FunctionComponent<LoginDialogProps> = ({
|
24 |
4f42fa52
|
Václav Honzík
|
maxWidth,
|
25 |
|
|
}) => {
|
26 |
6129910f
|
Vaclav Honzik
|
const [open, setOpen] = useState(true)
|
27 |
4f42fa52
|
Václav Honzík
|
|
28 |
|
|
const dispatch = useDispatch()
|
29 |
6129910f
|
Vaclav Honzik
|
const navigate = useNavigate()
|
30 |
|
|
dispatch(resetLoggingIn())
|
31 |
4f42fa52
|
Václav Honzík
|
const validationSchema = yup.object().shape({
|
32 |
6129910f
|
Vaclav Honzik
|
username: yup.string().required('Username is required'),
|
33 |
4f42fa52
|
Václav Honzík
|
password: yup.string().required('Password is required'),
|
34 |
|
|
})
|
35 |
|
|
|
36 |
6129910f
|
Vaclav Honzik
|
const isLoggingIn = useSelector(
|
37 |
|
|
(state: RootState) => state.user.isLoggingIn
|
38 |
|
|
)
|
39 |
|
|
|
40 |
4f42fa52
|
Václav Honzík
|
const formik = useFormik({
|
41 |
|
|
initialValues: {
|
42 |
6129910f
|
Vaclav Honzik
|
username: '',
|
43 |
4f42fa52
|
Václav Honzík
|
password: '',
|
44 |
|
|
},
|
45 |
|
|
validationSchema,
|
46 |
6129910f
|
Vaclav Honzik
|
onSubmit: () => {
|
47 |
|
|
dispatch(
|
48 |
|
|
logIn({
|
49 |
|
|
username: formik.values.username,
|
50 |
|
|
password: formik.values.password,
|
51 |
|
|
})
|
52 |
|
|
)
|
53 |
4f42fa52
|
Václav Honzík
|
},
|
54 |
|
|
})
|
55 |
|
|
|
56 |
6129910f
|
Vaclav Honzik
|
const onCancel = () => {
|
57 |
4f42fa52
|
Václav Honzík
|
formik.resetForm()
|
58 |
6129910f
|
Vaclav Honzik
|
navigate('/')
|
59 |
4f42fa52
|
Václav Honzík
|
}
|
60 |
|
|
|
61 |
|
|
return (
|
62 |
|
|
<Fragment>
|
63 |
|
|
<Dialog
|
64 |
|
|
open={open}
|
65 |
|
|
fullWidth={true}
|
66 |
6129910f
|
Vaclav Honzik
|
onClose={onCancel}
|
67 |
|
|
maxWidth="md"
|
68 |
4f42fa52
|
Václav Honzík
|
>
|
69 |
|
|
<Typography sx={{ ml: 2, mt: 2 }} variant="h5" fontWeight="600">
|
70 |
|
|
Login
|
71 |
|
|
</Typography>
|
72 |
|
|
<DialogContent>
|
73 |
|
|
<form onSubmit={formik.handleSubmit}>
|
74 |
|
|
<TextField
|
75 |
|
|
fullWidth
|
76 |
|
|
label="Name"
|
77 |
6129910f
|
Vaclav Honzik
|
name="username"
|
78 |
4f42fa52
|
Václav Honzík
|
sx={{ mb: 2 }}
|
79 |
6129910f
|
Vaclav Honzik
|
value={formik.values.username}
|
80 |
4f42fa52
|
Václav Honzík
|
onChange={formik.handleChange}
|
81 |
|
|
error={
|
82 |
6129910f
|
Vaclav Honzik
|
Boolean(formik.errors.username) &&
|
83 |
|
|
formik.touched.username
|
84 |
4f42fa52
|
Václav Honzík
|
}
|
85 |
|
|
helperText={
|
86 |
6129910f
|
Vaclav Honzik
|
formik.errors.username &&
|
87 |
|
|
formik.touched.username &&
|
88 |
|
|
formik.errors.username
|
89 |
4f42fa52
|
Václav Honzík
|
}
|
90 |
|
|
/>
|
91 |
|
|
<TextField
|
92 |
|
|
fullWidth
|
93 |
|
|
label="Password"
|
94 |
|
|
name="password"
|
95 |
|
|
type="password"
|
96 |
|
|
sx={{ mb: 2 }}
|
97 |
|
|
value={formik.values.password}
|
98 |
|
|
onChange={formik.handleChange}
|
99 |
|
|
error={
|
100 |
|
|
Boolean(formik.errors.password) &&
|
101 |
|
|
formik.touched.password
|
102 |
|
|
}
|
103 |
|
|
helperText={
|
104 |
|
|
formik.errors.password &&
|
105 |
|
|
formik.touched.password &&
|
106 |
|
|
formik.errors.password
|
107 |
|
|
}
|
108 |
|
|
/>
|
109 |
|
|
<Fragment>
|
110 |
|
|
<Button
|
111 |
f2733d24
|
Vaclav Honzik
|
sx={{mb: 2}}
|
112 |
4f42fa52
|
Václav Honzík
|
type="submit"
|
113 |
|
|
variant="contained"
|
114 |
|
|
fullWidth
|
115 |
6129910f
|
Vaclav Honzik
|
disabled={isLoggingIn}
|
116 |
4f42fa52
|
Václav Honzík
|
>
|
117 |
|
|
Log in
|
118 |
|
|
</Button>
|
119 |
|
|
</Fragment>
|
120 |
6129910f
|
Vaclav Honzik
|
</form>
|
121 |
4f42fa52
|
Václav Honzík
|
|
122 |
6129910f
|
Vaclav Honzik
|
<Link component={RouterLink} to="/resetPassword">
|
123 |
|
|
Forgot password?
|
124 |
|
|
</Link>
|
125 |
f2733d24
|
Vaclav Honzik
|
<Box sx={{mb: 1}} />
|
126 |
4f42fa52
|
Václav Honzík
|
</DialogContent>
|
127 |
|
|
</Dialog>
|
128 |
|
|
</Fragment>
|
129 |
|
|
)
|
130 |
|
|
}
|
131 |
|
|
|
132 |
8dd364ad
|
Vaclav Honzik
|
export default LoginDialog
|