|  | 
| 1 |  | -import React, { Component,Fragment } from 'react'; | 
|  | 1 | +import React, { Fragment } from 'react'; | 
| 2 | 2 | import { withRouter } from 'react-router-dom'; | 
| 3 | 3 | import PropTypes from 'prop-types'; | 
| 4 | 4 | import { Formik } from 'formik'; | 
| 5 | 5 | import { | 
| 6 | 6 |  Button, Form, Container, Header, Segment, | 
| 7 | 7 | } from 'semantic-ui-react'; | 
| 8 | 8 | import FormMessages from '../components/FormMessages'; | 
| 9 |  | -import { resetPasswordValidationSchema, profileValidationSchema } from '../validation/user-schema'; | 
|  | 9 | +import { resetPasswordValidationSchema } from '../validation/user-schema'; | 
|  | 10 | +import UserProfileForm from '../components/Forms/UserProfileForm'; | 
| 10 | 11 | 
 | 
| 11 |  | -class MyAccountPage extends Component { | 
| 12 |  | - state = { | 
| 13 |  | - username: '', | 
| 14 |  | - email: '', | 
| 15 |  | - }; | 
| 16 |  | - | 
| 17 |  | - componentDidMount() { | 
| 18 |  | - Meteor.call('getUserData', (err, res) => { | 
| 19 |  | - if (err) { | 
| 20 |  | - Bert.alert('There was an error trying to get your user data', 'danger'); | 
| 21 |  | - return false; | 
| 22 |  | - } | 
| 23 |  | - | 
| 24 |  | - this.setState({ username: res.username, email: res.emails[0].address }); | 
| 25 |  | - }); | 
| 26 |  | - } | 
| 27 |  | - | 
| 28 |  | - render() { | 
| 29 |  | - const { username, email } = this.state; | 
| 30 |  | - return ( | 
| 31 |  | - <Container> | 
| 32 |  | - <Header as="h1">My Account</Header> | 
| 33 |  | - <Segment padded> | 
| 34 |  | - <Formik | 
| 35 |  | - validationSchema={profileValidationSchema} | 
| 36 |  | - initialValues={{ email, username }} | 
| 37 |  | - enableReinitialize | 
| 38 |  | - onSubmit={(values, { setSubmitting }) => { | 
| 39 |  | - const options = { username: values.username }; | 
| 40 |  | - | 
| 41 |  | - Meteor.call('updateUserData', Meteor.userId(), options, (err) => { | 
| 42 |  | - if (err) { | 
| 43 |  | - Bert.alert( | 
| 44 |  | - `There was an error trying to update your user data ${err.message}`, | 
| 45 |  | - 'danger', | 
| 46 |  | - ); | 
| 47 |  | - return false; | 
| 48 |  | - } | 
| 49 |  | - Bert.alert('Your profile was updated successfully', 'success'); | 
| 50 |  | - setSubmitting(false); | 
| 51 |  | - }); | 
| 52 |  | - }} | 
| 53 |  | - render={({ | 
| 54 |  | - values, | 
| 55 |  | - touched, | 
| 56 |  | - errors, | 
| 57 |  | - handleSubmit, | 
| 58 |  | - handleChange, | 
| 59 |  | - handleBlur, | 
| 60 |  | - isSubmitting, | 
| 61 |  | - }) => ( | 
| 62 |  | - <Fragment> | 
| 63 |  | - <FormMessages errors={errors} touched={touched} /> | 
| 64 |  | - <Form> | 
| 65 |  | - <Form.Input value={values.email} fluid label="Email" type="email" readOnly /> | 
| 66 |  | - <Form.Input | 
| 67 |  | - onChange={handleChange} | 
| 68 |  | - onBlur={handleBlur} | 
| 69 |  | - value={values.username} | 
| 70 |  | - name="username" | 
| 71 |  | - fluid | 
| 72 |  | - required | 
| 73 |  | - label="Username" | 
| 74 |  | - type="text" | 
| 75 |  | - placeholder="Username" | 
| 76 |  | - /> | 
| 77 |  | - <Button color="blue" onClick={handleSubmit} disabled={isSubmitting} type="submit"> | 
| 78 |  | - Save | 
| 79 |  | - </Button> | 
| 80 |  | - </Form> | 
| 81 |  | - </Fragment> | 
| 82 |  | - )} | 
| 83 |  | - /> | 
| 84 |  | - <br /> | 
| 85 |  | - <Formik | 
| 86 |  | - validationSchema={resetPasswordValidationSchema} | 
| 87 |  | - initialValues={{ password: '', passwordConfirm: '' }} | 
| 88 |  | - onSubmit={(values, { setSubmitting }) => { | 
| 89 |  | - Meteor.call('changeUserPassword', Meteor.userId(), values.passwordConfirm, (err) => { | 
| 90 |  | - if (err) { | 
| 91 |  | - Bert.alert( | 
| 92 |  | - `There was an error trying to update your password ${err.message}`, | 
| 93 |  | - 'danger', | 
| 94 |  | - ); | 
| 95 |  | - return false; | 
| 96 |  | - } | 
| 97 |  | - Bert.alert('Your password was updated successfully', 'success'); | 
| 98 |  | - setSubmitting(false); | 
| 99 |  | - // We need to wait 1 second before redirecting user to login | 
| 100 |  | - // That's because meteor userId() does not update instantly | 
| 101 |  | - setTimeout(() => { | 
| 102 |  | - const { history } = this.props; | 
| 103 |  | - history.push('/login'); | 
| 104 |  | - }, 1000); | 
| 105 |  | - }); | 
| 106 |  | - }} | 
| 107 |  | - render={({ | 
| 108 |  | - values, | 
| 109 |  | - touched, | 
| 110 |  | - errors, | 
| 111 |  | - handleSubmit, | 
| 112 |  | - handleChange, | 
| 113 |  | - handleBlur, | 
| 114 |  | - isSubmitting, | 
| 115 |  | - }) => ( | 
| 116 |  | - <Fragment> | 
| 117 |  | - <FormMessages errors={errors} touched={touched} /> | 
| 118 |  | - <Form> | 
| 119 |  | - <Form.Input | 
| 120 |  | - onChange={handleChange} | 
| 121 |  | - onBlur={handleBlur} | 
| 122 |  | - value={values.password} | 
| 123 |  | - name="password" | 
| 124 |  | - fluid | 
| 125 |  | - label="New password" | 
| 126 |  | - type="password" | 
| 127 |  | - placeholder="New password" | 
| 128 |  | - /> | 
| 129 |  | - <Form.Input | 
| 130 |  | - onChange={handleChange} | 
| 131 |  | - onBlur={handleBlur} | 
| 132 |  | - value={values.passwordConfirm} | 
| 133 |  | - name="passwordConfirm" | 
| 134 |  | - fluid | 
| 135 |  | - label="Confirm Password" | 
| 136 |  | - type="Password" | 
| 137 |  | - placeholder="Confirm Password" | 
| 138 |  | - /> | 
| 139 |  | - <Button color="blue" onClick={handleSubmit} disabled={isSubmitting} type="submit"> | 
| 140 |  | - Change password | 
| 141 |  | - </Button> | 
| 142 |  | - </Form> | 
| 143 |  | - </Fragment> | 
| 144 |  | - )} | 
| 145 |  | - /> | 
| 146 |  | - </Segment> | 
| 147 |  | - </Container> | 
| 148 |  | - ); | 
| 149 |  | - } | 
| 150 |  | -} | 
|  | 12 | +const MyAccountPage = props => ( | 
|  | 13 | + <Container> | 
|  | 14 | + <Header as="h1">My Account</Header> | 
|  | 15 | + <Segment padded> | 
|  | 16 | + <UserProfileForm /> | 
|  | 17 | + <br /> | 
|  | 18 | + <Formik | 
|  | 19 | + validationSchema={resetPasswordValidationSchema} | 
|  | 20 | + initialValues={{ password: '', passwordConfirm: '' }} | 
|  | 21 | + onSubmit={(values, { setSubmitting }) => { | 
|  | 22 | + Meteor.call('changeUserPassword', Meteor.userId(), values.passwordConfirm, (err) => { | 
|  | 23 | + if (err) { | 
|  | 24 | + Bert.alert( | 
|  | 25 | + `There was an error trying to update your password ${err.message}`, | 
|  | 26 | + 'danger', | 
|  | 27 | + ); | 
|  | 28 | + return false; | 
|  | 29 | + } | 
|  | 30 | + Bert.alert('Your password was updated successfully', 'success'); | 
|  | 31 | + setSubmitting(false); | 
|  | 32 | + // We need to wait 1 second before redirecting user to login | 
|  | 33 | + // That's because meteor userId() does not update instantly | 
|  | 34 | + setTimeout(() => { | 
|  | 35 | + const { history } = props; | 
|  | 36 | + history.push('/login'); | 
|  | 37 | + }, 1000); | 
|  | 38 | + }); | 
|  | 39 | + }} | 
|  | 40 | + render={({ | 
|  | 41 | + values, | 
|  | 42 | + touched, | 
|  | 43 | + errors, | 
|  | 44 | + handleSubmit, | 
|  | 45 | + handleChange, | 
|  | 46 | + handleBlur, | 
|  | 47 | + isSubmitting, | 
|  | 48 | + }) => ( | 
|  | 49 | + <Fragment> | 
|  | 50 | + <FormMessages errors={errors} touched={touched} /> | 
|  | 51 | + <Form> | 
|  | 52 | + <Form.Input | 
|  | 53 | + onChange={handleChange} | 
|  | 54 | + onBlur={handleBlur} | 
|  | 55 | + value={values.password} | 
|  | 56 | + name="password" | 
|  | 57 | + fluid | 
|  | 58 | + label="New password" | 
|  | 59 | + type="password" | 
|  | 60 | + placeholder="New password" | 
|  | 61 | + /> | 
|  | 62 | + <Form.Input | 
|  | 63 | + onChange={handleChange} | 
|  | 64 | + onBlur={handleBlur} | 
|  | 65 | + value={values.passwordConfirm} | 
|  | 66 | + name="passwordConfirm" | 
|  | 67 | + fluid | 
|  | 68 | + label="Confirm Password" | 
|  | 69 | + type="Password" | 
|  | 70 | + placeholder="Confirm Password" | 
|  | 71 | + /> | 
|  | 72 | + <Button color="blue" onClick={handleSubmit} disabled={isSubmitting} type="submit"> | 
|  | 73 | + Change password | 
|  | 74 | + </Button> | 
|  | 75 | + </Form> | 
|  | 76 | + </Fragment> | 
|  | 77 | + )} | 
|  | 78 | + /> | 
|  | 79 | + </Segment> | 
|  | 80 | + </Container> | 
|  | 81 | +); | 
| 151 | 82 | 
 | 
| 152 | 83 | MyAccountPage.propTypes = { | 
| 153 | 84 |  history: PropTypes.object.isRequired, | 
|  | 
0 commit comments