|
| 1 | +import { Stack } from "../Stack/Stack" |
| 2 | +import { AlertBanner } from "../AlertBanner/AlertBanner" |
| 3 | +import TextField from "@material-ui/core/TextField" |
| 4 | +import { getFormHelpers, onChangeTrimmed } from "../../util/formUtils" |
| 5 | +import { LoadingButton } from "../LoadingButton/LoadingButton" |
| 6 | +import { Language, LoginErrors } from "./SignInForm" |
| 7 | +import { FormikContextType, FormikTouched, useFormik } from "formik" |
| 8 | +import * as Yup from "yup" |
| 9 | +import { FC } from "react" |
| 10 | +import { BuiltInAuthFormValues } from "./SignInForm.types" |
| 11 | + |
| 12 | +type PasswordSignInFormProps = { |
| 13 | + loginErrors: Partial<Record<LoginErrors, Error | unknown>> |
| 14 | + onSubmit: (credentials: { email: string; password: string }) => void |
| 15 | + initialTouched?: FormikTouched<BuiltInAuthFormValues> |
| 16 | + isLoading: boolean |
| 17 | +} |
| 18 | + |
| 19 | +export const PasswordSignInForm: FC<PasswordSignInFormProps> = ({ |
| 20 | + loginErrors, |
| 21 | + onSubmit, |
| 22 | + initialTouched, |
| 23 | + isLoading, |
| 24 | +}) => { |
| 25 | + const validationSchema = Yup.object({ |
| 26 | + email: Yup.string() |
| 27 | + .trim() |
| 28 | + .email(Language.emailInvalid) |
| 29 | + .required(Language.emailRequired), |
| 30 | + password: Yup.string(), |
| 31 | + }) |
| 32 | + |
| 33 | + const form: FormikContextType<BuiltInAuthFormValues> = |
| 34 | + useFormik<BuiltInAuthFormValues>({ |
| 35 | + initialValues: { |
| 36 | + email: "", |
| 37 | + password: "", |
| 38 | + }, |
| 39 | + validationSchema, |
| 40 | + // The email field has an autoFocus, but users may log in with a button click. |
| 41 | + // This is set to `false` in order to keep the autoFocus, validateOnChange |
| 42 | + // and Formik experience friendly. Validation will kick in onChange (any |
| 43 | + // field), or after a submission attempt. |
| 44 | + validateOnBlur: false, |
| 45 | + onSubmit, |
| 46 | + initialTouched, |
| 47 | + }) |
| 48 | + const getFieldHelpers = getFormHelpers<BuiltInAuthFormValues>( |
| 49 | + form, |
| 50 | + loginErrors.authError, |
| 51 | + ) |
| 52 | + |
| 53 | + return ( |
| 54 | + <form onSubmit={form.handleSubmit}> |
| 55 | + <Stack> |
| 56 | + {Object.keys(loginErrors).map( |
| 57 | + (errorKey: string) => |
| 58 | + Boolean(loginErrors[errorKey as LoginErrors]) && ( |
| 59 | + <AlertBanner |
| 60 | + key={errorKey} |
| 61 | + severity="error" |
| 62 | + error={loginErrors[errorKey as LoginErrors]} |
| 63 | + text={Language.errorMessages[errorKey as LoginErrors]} |
| 64 | + /> |
| 65 | + ), |
| 66 | + )} |
| 67 | + <TextField |
| 68 | + {...getFieldHelpers("email")} |
| 69 | + onChange={onChangeTrimmed(form)} |
| 70 | + autoFocus |
| 71 | + autoComplete="email" |
| 72 | + fullWidth |
| 73 | + label={Language.emailLabel} |
| 74 | + type="email" |
| 75 | + variant="outlined" |
| 76 | + /> |
| 77 | + <TextField |
| 78 | + {...getFieldHelpers("password")} |
| 79 | + autoComplete="current-password" |
| 80 | + fullWidth |
| 81 | + id="password" |
| 82 | + label={Language.passwordLabel} |
| 83 | + type="password" |
| 84 | + variant="outlined" |
| 85 | + /> |
| 86 | + <div> |
| 87 | + <LoadingButton |
| 88 | + loading={isLoading} |
| 89 | + fullWidth |
| 90 | + type="submit" |
| 91 | + variant="contained" |
| 92 | + > |
| 93 | + {isLoading ? "" : Language.passwordSignIn} |
| 94 | + </LoadingButton> |
| 95 | + </div> |
| 96 | + </Stack> |
| 97 | + </form> |
| 98 | + ) |
| 99 | +} |
0 commit comments