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,
|
2018-11-05 16:28:46 +01:00
|
|
|
Notification,
|
|
|
|
|
ErrorNotification
|
2018-11-05 16:14:54 +01:00
|
|
|
} 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,
|
2018-11-06 15:48:12 +01:00
|
|
|
passwordConfirmationError: boolean,
|
2018-11-05 15:18:44 +01:00
|
|
|
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,
|
2018-11-06 15:48:12 +01:00
|
|
|
passwordConfirmationError: false,
|
2018-11-05 15:18:44 +01:00
|
|
|
validatePasswordError: false,
|
2018-11-05 16:14:54 +01:00
|
|
|
validatePassword: "",
|
|
|
|
|
passwordChanged: false
|
2018-11-05 15:18:44 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 15:25:49 +01:00
|
|
|
passwordIsValid = () => {
|
2018-11-05 15:18:44 +01:00
|
|
|
return !(
|
2018-11-06 15:48:12 +01:00
|
|
|
this.state.validatePasswordError || this.state.passwordConfirmationError
|
2018-11-05 15:18:44 +01:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-05 16:28:46 +01:00
|
|
|
setLoadingState = () => {
|
|
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
loading: true
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
setErrorState = (error: Error) => {
|
|
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
error: error,
|
|
|
|
|
loading: false
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
setSuccessfulState = () => {
|
|
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
loading: false,
|
|
|
|
|
passwordChanged: true,
|
|
|
|
|
password: "",
|
|
|
|
|
validatePassword: "",
|
|
|
|
|
validatePasswordError: false,
|
2018-11-06 15:48:12 +01:00
|
|
|
passwordConfirmationError: false
|
2018-11-05 16:28:46 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-05 15:18:44 +01:00
|
|
|
submit = (event: Event) => {
|
|
|
|
|
event.preventDefault();
|
2018-11-06 15:25:49 +01:00
|
|
|
if (this.passwordIsValid()) {
|
2018-11-05 16:14:54 +01:00
|
|
|
const { user } = this.props;
|
|
|
|
|
const { password } = this.state;
|
2018-11-05 16:28:46 +01:00
|
|
|
this.setLoadingState();
|
2018-11-05 16:14:54 +01:00
|
|
|
updatePassword(user._links.password.href, password)
|
|
|
|
|
.then(result => {
|
2018-11-05 16:28:46 +01:00
|
|
|
if (result.error) {
|
|
|
|
|
this.setErrorState(result.error);
|
2018-11-05 16:14:54 +01:00
|
|
|
} else {
|
2018-11-05 16:28:46 +01:00
|
|
|
this.setSuccessfulState();
|
2018-11-05 16:14:54 +01:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.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() {
|
2018-11-05 16:28:46 +01:00
|
|
|
const { t } = this.props;
|
|
|
|
|
const { loading, passwordChanged, error } = this.state;
|
2018-11-05 16:14:54 +01:00
|
|
|
|
2018-11-05 16:28:46 +01:00
|
|
|
let message = null;
|
2018-11-05 16:14:54 +01:00
|
|
|
|
|
|
|
|
if (passwordChanged) {
|
2018-11-05 16:28:46 +01:00
|
|
|
message = (
|
2018-11-05 16:14:54 +01:00
|
|
|
<Notification
|
|
|
|
|
type={"success"}
|
|
|
|
|
children={t("password.set-password-successful")}
|
|
|
|
|
onClose={() => this.onClose()}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2018-11-05 16:28:46 +01:00
|
|
|
} else if (error) {
|
|
|
|
|
message = <ErrorNotification error={error} />;
|
2018-11-05 16:14:54 +01:00
|
|
|
}
|
|
|
|
|
|
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:28:46 +01:00
|
|
|
{message}
|
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 : ""}
|
2018-11-06 15:48:12 +01:00
|
|
|
validationError={this.state.passwordConfirmationError}
|
2018-11-05 15:18:44 +01:00
|
|
|
errorMessage={t("validation.passwordValidation-invalid")}
|
|
|
|
|
helpText={t("help.passwordConfirmHelpText")}
|
|
|
|
|
/>
|
|
|
|
|
<SubmitButton
|
2018-11-06 15:25:49 +01:00
|
|
|
disabled={!this.passwordIsValid()}
|
2018-11-05 15:18:44 +01:00
|
|
|
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),
|
2018-11-06 15:48:12 +01:00
|
|
|
passwordConfirmationError: validatePasswordError,
|
2018-11-05 15:18:44 +01:00
|
|
|
password: password
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handlePasswordValidationChange = (validatePassword: string) => {
|
2018-11-06 15:48:12 +01:00
|
|
|
const passwordConfirmed = this.checkPasswords(
|
2018-11-05 15:18:44 +01:00
|
|
|
this.state.password,
|
|
|
|
|
validatePassword
|
|
|
|
|
);
|
|
|
|
|
this.setState({
|
|
|
|
|
validatePassword,
|
2018-11-06 15:48:12 +01:00
|
|
|
passwordConfirmationError: !passwordConfirmed
|
2018-11-05 15:18:44 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|