1
|
import { Fragment, FunctionComponent, useEffect, useState } from 'react'
|
2
|
import Dialog, { DialogProps } from '@mui/material/Dialog'
|
3
|
import {
|
4
|
Button,
|
5
|
DialogContent,
|
6
|
DialogTitle,
|
7
|
IconButton,
|
8
|
Link,
|
9
|
Stack,
|
10
|
TextField,
|
11
|
Typography,
|
12
|
} from '@mui/material'
|
13
|
import { useFormik } from 'formik'
|
14
|
import * as yup from 'yup'
|
15
|
import { useDispatch, useSelector } from 'react-redux'
|
16
|
import { Link as RouterLink, useNavigate } from 'react-router-dom'
|
17
|
import { RootState } from '../redux/store'
|
18
|
import { Box } from '@mui/system'
|
19
|
import ShowErrorIfPresent from '../Reusables/ShowErrorIfPresent'
|
20
|
import { changePassword } from '../Auth/userThunks'
|
21
|
import { consumeError } from '../Auth/userSlice'
|
22
|
import CloseIcon from '@mui/icons-material/Close'
|
23
|
|
24
|
export interface ChangePasswordDialogProps {
|
25
|
username?: string
|
26
|
isAdmin: boolean
|
27
|
}
|
28
|
|
29
|
const ChangePasswordDialog: FunctionComponent<ChangePasswordDialogProps> = ({
|
30
|
username,
|
31
|
isAdmin,
|
32
|
}) => {
|
33
|
const [open, setOpen] = useState(true)
|
34
|
const lastErr = useSelector((state: RootState) => state.user.lastErr)
|
35
|
|
36
|
const dispatch = useDispatch()
|
37
|
const navigate = useNavigate()
|
38
|
|
39
|
const validationSchema = yup.object().shape({
|
40
|
isAdmin: yup.boolean(),
|
41
|
oldPassword: yup.string().
|
42
|
when('isAdmin', {
|
43
|
is: false,
|
44
|
then: yup.string().required('Old password is required')
|
45
|
}),
|
46
|
password: yup.string()
|
47
|
.required('Password is required')
|
48
|
.min(8, 'Password too short, minimal length is 8 characters'),
|
49
|
confirmationPassword: yup.string()
|
50
|
.oneOf([yup.ref('password'), null], 'Passwords must match'),
|
51
|
})
|
52
|
|
53
|
const formik = useFormik({
|
54
|
initialValues: {
|
55
|
isAdmin: isAdmin,
|
56
|
oldPassword: '',
|
57
|
password: '',
|
58
|
confirmationPassword: '',
|
59
|
},
|
60
|
validationSchema,
|
61
|
onSubmit: () => {
|
62
|
dispatch(
|
63
|
changePassword({
|
64
|
username: username,
|
65
|
isAdmin: isAdmin,
|
66
|
oldPassword: formik.values.oldPassword,
|
67
|
password: formik.values.password,
|
68
|
confirmationPassword: formik.values.confirmationPassword,
|
69
|
})
|
70
|
)
|
71
|
},
|
72
|
})
|
73
|
|
74
|
const onCancel = () => {
|
75
|
formik.resetForm()
|
76
|
dispatch(consumeError())
|
77
|
navigate('/Administration')
|
78
|
}
|
79
|
|
80
|
return (
|
81
|
<Fragment>
|
82
|
<Dialog
|
83
|
open={open}
|
84
|
fullWidth={true}
|
85
|
onClose={onCancel}
|
86
|
maxWidth="md"
|
87
|
>
|
88
|
<DialogTitle>
|
89
|
<Stack
|
90
|
direction="row"
|
91
|
spacing={1}
|
92
|
justifyContent="space-between"
|
93
|
alignItems="center"
|
94
|
>
|
95
|
<Typography
|
96
|
variant="h5"
|
97
|
fontWeight="600"
|
98
|
>
|
99
|
Change Password{isAdmin && username ? ": " + username : null}
|
100
|
</Typography>
|
101
|
<IconButton
|
102
|
onClick={() => {
|
103
|
formik.resetForm()
|
104
|
setOpen(false)
|
105
|
onCancel()
|
106
|
}}
|
107
|
>
|
108
|
<CloseIcon />
|
109
|
</IconButton>
|
110
|
</Stack>
|
111
|
</DialogTitle>
|
112
|
|
113
|
<DialogContent>
|
114
|
{lastErr ? (
|
115
|
<Typography
|
116
|
sx={{ mb: 1 }}
|
117
|
align="left"
|
118
|
color="error"
|
119
|
fontWeight="bold"
|
120
|
>
|
121
|
{lastErr}
|
122
|
</Typography>
|
123
|
) : null}
|
124
|
<form onSubmit={formik.handleSubmit}>
|
125
|
{!isAdmin ? (
|
126
|
<TextField
|
127
|
fullWidth
|
128
|
label="Old Password"
|
129
|
name="oldPassword"
|
130
|
type="password"
|
131
|
sx={{ mb: 2 }}
|
132
|
value={formik.values.oldPassword}
|
133
|
onChange={formik.handleChange}
|
134
|
error={
|
135
|
Boolean(formik.errors.oldPassword) &&
|
136
|
formik.touched.oldPassword
|
137
|
}
|
138
|
helperText={
|
139
|
formik.errors.oldPassword &&
|
140
|
formik.touched.oldPassword
|
141
|
}
|
142
|
/>
|
143
|
) : null}
|
144
|
<TextField
|
145
|
fullWidth
|
146
|
label="New Password"
|
147
|
name="password"
|
148
|
type="password"
|
149
|
sx={{ mb: 2, mt: 1 }}
|
150
|
value={formik.values.password}
|
151
|
onChange={formik.handleChange}
|
152
|
error={
|
153
|
Boolean(formik.errors.password) &&
|
154
|
formik.touched.password
|
155
|
}
|
156
|
helperText={
|
157
|
formik.errors.password &&
|
158
|
formik.touched.password &&
|
159
|
formik.errors.password
|
160
|
}
|
161
|
/>
|
162
|
<TextField
|
163
|
fullWidth
|
164
|
label="Confirmation Password"
|
165
|
name="confirmationPassword"
|
166
|
type="password"
|
167
|
sx={{ mb: 2 }}
|
168
|
value={formik.values.confirmationPassword}
|
169
|
onChange={formik.handleChange}
|
170
|
error={
|
171
|
Boolean(formik.errors.confirmationPassword) &&
|
172
|
formik.touched.confirmationPassword
|
173
|
}
|
174
|
helperText={
|
175
|
formik.errors.confirmationPassword &&
|
176
|
formik.touched.confirmationPassword &&
|
177
|
formik.errors.confirmationPassword
|
178
|
}
|
179
|
/>
|
180
|
<Fragment>
|
181
|
<Button
|
182
|
sx={{ mb: 2 }}
|
183
|
type="submit"
|
184
|
variant="contained"
|
185
|
fullWidth
|
186
|
>
|
187
|
Change Password
|
188
|
</Button>
|
189
|
</Fragment>
|
190
|
</form>
|
191
|
<Box sx={{ mb: 1 }} />
|
192
|
</DialogContent>
|
193
|
</Dialog>
|
194
|
</Fragment>
|
195
|
)
|
196
|
}
|
197
|
|
198
|
export default ChangePasswordDialog
|