Projekt

Obecné

Profil

Stáhnout (7.16 KB) Statistiky
| Větev: | Tag: | Revize:
1 bcd7951e Schwobik
import { Fragment, FunctionComponent, useEffect, useState } from 'react'
2
import Dialog, { DialogProps } from '@mui/material/Dialog'
3
import {
4
    Button,
5
    DialogContent,
6 484adbb5 Vaclav Honzik
    DialogTitle,
7
    IconButton,
8 bcd7951e Schwobik
    Link,
9 484adbb5 Vaclav Honzik
    Stack,
10 bcd7951e Schwobik
    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 484adbb5 Vaclav Honzik
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 bcd7951e Schwobik
24
export interface ChangePasswordDialogProps {
25
    username?: string
26
    isAdmin: boolean
27
}
28
29
const ChangePasswordDialog: FunctionComponent<ChangePasswordDialogProps> = ({
30
    username,
31 484adbb5 Vaclav Honzik
    isAdmin,
32 bcd7951e Schwobik
}) => {
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 1f0210e0 Schwobik
        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 484adbb5 Vaclav Honzik
            .oneOf([yup.ref('password'), null], 'Passwords must match'),
51 bcd7951e Schwobik
    })
52
53
    const formik = useFormik({
54
        initialValues: {
55 1f0210e0 Schwobik
            isAdmin: isAdmin,
56 bcd7951e Schwobik
            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 484adbb5 Vaclav Honzik
                <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 1f0210e0 Schwobik
                            Change Password{isAdmin && username ? ": " + username : null}
100 484adbb5 Vaclav Honzik
                        </Typography>
101
                        <IconButton
102
                            onClick={() => {
103
                                formik.resetForm()
104
                                setOpen(false)
105 1c89ac84 Schwobik
                                onCancel()
106 484adbb5 Vaclav Honzik
                            }}
107
                        >
108
                            <CloseIcon />
109
                        </IconButton>
110
                    </Stack>
111
                </DialogTitle>
112 bcd7951e Schwobik
113
                <DialogContent>
114
                    {lastErr ? (
115 484adbb5 Vaclav Honzik
                        <Typography
116
                            sx={{ mb: 1 }}
117
                            align="left"
118
                            color="error"
119
                            fontWeight="bold"
120
                        >
121 bcd7951e Schwobik
                            {lastErr}
122
                        </Typography>
123
                    ) : null}
124
                    <form onSubmit={formik.handleSubmit}>
125 484adbb5 Vaclav Honzik
                        {!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 bcd7951e Schwobik
                        <TextField
145
                            fullWidth
146
                            label="New Password"
147
                            name="password"
148
                            type="password"
149 1f0210e0 Schwobik
                            sx={{ mb: 2, mt: 1 }}
150 bcd7951e Schwobik
                            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 1f0210e0 Schwobik
                                formik.touched.password &&
159
                                formik.errors.password
160 bcd7951e Schwobik
                            }
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 1f0210e0 Schwobik
                                formik.touched.confirmationPassword &&
177
                                formik.errors.confirmationPassword
178 bcd7951e Schwobik
                            }
179
                        />
180
                        <Fragment>
181
                            <Button
182 484adbb5 Vaclav Honzik
                                sx={{ mb: 2 }}
183 bcd7951e Schwobik
                                type="submit"
184
                                variant="contained"
185
                                fullWidth
186
                            >
187
                                Change Password
188
                            </Button>
189
                        </Fragment>
190
                    </form>
191 484adbb5 Vaclav Honzik
                    <Box sx={{ mb: 1 }} />
192 bcd7951e Schwobik
                </DialogContent>
193
            </Dialog>
194
        </Fragment>
195
    )
196
}
197
198
export default ChangePasswordDialog