2016-12-13 15:24:58 +00:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { goBack } from 'react-router-redux';
|
|
|
|
import moment from 'moment';
|
|
|
|
|
|
|
|
import Avatar from 'components/Avatar';
|
|
|
|
import Button from 'components/buttons/Button';
|
|
|
|
import ChangePasswordForm from 'components/forms/ChangePasswordForm';
|
2017-01-18 17:10:37 +00:00
|
|
|
import deepDifference from 'utilities/deep_difference';
|
2016-12-22 19:26:18 +00:00
|
|
|
import Icon from 'components/icons/Icon';
|
2017-01-06 00:01:17 +00:00
|
|
|
import { logoutUser, updateUser } from 'redux/nodes/auth/actions';
|
2016-12-13 15:24:58 +00:00
|
|
|
import Modal from 'components/modals/Modal';
|
|
|
|
import { renderFlash } from 'redux/nodes/notifications/actions';
|
2017-01-19 19:55:42 +00:00
|
|
|
import userActions from 'redux/nodes/entities/users/actions';
|
2016-12-13 15:24:58 +00:00
|
|
|
import userInterface from 'interfaces/user';
|
|
|
|
import UserSettingsForm from 'components/forms/UserSettingsForm';
|
|
|
|
|
2016-12-21 18:22:18 +00:00
|
|
|
const baseClass = 'user-settings';
|
2016-12-13 15:24:58 +00:00
|
|
|
|
2017-01-07 05:07:23 +00:00
|
|
|
export class UserSettingsPage extends Component {
|
2016-12-13 15:24:58 +00:00
|
|
|
static propTypes = {
|
|
|
|
dispatch: PropTypes.func.isRequired,
|
2017-01-06 00:01:17 +00:00
|
|
|
errors: PropTypes.shape({
|
|
|
|
username: PropTypes.string,
|
|
|
|
base: PropTypes.string,
|
|
|
|
}),
|
2016-12-13 15:24:58 +00:00
|
|
|
user: userInterface,
|
2017-01-19 19:55:42 +00:00
|
|
|
userErrors: PropTypes.shape({
|
|
|
|
base: PropTypes.string,
|
|
|
|
new_password: PropTypes.string,
|
|
|
|
old_password: PropTypes.string,
|
|
|
|
}),
|
2016-12-13 15:24:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = { showModal: false };
|
|
|
|
}
|
|
|
|
|
|
|
|
onCancel = (evt) => {
|
|
|
|
evt.preventDefault();
|
|
|
|
|
|
|
|
const { dispatch } = this.props;
|
|
|
|
|
|
|
|
dispatch(goBack());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
onLogout = (evt) => {
|
|
|
|
evt.preventDefault();
|
|
|
|
|
|
|
|
const { dispatch } = this.props;
|
|
|
|
|
|
|
|
dispatch(logoutUser());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
onShowModal = (evt) => {
|
|
|
|
evt.preventDefault();
|
|
|
|
|
|
|
|
this.setState({ showModal: true });
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
onToggleModal = (evt) => {
|
|
|
|
evt.preventDefault();
|
|
|
|
|
|
|
|
const { showModal } = this.state;
|
|
|
|
|
|
|
|
this.setState({ showModal: !showModal });
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit = (formData) => {
|
|
|
|
const { dispatch, user } = this.props;
|
2017-01-18 17:10:37 +00:00
|
|
|
const updatedUser = deepDifference(formData, user);
|
2016-12-13 15:24:58 +00:00
|
|
|
|
2017-01-18 17:10:37 +00:00
|
|
|
return dispatch(updateUser(user, updatedUser))
|
2016-12-13 15:24:58 +00:00
|
|
|
.then(() => {
|
|
|
|
return dispatch(renderFlash('success', 'Account updated!'));
|
2017-01-06 00:01:17 +00:00
|
|
|
})
|
|
|
|
.catch(() => false);
|
2016-12-13 15:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmitPasswordForm = (formData) => {
|
2017-01-19 19:55:42 +00:00
|
|
|
const { dispatch, user } = this.props;
|
2016-12-13 15:24:58 +00:00
|
|
|
|
2017-01-19 19:55:42 +00:00
|
|
|
return dispatch(userActions.changePassword(user, formData))
|
|
|
|
.then(() => {
|
|
|
|
dispatch(renderFlash('success', 'Password changed successfully'));
|
|
|
|
this.setState({ showModal: false });
|
2016-12-13 15:24:58 +00:00
|
|
|
|
2017-01-19 19:55:42 +00:00
|
|
|
return false;
|
|
|
|
});
|
2016-12-13 15:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderModal = () => {
|
2017-01-19 19:55:42 +00:00
|
|
|
const { userErrors } = this.props;
|
2016-12-13 15:24:58 +00:00
|
|
|
const { showModal } = this.state;
|
|
|
|
const { handleSubmitPasswordForm, onToggleModal } = this;
|
|
|
|
|
|
|
|
if (!showModal) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
title="Change Password"
|
|
|
|
onExit={onToggleModal}
|
|
|
|
>
|
|
|
|
<ChangePasswordForm
|
|
|
|
handleSubmit={handleSubmitPasswordForm}
|
|
|
|
onCancel={onToggleModal}
|
2017-01-19 19:55:42 +00:00
|
|
|
serverErrors={userErrors}
|
2016-12-13 15:24:58 +00:00
|
|
|
/>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { handleSubmit, onCancel, onLogout, onShowModal, renderModal } = this;
|
2017-01-06 00:01:17 +00:00
|
|
|
const { errors, user } = this.props;
|
2016-12-13 15:24:58 +00:00
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-07 05:07:23 +00:00
|
|
|
const { admin, updated_at: updatedAt } = user;
|
|
|
|
const roleText = admin ? 'ADMIN' : 'USER';
|
2016-12-13 15:24:58 +00:00
|
|
|
const lastUpdatedAt = moment(updatedAt).fromNow();
|
|
|
|
|
|
|
|
return (
|
2016-12-21 18:22:18 +00:00
|
|
|
<div className={baseClass}>
|
|
|
|
<div className={`${baseClass}__manage body-wrap`}>
|
2016-12-13 15:24:58 +00:00
|
|
|
<h1>Manage User Settings</h1>
|
2017-01-06 00:01:17 +00:00
|
|
|
<UserSettingsForm
|
|
|
|
formData={user}
|
|
|
|
handleSubmit={handleSubmit}
|
|
|
|
onCancel={onCancel}
|
|
|
|
serverErrors={errors}
|
|
|
|
/>
|
2016-12-13 15:24:58 +00:00
|
|
|
</div>
|
2016-12-21 18:22:18 +00:00
|
|
|
<div className={`${baseClass}__additional body-wrap`}>
|
2016-12-13 15:24:58 +00:00
|
|
|
<h1>Additional Info</h1>
|
2016-12-21 18:22:18 +00:00
|
|
|
|
|
|
|
<div className={`${baseClass}__change-avatar`}>
|
|
|
|
<Avatar user={user} className={`${baseClass}__avatar`} />
|
|
|
|
<a href="http://en.gravatar.com/emails/">Change Photo at Gravatar</a>
|
2016-12-13 15:24:58 +00:00
|
|
|
</div>
|
2016-12-21 18:22:18 +00:00
|
|
|
|
2016-12-13 15:24:58 +00:00
|
|
|
<div className={`${baseClass}__more-info-detail`}>
|
|
|
|
<Icon name="username" />
|
2017-01-07 05:07:23 +00:00
|
|
|
<strong>Role</strong> - {roleText}
|
2016-12-13 15:24:58 +00:00
|
|
|
</div>
|
|
|
|
<div className={`${baseClass}__more-info-detail`}>
|
|
|
|
<Icon name="lock-big" />
|
2016-12-21 18:22:18 +00:00
|
|
|
<strong>Password</strong>
|
2016-12-13 15:24:58 +00:00
|
|
|
</div>
|
2016-12-28 15:24:52 +00:00
|
|
|
<Button onClick={onShowModal} variant="brand" className={`${baseClass}__button`}>
|
|
|
|
CHANGE PASSWORD
|
|
|
|
</Button>
|
2016-12-21 18:22:18 +00:00
|
|
|
<p className={`${baseClass}__last-updated`}>Last changed: {lastUpdatedAt}</p>
|
2016-12-28 15:24:52 +00:00
|
|
|
<Button onClick={onLogout} variant="alert" className={`${baseClass}__button`}>
|
|
|
|
LOGOUT
|
|
|
|
</Button>
|
2016-12-13 15:24:58 +00:00
|
|
|
</div>
|
|
|
|
{renderModal()}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = (state) => {
|
2017-01-06 00:01:17 +00:00
|
|
|
const { errors, user } = state.auth;
|
2017-01-19 19:55:42 +00:00
|
|
|
const { errors: userErrors } = state.entities.users;
|
2016-12-13 15:24:58 +00:00
|
|
|
|
2017-01-19 19:55:42 +00:00
|
|
|
return { errors, user, userErrors };
|
2016-12-13 15:24:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(UserSettingsPage);
|