|
1 | 1 | import React, { useState } from 'react'; |
2 | 2 | import { State, useStoreState } from 'easy-peasy'; |
3 | 3 | import { ApplicationState } from '@/state/types'; |
| 4 | +import { Form, Formik, FormikActions } from 'formik'; |
| 5 | +import Field from '@/components/elements/Field'; |
| 6 | +import * as Yup from 'yup'; |
| 7 | +import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; |
| 8 | + |
| 9 | +interface Values { |
| 10 | + current: string; |
| 11 | + password: string; |
| 12 | + confirmPassword: string; |
| 13 | +} |
| 14 | + |
| 15 | +const schema = Yup.object().shape({ |
| 16 | + current: Yup.string().min(1).required('You must provide your current password.'), |
| 17 | + password: Yup.string().min(8).required(), |
| 18 | + confirmPassword: Yup.string().test('password', 'Password confirmation does not match the password you entered.', function (value) { |
| 19 | + return value === this.parent.password; |
| 20 | + }), |
| 21 | +}); |
4 | 22 |
|
5 | 23 | export default () => { |
6 | | - const [ isLoading, setIsLoading ] = useState(false); |
7 | 24 | const user = useStoreState((state: State<ApplicationState>) => state.user.data); |
8 | 25 |
|
9 | 26 | if (!user) { |
10 | 27 | return null; |
11 | 28 | } |
12 | 29 |
|
| 30 | + const submit = (values: Values, { setSubmitting }: FormikActions<Values>) => { |
| 31 | + setTimeout(() => setSubmitting(false), 1500); |
| 32 | + }; |
| 33 | + |
13 | 34 | return ( |
14 | | - <form className={'m-0'}> |
15 | | - <label htmlFor={'current_password'} className={'input-dark-label'}>Current Password</label> |
16 | | - <input |
17 | | - id={'current_password'} |
18 | | - type={'password'} |
19 | | - className={'input-dark'} |
20 | | - /> |
21 | | - <div className={'mt-6'}> |
22 | | - <label htmlFor={'new_password'} className={'input-dark-label'}>New Password</label> |
23 | | - <input |
24 | | - id={'new_password'} |
25 | | - type={'password'} |
26 | | - className={'input-dark'} |
27 | | - /> |
28 | | - <p className={'input-help'}> |
29 | | - Your new password must be at least 8 characters in length. |
30 | | - </p> |
31 | | - </div> |
32 | | - <div className={'mt-6'}> |
33 | | - <label htmlFor={'new_password_confirm'} className={'input-dark-label'}>Confirm New Password</label> |
34 | | - <input |
35 | | - id={'new_password_confirm'} |
36 | | - type={'password'} |
37 | | - className={'input-dark'} |
38 | | - /> |
39 | | - </div> |
40 | | - <div className={'mt-6'}> |
41 | | - <button className={'btn btn-primary btn-sm'} disabled={true}> |
42 | | - Update Password |
43 | | - </button> |
44 | | - </div> |
45 | | - </form> |
| 35 | + <React.Fragment> |
| 36 | + <Formik |
| 37 | + onSubmit={submit} |
| 38 | + validationSchema={schema} |
| 39 | + initialValues={{ current: '', password: '', confirmPassword: '' }} |
| 40 | + > |
| 41 | + { |
| 42 | + ({ isSubmitting, isValid }) => ( |
| 43 | + <React.Fragment> |
| 44 | + <SpinnerOverlay large={true} visible={isSubmitting}/> |
| 45 | + <Form className={'m-0'}> |
| 46 | + <Field |
| 47 | + id={'current_password'} |
| 48 | + type={'password'} |
| 49 | + name={'current'} |
| 50 | + label={'Current Password'} |
| 51 | + /> |
| 52 | + <div className={'mt-6'}> |
| 53 | + <Field |
| 54 | + id={'new_password'} |
| 55 | + type={'password'} |
| 56 | + name={'password'} |
| 57 | + label={'New Password'} |
| 58 | + description={'Your new password should be at least 8 characters in length and unique to this website.'} |
| 59 | + /> |
| 60 | + </div> |
| 61 | + <div className={'mt-6'}> |
| 62 | + <Field |
| 63 | + id={'confirm_password'} |
| 64 | + type={'password'} |
| 65 | + name={'confirmPassword'} |
| 66 | + label={'Confirm New Password'} |
| 67 | + /> |
| 68 | + </div> |
| 69 | + <div className={'mt-6'}> |
| 70 | + <button className={'btn btn-primary btn-sm'} disabled={isSubmitting || !isValid}> |
| 71 | + Update Password |
| 72 | + </button> |
| 73 | + </div> |
| 74 | + </Form> |
| 75 | + </React.Fragment> |
| 76 | + ) |
| 77 | + } |
| 78 | + </Formik> |
| 79 | + </React.Fragment> |
46 | 80 | ); |
47 | 81 | }; |
0 commit comments