Files
SCM-Manager/scm-ui/src/users/components/SetUserPassword.js

178 lines
4.3 KiB
JavaScript
Raw Normal View History

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";
import * as userValidator from "./userValidation";
import { translate } from "react-i18next";
import { setPassword } from "./changePassword";
2018-11-05 15:08:58 +01:00
type Props = {
user: User,
t: string => string
2018-11-05 15:08:58 +01:00
};
type State = {
password: string,
loading: boolean,
passwordConfirmationError: boolean,
validatePasswordError: boolean,
2018-11-05 16:14:54 +01:00
validatePassword: string,
error?: Error,
passwordChanged: boolean
};
2018-11-05 15:08:58 +01:00
class SetUserPassword extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
password: "",
loading: false,
passwordConfirmationError: false,
validatePasswordError: false,
2018-11-05 16:14:54 +01:00
validatePassword: "",
passwordChanged: false
};
}
2018-11-06 15:25:49 +01:00
passwordIsValid = () => {
return !(
this.state.validatePasswordError || this.state.passwordConfirmationError
);
};
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,
passwordConfirmationError: false
2018-11-05 16:28:46 +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();
setPassword(user._links.password.href, password)
2018-11-05 16:14:54 +01:00
.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:08:58 +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 (
<form onSubmit={this.submit}>
2018-11-05 16:28:46 +01:00
{message}
<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 : ""}
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.passwordConfirmationError}
errorMessage={t("validation.passwordValidation-invalid")}
helpText={t("help.passwordConfirmHelpText")}
/>
<SubmitButton
2018-11-06 15:25:49 +01:00
disabled={!this.passwordIsValid()}
loading={loading}
label={t("user-form.submit")}
/>
</form>
2018-11-05 15:08:58 +01:00
);
}
handlePasswordChange = (password: string) => {
const validatePasswordError = !this.checkPasswords(
password,
this.state.validatePassword
);
this.setState({
validatePasswordError: !userValidator.isPasswordValid(password),
passwordConfirmationError: validatePasswordError,
password: password
});
};
handlePasswordValidationChange = (validatePassword: string) => {
const passwordConfirmed = this.checkPasswords(
this.state.password,
validatePassword
);
this.setState({
validatePassword,
passwordConfirmationError: !passwordConfirmed
});
};
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
}
export default translate("users")(SetUserPassword);