2018-11-05 15:08:58 +01:00
|
|
|
// @flow
|
|
|
|
|
import React from "react";
|
|
|
|
|
import type { User } from "@scm-manager/ui-types";
|
2018-11-05 16:14:54 +01:00
|
|
|
import {
|
|
|
|
|
InputField,
|
|
|
|
|
SubmitButton,
|
|
|
|
|
Notification
|
|
|
|
|
} from "@scm-manager/ui-components";
|
2018-11-05 15:18:44 +01:00
|
|
|
import * as userValidator from "./userValidation";
|
|
|
|
|
import { translate } from "react-i18next";
|
2018-11-05 16:14:54 +01:00
|
|
|
import { updatePassword } from "./updatePassword";
|
2018-11-05 15:08:58 +01:00
|
|
|
|
|
|
|
|
type Props = {
|
2018-11-05 15:18:44 +01:00
|
|
|
user: User,
|
|
|
|
|
t: string => string
|
2018-11-05 15:08:58 +01:00
|
|
|
};
|
|
|
|
|
|
2018-11-05 15:18:44 +01:00
|
|
|
type State = {
|
|
|
|
|
password: string,
|
|
|
|
|
loading: boolean,
|
|
|
|
|
passwordValidationError: boolean,
|
|
|
|
|
validatePasswordError: boolean,
|
2018-11-05 16:14:54 +01:00
|
|
|
validatePassword: string,
|
|
|
|
|
error?: Error,
|
|
|
|
|
passwordChanged: boolean
|
2018-11-05 15:18:44 +01:00
|
|
|
};
|
2018-11-05 15:08:58 +01:00
|
|
|
|
2018-11-05 15:18:44 +01:00
|
|
|
class SetUserPassword extends React.Component<Props, State> {
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
password: "",
|
|
|
|
|
loading: false,
|
|
|
|
|
passwordValidationError: false,
|
|
|
|
|
validatePasswordError: false,
|
2018-11-05 16:14:54 +01:00
|
|
|
validatePassword: "",
|
|
|
|
|
passwordChanged: false
|
2018-11-05 15:18:44 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isValid = () => {
|
|
|
|
|
return !(
|
|
|
|
|
this.state.validatePasswordError || this.state.passwordValidationError
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
submit = (event: Event) => {
|
2018-11-05 16:14:54 +01:00
|
|
|
//TODO: set loading
|
2018-11-05 15:18:44 +01:00
|
|
|
event.preventDefault();
|
|
|
|
|
if (this.isValid()) {
|
2018-11-05 16:14:54 +01:00
|
|
|
const { user } = this.props;
|
|
|
|
|
const { password } = this.state;
|
|
|
|
|
updatePassword(user._links.password.href, password)
|
|
|
|
|
.then(result => {
|
|
|
|
|
if (result.error || result.status !== 204) {
|
|
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
error: result.error,
|
|
|
|
|
loading: false
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
loading: false,
|
|
|
|
|
passwordChanged: true,
|
|
|
|
|
password: "",
|
|
|
|
|
validatePassword: ""
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {});
|
2018-11-05 15:18:44 +01:00
|
|
|
}
|
|
|
|
|
};
|
2018-11-05 15:08:58 +01:00
|
|
|
|
2018-11-05 15:18:44 +01:00
|
|
|
render() {
|
|
|
|
|
const { user, t } = this.props;
|
2018-11-05 16:14:54 +01:00
|
|
|
const { loading, passwordChanged } = this.state;
|
|
|
|
|
|
|
|
|
|
let passwordChangedSuccessful = null;
|
|
|
|
|
|
|
|
|
|
if (passwordChanged) {
|
|
|
|
|
passwordChangedSuccessful = (
|
|
|
|
|
<Notification
|
|
|
|
|
type={"success"}
|
|
|
|
|
children={t("password.set-password-successful")}
|
|
|
|
|
onClose={() => this.onClose()}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 15:08:58 +01:00
|
|
|
return (
|
2018-11-05 15:18:44 +01:00
|
|
|
<form onSubmit={this.submit}>
|
2018-11-05 16:14:54 +01:00
|
|
|
{passwordChangedSuccessful}
|
2018-11-05 15:18:44 +01:00
|
|
|
<InputField
|
|
|
|
|
label={t("user.password")}
|
|
|
|
|
type="password"
|
|
|
|
|
onChange={this.handlePasswordChange}
|
2018-11-05 16:14:54 +01:00
|
|
|
value={this.state.password ? this.state.password : ""}
|
2018-11-05 15:18:44 +01:00
|
|
|
validationError={this.state.validatePasswordError}
|
|
|
|
|
errorMessage={t("validation.password-invalid")}
|
|
|
|
|
helpText={t("help.passwordHelpText")}
|
|
|
|
|
/>
|
|
|
|
|
<InputField
|
|
|
|
|
label={t("validation.validatePassword")}
|
|
|
|
|
type="password"
|
|
|
|
|
onChange={this.handlePasswordValidationChange}
|
|
|
|
|
value={this.state ? this.state.validatePassword : ""}
|
|
|
|
|
validationError={this.state.passwordValidationError}
|
|
|
|
|
errorMessage={t("validation.passwordValidation-invalid")}
|
|
|
|
|
helpText={t("help.passwordConfirmHelpText")}
|
|
|
|
|
/>
|
|
|
|
|
<SubmitButton
|
|
|
|
|
disabled={!this.isValid()}
|
|
|
|
|
loading={loading}
|
|
|
|
|
label={t("user-form.submit")}
|
|
|
|
|
/>
|
|
|
|
|
</form>
|
2018-11-05 15:08:58 +01:00
|
|
|
);
|
|
|
|
|
}
|
2018-11-05 15:18:44 +01:00
|
|
|
|
|
|
|
|
handlePasswordChange = (password: string) => {
|
|
|
|
|
const validatePasswordError = !this.checkPasswords(
|
|
|
|
|
password,
|
|
|
|
|
this.state.validatePassword
|
|
|
|
|
);
|
|
|
|
|
this.setState({
|
|
|
|
|
validatePasswordError: !userValidator.isPasswordValid(password),
|
|
|
|
|
passwordValidationError: validatePasswordError,
|
|
|
|
|
password: password
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handlePasswordValidationChange = (validatePassword: string) => {
|
|
|
|
|
const validatePasswordError = this.checkPasswords(
|
|
|
|
|
this.state.password,
|
|
|
|
|
validatePassword
|
|
|
|
|
);
|
|
|
|
|
this.setState({
|
|
|
|
|
validatePassword,
|
|
|
|
|
passwordValidationError: !validatePasswordError
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
checkPasswords = (password1: string, password2: string) => {
|
|
|
|
|
return password1 === password2;
|
|
|
|
|
};
|
2018-11-05 16:14:54 +01:00
|
|
|
|
|
|
|
|
onClose = () => {
|
|
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
passwordChanged: false
|
|
|
|
|
});
|
|
|
|
|
};
|
2018-11-05 15:08:58 +01:00
|
|
|
}
|
2018-11-05 15:18:44 +01:00
|
|
|
|
|
|
|
|
export default translate("users")(SetUserPassword);
|