From f864b72e0aceae28fbf7cf6c192f8f9169020557 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 15 Dec 2019 16:41:20 -0800 Subject: [PATCH] Get formik used on login form --- .../components/auth/LoginContainer.tsx | 152 ++++++++++-------- .../components/auth/LoginFormContainer.tsx | 5 +- .../scripts/components/elements/Field.tsx | 16 +- 3 files changed, 96 insertions(+), 77 deletions(-) diff --git a/resources/scripts/components/auth/LoginContainer.tsx b/resources/scripts/components/auth/LoginContainer.tsx index 95538d78c..ca988484b 100644 --- a/resources/scripts/components/auth/LoginContainer.tsx +++ b/resources/scripts/components/auth/LoginContainer.tsx @@ -1,26 +1,88 @@ -import React, { useState } from 'react'; +import React from 'react'; import { Link, RouteComponentProps } from 'react-router-dom'; import login from '@/api/auth/login'; -import { httpErrorToHuman } from '@/api/http'; import LoginFormContainer from '@/components/auth/LoginFormContainer'; import FlashMessageRender from '@/components/FlashMessageRender'; import { Actions, useStoreActions } from 'easy-peasy'; import { ApplicationStore } from '@/state'; +import { FormikProps, withFormik } from 'formik'; +import { object, string } from 'yup'; +import Field from '@/components/elements/Field'; +import { httpErrorToHuman } from '@/api/http'; -export default ({ history }: RouteComponentProps) => { - const [ username, setUsername ] = useState(''); - const [ password, setPassword ] = useState(''); - const [ isLoading, setLoading ] = useState(false); +interface Values { + username: string; + password: string; +} - const { clearFlashes, addFlash } = useStoreActions((actions: Actions) => actions.flashes); +type OwnProps = RouteComponentProps & { + clearFlashes: any; + addFlash: any; +} - const submit = (e: React.FormEvent) => { - e.preventDefault(); +const LoginContainer = ({ isSubmitting }: OwnProps & FormikProps) => ( + +

+ Login to Continue +

+ + + + +
+ + +
+
+ +
+
+ + Forgot password? + +
+
+
+); - setLoading(true); - clearFlashes(); +const EnhancedForm = withFormik({ + displayName: 'LoginContainerForm', - login(username!, password!) + mapPropsToValues: (props) => ({ + username: '', + password: '', + }), + + validationSchema: () => object().shape({ + username: string().required('A username or email must be provided.'), + password: string().required('Please enter your account password.'), + }), + + handleSubmit: ({ username, password }, { props, setSubmitting }) => { + props.clearFlashes(); + login(username, password) .then(response => { if (response.complete) { // @ts-ignore @@ -28,67 +90,25 @@ export default ({ history }: RouteComponentProps) => { return; } - history.replace('/auth/login/checkpoint', { token: response.confirmationToken }); + props.history.replace('/auth/login/checkpoint', { token: response.confirmationToken }); }) .catch(error => { console.error(error); - setLoading(false); - addFlash({ type: 'error', title: 'Error', message: httpErrorToHuman(error) }); + setSubmitting(false); + props.addFlash({ type: 'error', title: 'Error', message: httpErrorToHuman(error) }); }); - }; + }, +})(LoginContainer); - const canSubmit = () => username && password && username.length > 0 && password.length > 0; +export default (props: RouteComponentProps) => { + const { clearFlashes, addFlash } = useStoreActions((actions: Actions) => actions.flashes); return ( - -

- Login to Continue -

- - - - setUsername(e.target.value)} - disabled={isLoading} - /> -
- - setPassword(e.target.value)} - disabled={isLoading} - /> -
-
- -
-
- - Forgot password? - -
-
-
+ ); }; diff --git a/resources/scripts/components/auth/LoginFormContainer.tsx b/resources/scripts/components/auth/LoginFormContainer.tsx index 8f364e335..2caa15784 100644 --- a/resources/scripts/components/auth/LoginFormContainer.tsx +++ b/resources/scripts/components/auth/LoginFormContainer.tsx @@ -1,7 +1,8 @@ import * as React from 'react'; +import { Form } from 'formik'; export default ({ className, ...props }: React.DetailedHTMLProps, HTMLFormElement>) => ( -
{props.children} -
+ ); diff --git a/resources/scripts/components/elements/Field.tsx b/resources/scripts/components/elements/Field.tsx index c9557c0f0..120d5afd0 100644 --- a/resources/scripts/components/elements/Field.tsx +++ b/resources/scripts/components/elements/Field.tsx @@ -2,17 +2,16 @@ import React from 'react'; import { Field, FieldProps } from 'formik'; import classNames from 'classnames'; -interface Props { - id?: string; - type: string; +interface OwnProps { name: string; label?: string; description?: string; - autoFocus?: boolean; validate?: (value: any) => undefined | string | Promise; } -export default ({ id, type, name, label, description, autoFocus, validate }: Props) => ( +type Props = OwnProps & Omit, 'name'>; + +export default ({ id, name, label, description, validate, className, ...props }: Props) => ( { ({ field, form: { errors, touched } }: FieldProps) => ( @@ -22,15 +21,14 @@ export default ({ id, type, name, label, description, autoFocus, validate }: Pro } {touched[field.name] && errors[field.name] ? -

+

{(errors[field.name] as string).charAt(0).toUpperCase() + (errors[field.name] as string).slice(1)}

: