|
1 | 1 | import TextField from "@mui/material/TextField"
|
2 |
| -import { FormikContextType, FormikTouched, useFormik } from "formik" |
| 2 | +import { FormikContextType, useFormik } from "formik" |
3 | 3 | import { FC } from "react"
|
4 | 4 | import * as Yup from "yup"
|
5 | 5 | import { getFormHelpers } from "../../utils/formUtils"
|
6 | 6 | import { LoadingButton } from "../LoadingButton/LoadingButton"
|
7 |
| -import { Stack } from "../Stack/Stack" |
8 | 7 | import { ErrorAlert } from "components/Alert/ErrorAlert"
|
| 8 | +import { Form, FormFields } from "components/Form/Form" |
| 9 | +import { Alert } from "components/Alert/Alert" |
9 | 10 |
|
10 | 11 | interface SecurityFormValues {
|
11 | 12 | old_password: string
|
@@ -41,40 +42,43 @@ const validationSchema = Yup.object({
|
41 | 42 | })
|
42 | 43 |
|
43 | 44 | export interface SecurityFormProps {
|
| 45 | + disabled: boolean |
44 | 46 | isLoading: boolean
|
45 |
| - initialValues: SecurityFormValues |
46 | 47 | onSubmit: (values: SecurityFormValues) => void
|
47 |
| - updateSecurityError?: Error | unknown |
48 |
| - // initialTouched is only used for testing the error state of the form. |
49 |
| - initialTouched?: FormikTouched<SecurityFormValues> |
| 48 | + error?: unknown |
50 | 49 | }
|
51 | 50 |
|
52 | 51 | export const SecurityForm: FC<SecurityFormProps> = ({
|
| 52 | + disabled, |
53 | 53 | isLoading,
|
54 | 54 | onSubmit,
|
55 |
| - initialValues, |
56 |
| - updateSecurityError, |
57 |
| - initialTouched, |
| 55 | + error, |
58 | 56 | }) => {
|
59 | 57 | const form: FormikContextType<SecurityFormValues> =
|
60 | 58 | useFormik<SecurityFormValues>({
|
61 |
| - initialValues, |
| 59 | + initialValues: { |
| 60 | + old_password: "", |
| 61 | + password: "", |
| 62 | + confirm_password: "", |
| 63 | + }, |
62 | 64 | validationSchema,
|
63 | 65 | onSubmit,
|
64 |
| - initialTouched, |
65 | 66 | })
|
66 |
| - const getFieldHelpers = getFormHelpers<SecurityFormValues>( |
67 |
| - form, |
68 |
| - updateSecurityError, |
69 |
| - ) |
| 67 | + const getFieldHelpers = getFormHelpers<SecurityFormValues>(form, error) |
| 68 | + |
| 69 | + if (disabled) { |
| 70 | + return ( |
| 71 | + <Alert severity="info"> |
| 72 | + Password changes are only allowed for password based accounts. |
| 73 | + </Alert> |
| 74 | + ) |
| 75 | + } |
70 | 76 |
|
71 | 77 | return (
|
72 | 78 | <>
|
73 |
| - <form onSubmit={form.handleSubmit}> |
74 |
| - <Stack> |
75 |
| - {Boolean(updateSecurityError) && ( |
76 |
| - <ErrorAlert error={updateSecurityError} /> |
77 |
| - )} |
| 79 | + <Form onSubmit={form.handleSubmit}> |
| 80 | + <FormFields> |
| 81 | + {Boolean(error) && <ErrorAlert error={error} />} |
78 | 82 | <TextField
|
79 | 83 | {...getFieldHelpers("old_password")}
|
80 | 84 | autoComplete="old_password"
|
@@ -106,8 +110,8 @@ export const SecurityForm: FC<SecurityFormProps> = ({
|
106 | 110 | {isLoading ? "" : Language.updatePassword}
|
107 | 111 | </LoadingButton>
|
108 | 112 | </div>
|
109 |
| - </Stack> |
110 |
| - </form> |
| 113 | + </FormFields> |
| 114 | + </Form> |
111 | 115 | </>
|
112 | 116 | )
|
113 | 117 | }
|
0 commit comments