|
1 | | -import React, { useState } from 'react'; |
| 1 | +import React from 'react'; |
2 | 2 | import { Link, RouteComponentProps } from 'react-router-dom'; |
3 | 3 | import login from '@/api/auth/login'; |
4 | | -import { httpErrorToHuman } from '@/api/http'; |
5 | 4 | import LoginFormContainer from '@/components/auth/LoginFormContainer'; |
6 | 5 | import FlashMessageRender from '@/components/FlashMessageRender'; |
7 | 6 | import { Actions, useStoreActions } from 'easy-peasy'; |
8 | 7 | import { ApplicationStore } from '@/state'; |
| 8 | +import { FormikProps, withFormik } from 'formik'; |
| 9 | +import { object, string } from 'yup'; |
| 10 | +import Field from '@/components/elements/Field'; |
| 11 | +import { httpErrorToHuman } from '@/api/http'; |
9 | 12 |
|
10 | | -export default ({ history }: RouteComponentProps) => { |
11 | | - const [ username, setUsername ] = useState(''); |
12 | | - const [ password, setPassword ] = useState(''); |
13 | | - const [ isLoading, setLoading ] = useState(false); |
| 13 | +interface Values { |
| 14 | + username: string; |
| 15 | + password: string; |
| 16 | +} |
14 | 17 |
|
15 | | - const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes); |
| 18 | +type OwnProps = RouteComponentProps & { |
| 19 | + clearFlashes: any; |
| 20 | + addFlash: any; |
| 21 | +} |
| 22 | + |
| 23 | +const LoginContainer = ({ isSubmitting }: OwnProps & FormikProps<Values>) => ( |
| 24 | + <React.Fragment> |
| 25 | + <h2 className={'text-center text-neutral-100 font-medium py-4'}> |
| 26 | + Login to Continue |
| 27 | + </h2> |
| 28 | + <FlashMessageRender className={'mb-2'}/> |
| 29 | + <LoginFormContainer> |
| 30 | + <label htmlFor={'username'}>Username or Email</label> |
| 31 | + <Field |
| 32 | + type={'text'} |
| 33 | + id={'username'} |
| 34 | + name={'username'} |
| 35 | + className={'input'} |
| 36 | + /> |
| 37 | + <div className={'mt-6'}> |
| 38 | + <label htmlFor={'password'}>Password</label> |
| 39 | + <Field |
| 40 | + type={'password'} |
| 41 | + id={'password'} |
| 42 | + name={'password'} |
| 43 | + className={'input'} |
| 44 | + /> |
| 45 | + </div> |
| 46 | + <div className={'mt-6'}> |
| 47 | + <button |
| 48 | + type={'submit'} |
| 49 | + className={'btn btn-primary btn-jumbo'} |
| 50 | + > |
| 51 | + {isSubmitting ? |
| 52 | + <span className={'spinner white'}> </span> |
| 53 | + : |
| 54 | + 'Login' |
| 55 | + } |
| 56 | + </button> |
| 57 | + </div> |
| 58 | + <div className={'mt-6 text-center'}> |
| 59 | + <Link |
| 60 | + to={'/auth/password'} |
| 61 | + className={'text-xs text-neutral-500 tracking-wide no-underline uppercase hover:text-neutral-600'} |
| 62 | + > |
| 63 | + Forgot password? |
| 64 | + </Link> |
| 65 | + </div> |
| 66 | + </LoginFormContainer> |
| 67 | + </React.Fragment> |
| 68 | +); |
| 69 | + |
| 70 | +const EnhancedForm = withFormik<OwnProps, Values>({ |
| 71 | + displayName: 'LoginContainerForm', |
16 | 72 |
|
17 | | - const submit = (e: React.FormEvent<HTMLFormElement>) => { |
18 | | - e.preventDefault(); |
| 73 | + mapPropsToValues: (props) => ({ |
| 74 | + username: '', |
| 75 | + password: '', |
| 76 | + }), |
19 | 77 |
|
20 | | - setLoading(true); |
21 | | - clearFlashes(); |
| 78 | + validationSchema: () => object().shape({ |
| 79 | + username: string().required('A username or email must be provided.'), |
| 80 | + password: string().required('Please enter your account password.'), |
| 81 | + }), |
22 | 82 |
|
23 | | - login(username!, password!) |
| 83 | + handleSubmit: ({ username, password }, { props, setSubmitting }) => { |
| 84 | + props.clearFlashes(); |
| 85 | + login(username, password) |
24 | 86 | .then(response => { |
25 | 87 | if (response.complete) { |
26 | 88 | // @ts-ignore |
27 | 89 | window.location = response.intended || '/'; |
28 | 90 | return; |
29 | 91 | } |
30 | 92 |
|
31 | | - history.replace('/auth/login/checkpoint', { token: response.confirmationToken }); |
| 93 | + props.history.replace('/auth/login/checkpoint', { token: response.confirmationToken }); |
32 | 94 | }) |
33 | 95 | .catch(error => { |
34 | 96 | console.error(error); |
35 | 97 |
|
36 | | - setLoading(false); |
37 | | - addFlash({ type: 'error', title: 'Error', message: httpErrorToHuman(error) }); |
| 98 | + setSubmitting(false); |
| 99 | + props.addFlash({ type: 'error', title: 'Error', message: httpErrorToHuman(error) }); |
38 | 100 | }); |
39 | | - }; |
| 101 | + }, |
| 102 | +})(LoginContainer); |
40 | 103 |
|
41 | | - const canSubmit = () => username && password && username.length > 0 && password.length > 0; |
| 104 | +export default (props: RouteComponentProps) => { |
| 105 | + const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes); |
42 | 106 |
|
43 | 107 | return ( |
44 | | - <React.Fragment> |
45 | | - <h2 className={'text-center text-neutral-100 font-medium py-4'}> |
46 | | - Login to Continue |
47 | | - </h2> |
48 | | - <FlashMessageRender/> |
49 | | - <LoginFormContainer onSubmit={submit}> |
50 | | - <label htmlFor={'username'}>Username or Email</label> |
51 | | - <input |
52 | | - id={'username'} |
53 | | - autoFocus={true} |
54 | | - required={true} |
55 | | - className={'input'} |
56 | | - onChange={e => setUsername(e.target.value)} |
57 | | - disabled={isLoading} |
58 | | - /> |
59 | | - <div className={'mt-6'}> |
60 | | - <label htmlFor={'password'}>Password</label> |
61 | | - <input |
62 | | - id={'password'} |
63 | | - required={true} |
64 | | - type={'password'} |
65 | | - className={'input'} |
66 | | - onChange={e => setPassword(e.target.value)} |
67 | | - disabled={isLoading} |
68 | | - /> |
69 | | - </div> |
70 | | - <div className={'mt-6'}> |
71 | | - <button |
72 | | - type={'submit'} |
73 | | - className={'btn btn-primary btn-jumbo'} |
74 | | - disabled={isLoading || !canSubmit()} |
75 | | - > |
76 | | - {isLoading ? |
77 | | - <span className={'spinner white'}> </span> |
78 | | - : |
79 | | - 'Login' |
80 | | - } |
81 | | - </button> |
82 | | - </div> |
83 | | - <div className={'mt-6 text-center'}> |
84 | | - <Link |
85 | | - to={'/auth/password'} |
86 | | - className={'text-xs text-neutral-500 tracking-wide no-underline uppercase hover:text-neutral-600'} |
87 | | - > |
88 | | - Forgot password? |
89 | | - </Link> |
90 | | - </div> |
91 | | - </LoginFormContainer> |
92 | | - </React.Fragment> |
| 108 | + <EnhancedForm |
| 109 | + {...props} |
| 110 | + addFlash={addFlash} |
| 111 | + clearFlashes={clearFlashes} |
| 112 | + /> |
93 | 113 | ); |
94 | 114 | }; |
0 commit comments