Files
SCM-Manager/scm-ui/ui-webapp/src/users/components/UserForm.tsx

246 lines
5.9 KiB
TypeScript
Raw Normal View History

import React from 'react';
import { translate } from 'react-i18next';
import { User } from '@scm-manager/ui-types';
2018-12-20 10:21:18 +01:00
import {
Subtitle,
2018-12-20 10:21:18 +01:00
Checkbox,
InputField,
PasswordConfirmation,
SubmitButton,
validation as validator,
} from '@scm-manager/ui-components';
import * as userValidator from './userValidation';
2018-12-20 10:21:18 +01:00
type Props = {
submitForm: (p: User) => void;
user?: User;
loading?: boolean;
t: (p: string) => string;
2018-12-20 10:21:18 +01:00
};
type State = {
user: User;
mailValidationError: boolean;
nameValidationError: boolean;
displayNameValidationError: boolean;
passwordValid: boolean;
2018-12-20 10:21:18 +01:00
};
class UserForm extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
user: {
name: '',
displayName: '',
mail: '',
password: '',
2018-12-20 10:21:18 +01:00
active: true,
_links: {},
2018-12-20 10:21:18 +01:00
},
mailValidationError: false,
displayNameValidationError: false,
nameValidationError: false,
passwordValid: false,
2018-12-20 10:21:18 +01:00
};
}
componentDidMount() {
const { user } = this.props;
if (user) {
this.setState({
user: {
...user,
},
});
2018-12-20 10:21:18 +01:00
}
}
isFalsy(value) {
if (!value) {
return true;
}
return false;
}
2019-01-31 10:37:48 +01:00
createUserComponentsAreInvalid = () => {
2018-12-20 10:21:18 +01:00
const user = this.state.user;
if (!this.props.user) {
return (
this.state.nameValidationError ||
2019-01-28 14:54:01 +01:00
this.isFalsy(user.name) ||
!this.state.passwordValid
);
} else {
return false;
}
};
2019-01-28 14:54:01 +01:00
2019-01-31 10:37:48 +01:00
editUserComponentsAreUnchanged = () => {
const user = this.state.user;
if (this.props.user) {
return (
this.props.user.displayName === user.displayName &&
this.props.user.mail === user.mail &&
this.props.user.active === user.active
);
} else {
return false;
}
};
2018-12-20 10:21:18 +01:00
isValid = () => {
const user = this.state.user;
return !(
2019-01-31 10:37:48 +01:00
this.createUserComponentsAreInvalid() ||
this.editUserComponentsAreUnchanged() ||
2018-12-20 10:21:18 +01:00
this.state.mailValidationError ||
this.state.displayNameValidationError ||
this.isFalsy(user.displayName) ||
2019-01-28 14:54:01 +01:00
this.isFalsy(user.mail)
2018-12-20 10:21:18 +01:00
);
};
submit = (event: Event) => {
event.preventDefault();
if (this.isValid()) {
this.props.submitForm(this.state.user);
}
};
render() {
const { loading, t } = this.props;
const user = this.state.user;
let nameField = null;
let passwordChangeField = null;
2019-01-23 14:48:29 +01:00
let subtitle = null;
2018-12-20 10:21:18 +01:00
if (!this.props.user) {
2019-01-23 14:48:29 +01:00
// create new user
2018-12-20 10:21:18 +01:00
nameField = (
2019-01-25 15:58:36 +01:00
<div className="column is-half">
<InputField
label={t('user.name')}
2019-01-25 15:58:36 +01:00
onChange={this.handleUsernameChange}
value={user ? user.name : ''}
2019-01-25 15:58:36 +01:00
validationError={this.state.nameValidationError}
errorMessage={t('validation.name-invalid')}
helpText={t('help.usernameHelpText')}
2019-01-25 15:58:36 +01:00
/>
</div>
2018-12-20 10:21:18 +01:00
);
passwordChangeField = (
<PasswordConfirmation passwordChanged={this.handlePasswordChange} />
);
2019-01-23 14:48:29 +01:00
} else {
// edit existing user
subtitle = <Subtitle subtitle={t('userForm.subtitle')} />;
2018-12-20 10:21:18 +01:00
}
return (
<>
2019-01-23 14:48:29 +01:00
{subtitle}
<form onSubmit={this.submit}>
2019-02-01 13:51:14 +01:00
<div className="columns is-multiline">
{nameField}
<div className="column is-half">
<InputField
label={t('user.displayName')}
onChange={this.handleDisplayNameChange}
value={user ? user.displayName : ''}
validationError={this.state.displayNameValidationError}
errorMessage={t('validation.displayname-invalid')}
helpText={t('help.displayNameHelpText')}
/>
</div>
<div className="column is-half">
<InputField
label={t('user.mail')}
onChange={this.handleEmailChange}
value={user ? user.mail : ''}
validationError={this.state.mailValidationError}
errorMessage={t('validation.mail-invalid')}
helpText={t('help.mailHelpText')}
/>
</div>
2018-12-20 10:21:18 +01:00
</div>
{passwordChangeField}
<div className="columns">
<div className="column">
<Checkbox
label={t('user.active')}
onChange={this.handleActiveChange}
checked={user ? user.active : false}
helpText={t('help.activeHelpText')}
/>
</div>
2018-12-20 10:21:18 +01:00
</div>
<div className="columns">
<div className="column">
<SubmitButton
disabled={!this.isValid()}
loading={loading}
label={t('userForm.button')}
/>
</div>
2018-12-20 10:21:18 +01:00
</div>
</form>
</>
2018-12-20 10:21:18 +01:00
);
}
handleUsernameChange = (name: string) => {
this.setState({
nameValidationError: !validator.isNameValid(name),
user: {
...this.state.user,
name,
},
2018-12-20 10:21:18 +01:00
});
};
handleDisplayNameChange = (displayName: string) => {
this.setState({
displayNameValidationError: !userValidator.isDisplayNameValid(
displayName,
2018-12-20 10:21:18 +01:00
),
user: {
...this.state.user,
displayName,
},
2018-12-20 10:21:18 +01:00
});
};
handleEmailChange = (mail: string) => {
this.setState({
mailValidationError: !validator.isMailValid(mail),
user: {
...this.state.user,
mail,
},
2018-12-20 10:21:18 +01:00
});
};
handlePasswordChange = (password: string, passwordValid: boolean) => {
this.setState({
user: {
...this.state.user,
password,
},
passwordValid: !this.isFalsy(password) && passwordValid,
2018-12-20 10:21:18 +01:00
});
};
handleActiveChange = (active: boolean) => {
this.setState({
user: {
...this.state.user,
active,
},
});
2018-12-20 10:21:18 +01:00
};
}
export default translate('users')(UserForm);