Skip to content

Commit

Permalink
A few improvements from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldutra committed Feb 8, 2019
1 parent ae5652e commit a89ae71
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
11 changes: 5 additions & 6 deletions client/app/components/dynamic-form/DynamicForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ import Checkbox from 'antd/lib/checkbox';
import Button from 'antd/lib/button';
import Upload from 'antd/lib/upload';
import Icon from 'antd/lib/icon';
import { includes } from 'lodash';
import { react2angular } from 'react2angular';
import { toastr } from '@/services/ng';
import { Field, Action, AntdForm } from '../proptypes';
import helper from './dynamicFormHelper';

const fieldRules = ({ title, name, type, required, minLength }) => {
const fieldLabel = title || helper.toHuman(name);

const fieldRules = ({ type, required, minLength }) => {
const requiredRule = required;
const minLengthRule = minLength && ['text', 'email', 'password'].includes(type);
const minLengthRule = minLength && includes(['text', 'email', 'password'], type);

return [
requiredRule && { required, message: `${fieldLabel} is required.` },
minLengthRule && { min: minLength, message: `${fieldLabel} is too short.` },
requiredRule && { required, message: 'This field is required.' },
minLengthRule && { min: minLength, message: 'This field is too short.' },
].filter(rule => rule);
};

Expand Down
60 changes: 31 additions & 29 deletions client/app/components/users/UserEdit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,15 @@ export class UserEdit extends React.Component {
super(props);
this.state = {
user: this.props.user,
changingPassword: false,
passwordModalIsOpen: false,
sendingPasswordEmail: false,
resendingInvitation: false,
togglingUser: false,
};
}

onSaveUser = (values, successCallback, errorCallback) => {
const data = {
id: this.props.user.id,
...values,
};

User.save(data, (user) => {
successCallback('Saved.');
this.setState({ user: User.convertUserInfo(user) });
}, (error) => {
errorCallback(error.data.message || 'Failed saving.');
});
};

onUpdatePassword = (values, successCallback, errorCallback) => {
if (values.password === values.password_repeat) {
this.onSaveUser(values, successCallback, errorCallback);
} else {
errorCallback('Passwords don\'t match.');
}
}

onClickChangePassword = () => {
this.setState({ changingPassword: true });
this.setState({ passwordModalIsOpen: true });
};

onClickSendPasswordReset = () => {
Expand All @@ -75,7 +54,8 @@ export class UserEdit extends React.Component {
onClickRegenerateApiKey = () => {
const doRegenerate = () => {
User.regenerateApiKey(this.state.user).then((apiKey) => {
this.setState(prevState => ({ user: { ...prevState.user, apiKey } }));
const { user } = this.state;
this.setState({ user: { ...user, apiKey } });
});
};

Expand All @@ -101,6 +81,28 @@ export class UserEdit extends React.Component {
});
};

saveUser = (values, successCallback, errorCallback) => {
const data = {
id: this.props.user.id,
...values,
};

User.save(data, (user) => {
successCallback('Saved.');
this.setState({ user: User.convertUserInfo(user) });
}, (error = {}) => {
errorCallback(error.data && error.data.message || 'Failed saving.');
});
};

updatePassword = (values, successCallback, errorCallback) => {
if (values.password === values.password_repeat) {
this.saveUser(values, successCallback, errorCallback);
} else {
errorCallback('Passwords don\'t match.');
}
}

renderBasicInfoForm() {
const { user } = this.state;
const formFields = [
Expand All @@ -124,7 +126,7 @@ export class UserEdit extends React.Component {
<DynamicForm
fields={formFields}
readOnly={user.isDisabled}
onSubmit={this.onSaveUser}
onSubmit={this.saveUser}
/>
);
}
Expand All @@ -140,13 +142,13 @@ export class UserEdit extends React.Component {
<Fragment>
<hr />
<Modal
visible={this.state.changingPassword}
visible={this.state.passwordModalIsOpen}
title="Change Password"
onCancel={() => { this.setState({ changingPassword: false }); }}
onCancel={() => { this.setState({ passwordModalIsOpen: false }); }}
footer={null}
destroyOnClose
>
<DynamicForm fields={fields} saveText="Update Password" onSubmit={this.onUpdatePassword} />
<DynamicForm fields={fields} saveText="Update Password" onSubmit={this.updatePassword} />
</Modal>
<Button className="w-100 m-t-10" onClick={this.onClickChangePassword} data-test="ChangePassword">
Change Password
Expand Down

0 comments on commit a89ae71

Please sign in to comment.