-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/edit-user-profile' into dev
- Loading branch information
Showing
13 changed files
with
384 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import React, { Component } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { Field, reduxForm, SubmissionError } from 'redux-form'; | ||
import Alert from 'react-bootstrap/lib/Alert'; | ||
import Button from 'react-bootstrap/lib/Button'; | ||
import userAPI from '../../../api/user'; | ||
import { pushErrors } from '../../../actions/errorActions'; | ||
import { Form, FormField, FormFooter } from '../../utils/BsForm'; | ||
|
||
export const validate = (values) => { | ||
const errors = {}; | ||
|
||
if ( | ||
values.newPasswordConfirm && | ||
values.newPassword !== values.newPasswordConfirm | ||
) { | ||
errors.newPassword = errors.newPasswordConfirm = 'Password Not Matched'; | ||
} | ||
|
||
if (values.oldPassword === values.newPassword) { | ||
errors.newPassword = 'Cannot be same as old password'; | ||
} | ||
|
||
if (!values.oldPassword) { | ||
errors.oldPassword = 'Required'; | ||
} | ||
|
||
if (!values.newPassword) { | ||
errors.newPassword = 'Required'; | ||
} | ||
|
||
if (!values.newPasswordConfirm) { | ||
errors.newPasswordConfirm = 'Required'; | ||
} | ||
|
||
return errors; | ||
}; | ||
|
||
class ChangePasswordForm extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.handleSubmit = this._handleSubmit.bind(this); | ||
} | ||
|
||
_handleSubmit(formData) { | ||
let { dispatch, apiEngine, initialize } = this.props; | ||
|
||
return userAPI(apiEngine) | ||
.updatePassword(formData) | ||
.catch((err) => { | ||
dispatch(pushErrors(err)); | ||
throw err; | ||
}) | ||
.then((json) => { | ||
if (json.isAuth) { | ||
initialize({ | ||
oldPassword: '', | ||
newPassword: '', | ||
newPasswordConfirm: '', | ||
}); | ||
} else { | ||
throw new SubmissionError({ | ||
oldPassword: 'Wrong old password', | ||
_error: 'Change password failed', | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
render() { | ||
const { | ||
handleSubmit, | ||
submitSucceeded, | ||
submitFailed, | ||
error, | ||
pristine, | ||
submitting, | ||
invalid, | ||
} = this.props; | ||
|
||
return ( | ||
<Form horizontal onSubmit={handleSubmit(this.handleSubmit)}> | ||
{submitSucceeded && (<Alert bsStyle="success">Password Changed</Alert>)} | ||
{submitFailed && error && (<Alert bsStyle="danger">{error}</Alert>)} | ||
<Field | ||
label="Old Password" | ||
name="oldPassword" | ||
component={FormField} | ||
type="password" | ||
placeholder="Old Password" | ||
/> | ||
<Field | ||
label="New Password" | ||
name="newPassword" | ||
component={FormField} | ||
type="password" | ||
placeholder="New Password" | ||
/> | ||
<Field | ||
label="New Password Confirm" | ||
name="newPasswordConfirm" | ||
component={FormField} | ||
type="password" | ||
placeholder="New Password Confirm" | ||
/> | ||
<FormFooter> | ||
<Button type="submit" disabled={pristine || submitting || invalid}> | ||
Change | ||
{submitting && ( | ||
<i className="fa fa-spinner fa-spin" aria-hidden="true" /> | ||
)} | ||
</Button> | ||
</FormFooter> | ||
</Form> | ||
); | ||
} | ||
}; | ||
|
||
export default reduxForm({ | ||
form: 'userChangePassword', | ||
validate, | ||
})(connect(state => ({ | ||
apiEngine: state.apiEngine, | ||
}))(ChangePasswordForm)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import React, { Component } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { Field, reduxForm } from 'redux-form'; | ||
import Alert from 'react-bootstrap/lib/Alert'; | ||
import Button from 'react-bootstrap/lib/Button'; | ||
import userAPI from '../../../api/user'; | ||
import { pushErrors } from '../../../actions/errorActions'; | ||
import { setCookies } from '../../../actions/cookieActions'; | ||
import { Form, FormField, FormFooter } from '../../utils/BsForm'; | ||
|
||
export const validate = (values) => { | ||
const errors = {}; | ||
|
||
if (!values.name) { | ||
errors.name = 'Required'; | ||
} | ||
|
||
return errors; | ||
}; | ||
|
||
class EditForm extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.init = this._init.bind(this); | ||
this.handleSubmit = this._handleSubmit.bind(this); | ||
} | ||
|
||
_init(user) { | ||
let { initialize } = this.props; | ||
|
||
initialize({ | ||
name: user.name, | ||
}); | ||
} | ||
|
||
componentDidMount() { | ||
let { dispatch, apiEngine } = this.props; | ||
|
||
userAPI(apiEngine) | ||
.show() | ||
.catch((err) => { | ||
dispatch(pushErrors(err)); | ||
throw err; | ||
}) | ||
.then((json) => { | ||
this.init(json.user); | ||
}); | ||
} | ||
|
||
_handleSubmit(formData) { | ||
let { dispatch, apiEngine } = this.props; | ||
|
||
return userAPI(apiEngine) | ||
.update(formData) | ||
.catch((err) => { | ||
dispatch(pushErrors(err)); | ||
throw err; | ||
}) | ||
.then((json) => { | ||
this.init(json.user); | ||
dispatch(setCookies({ | ||
user: json.user, | ||
})); | ||
}); | ||
} | ||
|
||
render() { | ||
const { | ||
handleSubmit, | ||
submitSucceeded, | ||
submitFailed, | ||
error, | ||
pristine, | ||
submitting, | ||
invalid, | ||
} = this.props; | ||
|
||
return ( | ||
<Form horizontal onSubmit={handleSubmit(this.handleSubmit)}> | ||
{submitSucceeded && (<Alert bsStyle="success">Profile Saved</Alert>)} | ||
{submitFailed && error && (<Alert bsStyle="danger">{error}</Alert>)} | ||
<Field | ||
label="Name" | ||
name="name" | ||
component={FormField} | ||
type="text" | ||
placeholder="Name" | ||
/> | ||
<FormFooter> | ||
<Button type="submit" disabled={pristine || submitting || invalid}> | ||
Save | ||
{submitting && ( | ||
<i className="fa fa-spinner fa-spin" aria-hidden="true" /> | ||
)} | ||
</Button> | ||
</FormFooter> | ||
</Form> | ||
); | ||
} | ||
}; | ||
|
||
export default reduxForm({ | ||
form: 'userEdit', | ||
validate, | ||
})(connect(state => ({ | ||
apiEngine: state.apiEngine, | ||
}))(EditForm)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from 'react'; | ||
import { Link } from 'react-router'; | ||
import PageHeader from 'react-bootstrap/lib/PageHeader'; | ||
import Row from 'react-bootstrap/lib/Row'; | ||
import Col from 'react-bootstrap/lib/Col'; | ||
import Button from 'react-bootstrap/lib/Button'; | ||
import PageLayout from '../../layouts/PageLayout'; | ||
import EditForm from '../../forms/user/EditForm'; | ||
import ChangePasswordForm from '../../forms/user/ChangePasswordForm'; | ||
|
||
let EditPage = () => { | ||
return ( | ||
<PageLayout> | ||
<Row> | ||
<Col md={12}> | ||
<Link to="/user/me"> | ||
<Button>Finish</Button> | ||
</Link> | ||
</Col> | ||
</Row> | ||
<hr /> | ||
<Row> | ||
<Col md={6}> | ||
<PageHeader>Edit Profile</PageHeader> | ||
<EditForm /> | ||
</Col> | ||
<Col md={6}> | ||
<PageHeader>Change Password</PageHeader> | ||
<ChangePasswordForm /> | ||
</Col> | ||
</Row> | ||
</PageLayout> | ||
); | ||
}; | ||
|
||
export default EditPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default (store) => ({ | ||
path: 'me/edit', | ||
getComponent(nextState, cb) { | ||
require.ensure([], (require) => { | ||
cb(null, require('../../components/pages/user/EditPage').default); | ||
}); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.