1
1
import React , { Component } from 'react' ;
2
2
import { Link as RouterLink } from 'react-router-dom' ;
3
- import { Formik , Form , withFormik } from 'formik' ;
3
+ import { Formik , Form } from 'formik' ;
4
4
import * as Yup from 'yup' ;
5
5
6
6
import {
@@ -26,6 +26,7 @@ class PasswordReset extends Component {
26
26
state = {
27
27
loading : false ,
28
28
message : { } ,
29
+ email : '' ,
29
30
showPassword : false ,
30
31
showPasswordConfirmation : false ,
31
32
} ;
@@ -52,21 +53,59 @@ class PasswordReset extends Component {
52
53
*
53
54
* @return {undefined }
54
55
*/
55
- handleSubmit = async ( values , { setSubmitting } ) => {
56
+ handleSubmit = async ( values , { setSubmitting, setErrors } ) => {
56
57
setSubmitting ( false ) ;
58
+
59
+ this . setState ( { loading : true } ) ;
60
+
61
+ try {
62
+ const { match, pageProps } = this . props ;
63
+ const { token } = match . params ;
64
+
65
+ const response = await axios . patch (
66
+ `api/v1/auth/password/reset/${ token } ` ,
67
+ values ,
68
+ ) ;
69
+
70
+ await pageProps . authenticate ( JSON . stringify ( response . data ) ) ;
71
+
72
+ this . setState ( { loading : false } ) ;
73
+ } catch ( error ) {
74
+ if ( ! error . response ) {
75
+ throw new Error ( 'Unknown error' ) ;
76
+ }
77
+
78
+ const { errors } = error . response . data ;
79
+
80
+ if ( errors ) {
81
+ setErrors ( errors ) ;
82
+ }
83
+
84
+ this . setState ( { loading : false } ) ;
85
+ }
57
86
} ;
58
87
88
+ componentDidMount ( ) {
89
+ const { location } = this . props ;
90
+
91
+ const queryParams = UrlUtils . _queryParams ( location . search ) ;
92
+
93
+ if ( ! queryParams . hasOwnProperty ( 'email' ) ) {
94
+ return ;
95
+ }
96
+
97
+ this . setState ( {
98
+ email : queryParams . email ,
99
+ } ) ;
100
+ }
101
+
59
102
render ( ) {
60
- const { classes, location } = this . props ;
61
- const email = UrlUtils . _queryParams ( location . search ) . hasOwnProperty (
62
- 'email' ,
63
- )
64
- ? UrlUtils . _queryParams ( location . search ) . email
65
- : '' ;
103
+ const { classes } = this . props ;
66
104
67
105
const {
68
106
loading,
69
107
message,
108
+ email,
70
109
showPassword,
71
110
showPasswordConfirmation,
72
111
} = this . state ;
@@ -265,4 +304,4 @@ const styles = theme => ({
265
304
} ,
266
305
} ) ;
267
306
268
- export default withStyles ( styles ) ( withFormik ( { } ) ( PasswordReset ) ) ;
307
+ export default withStyles ( styles ) ( PasswordReset ) ;
0 commit comments