2018-11-07 10:27:47 +01:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
|
|
import React from "react";
|
2018-11-09 15:43:36 +01:00
|
|
|
import {translate} from "react-i18next";
|
2018-11-08 13:27:34 +01:00
|
|
|
import InputField from "./InputField";
|
2018-11-07 10:27:47 +01:00
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
|
password: string,
|
|
|
|
|
confirmedPassword: string,
|
|
|
|
|
passwordValid: boolean,
|
|
|
|
|
passwordConfirmationFailed: boolean
|
|
|
|
|
};
|
|
|
|
|
type Props = {
|
|
|
|
|
passwordChanged: string => void,
|
2018-11-08 13:27:34 +01:00
|
|
|
passwordValidator?: string => boolean,
|
2018-11-07 10:27:47 +01:00
|
|
|
// Context props
|
|
|
|
|
t: string => string
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class PasswordConfirmation extends React.Component<Props, State> {
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
password: "",
|
|
|
|
|
confirmedPassword: "",
|
|
|
|
|
passwordValid: true,
|
|
|
|
|
passwordConfirmationFailed: false
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
this.setState({
|
|
|
|
|
password: "",
|
|
|
|
|
confirmedPassword: "",
|
|
|
|
|
passwordValid: true,
|
|
|
|
|
passwordConfirmationFailed: false
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { t } = this.props;
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<InputField
|
2018-11-09 15:43:36 +01:00
|
|
|
label={t("password.newPassword")}
|
2018-11-07 10:27:47 +01:00
|
|
|
type="password"
|
|
|
|
|
onChange={this.handlePasswordChange}
|
|
|
|
|
value={this.state.password ? this.state.password : ""}
|
|
|
|
|
validationError={!this.state.passwordValid}
|
2018-11-08 13:27:34 +01:00
|
|
|
errorMessage={t("password.passwordInvalid")}
|
|
|
|
|
helpText={t("password.passwordHelpText")}
|
2018-11-07 10:27:47 +01:00
|
|
|
/>
|
|
|
|
|
<InputField
|
2018-11-08 13:27:34 +01:00
|
|
|
label={t("password.confirmPassword")}
|
2018-11-07 10:27:47 +01:00
|
|
|
type="password"
|
|
|
|
|
onChange={this.handlePasswordValidationChange}
|
|
|
|
|
value={this.state ? this.state.confirmedPassword : ""}
|
|
|
|
|
validationError={this.state.passwordConfirmationFailed}
|
2018-11-08 13:27:34 +01:00
|
|
|
errorMessage={t("password.passwordConfirmFailed")}
|
|
|
|
|
helpText={t("password.passwordConfirmHelpText")}
|
2018-11-07 10:27:47 +01:00
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-08 13:27:34 +01:00
|
|
|
validatePassword = password => {
|
|
|
|
|
const { passwordValidator } = this.props;
|
|
|
|
|
if (passwordValidator) {
|
|
|
|
|
return passwordValidator(password);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return password.length >= 6 && password.length < 32;
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-07 10:27:47 +01:00
|
|
|
handlePasswordValidationChange = (confirmedPassword: string) => {
|
|
|
|
|
const passwordConfirmed = this.state.password === confirmedPassword;
|
|
|
|
|
|
|
|
|
|
this.setState(
|
|
|
|
|
{
|
|
|
|
|
confirmedPassword,
|
|
|
|
|
passwordConfirmationFailed: !passwordConfirmed
|
|
|
|
|
},
|
|
|
|
|
this.propagateChange
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handlePasswordChange = (password: string) => {
|
|
|
|
|
const passwordConfirmationFailed =
|
|
|
|
|
password !== this.state.confirmedPassword;
|
|
|
|
|
|
|
|
|
|
this.setState(
|
|
|
|
|
{
|
2018-11-08 13:27:34 +01:00
|
|
|
passwordValid: this.validatePassword(password),
|
2018-11-07 10:27:47 +01:00
|
|
|
passwordConfirmationFailed,
|
|
|
|
|
password: password
|
|
|
|
|
},
|
|
|
|
|
this.propagateChange
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
propagateChange = () => {
|
|
|
|
|
if (
|
|
|
|
|
this.state.password &&
|
|
|
|
|
this.state.passwordValid &&
|
|
|
|
|
!this.state.passwordConfirmationFailed
|
|
|
|
|
) {
|
|
|
|
|
this.props.passwordChanged(this.state.password);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-08 13:27:34 +01:00
|
|
|
export default translate("commons")(PasswordConfirmation);
|