mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 23:45:44 +01:00
Extracted PasswordConfirmation component
This commit is contained in:
105
scm-ui/src/users/components/PasswordConfirmation.js
Normal file
105
scm-ui/src/users/components/PasswordConfirmation.js
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
// @flow
|
||||||
|
|
||||||
|
import React from "react";
|
||||||
|
import { InputField } from "@scm-manager/ui-components";
|
||||||
|
import { compose } from "redux";
|
||||||
|
import { translate } from "react-i18next";
|
||||||
|
import * as userValidator from "./userValidation";
|
||||||
|
|
||||||
|
type State = {
|
||||||
|
password: string,
|
||||||
|
confirmedPassword: string,
|
||||||
|
passwordValid: boolean,
|
||||||
|
passwordConfirmationFailed: boolean
|
||||||
|
};
|
||||||
|
type Props = {
|
||||||
|
passwordChanged: string => void,
|
||||||
|
password: string,
|
||||||
|
// 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
|
||||||
|
label={t("user.password")}
|
||||||
|
type="password"
|
||||||
|
onChange={this.handlePasswordChange}
|
||||||
|
value={this.state.password ? this.state.password : ""}
|
||||||
|
validationError={!this.state.passwordValid}
|
||||||
|
errorMessage={t("validation.password-invalid")}
|
||||||
|
helpText={t("help.passwordHelpText")}
|
||||||
|
/>
|
||||||
|
<InputField
|
||||||
|
label={t("validation.validatePassword")}
|
||||||
|
type="password"
|
||||||
|
onChange={this.handlePasswordValidationChange}
|
||||||
|
value={this.state ? this.state.confirmedPassword : ""}
|
||||||
|
validationError={this.state.passwordConfirmationFailed}
|
||||||
|
errorMessage={t("validation.passwordValidation-invalid")}
|
||||||
|
helpText={t("help.passwordConfirmHelpText")}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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(
|
||||||
|
{
|
||||||
|
passwordValid: userValidator.isPasswordValid(password),
|
||||||
|
passwordConfirmationFailed,
|
||||||
|
password: password
|
||||||
|
},
|
||||||
|
this.propagateChange
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
propagateChange = () => {
|
||||||
|
if (
|
||||||
|
this.state.password &&
|
||||||
|
this.state.passwordValid &&
|
||||||
|
!this.state.passwordConfirmationFailed
|
||||||
|
) {
|
||||||
|
this.props.passwordChanged(this.state.password);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default compose(translate("users"))(PasswordConfirmation);
|
||||||
@@ -2,14 +2,13 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import type { User } from "@scm-manager/ui-types";
|
import type { User } from "@scm-manager/ui-types";
|
||||||
import {
|
import {
|
||||||
InputField,
|
|
||||||
SubmitButton,
|
SubmitButton,
|
||||||
Notification,
|
Notification,
|
||||||
ErrorNotification
|
ErrorNotification
|
||||||
} from "@scm-manager/ui-components";
|
} from "@scm-manager/ui-components";
|
||||||
import * as userValidator from "./userValidation";
|
|
||||||
import { translate } from "react-i18next";
|
import { translate } from "react-i18next";
|
||||||
import { setPassword } from "./changePassword";
|
import { setPassword } from "./changePassword";
|
||||||
|
import PasswordConfirmation from "./PasswordConfirmation";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
user: User,
|
user: User,
|
||||||
@@ -19,9 +18,6 @@ type Props = {
|
|||||||
type State = {
|
type State = {
|
||||||
password: string,
|
password: string,
|
||||||
loading: boolean,
|
loading: boolean,
|
||||||
passwordConfirmationError: boolean,
|
|
||||||
validatePasswordError: boolean,
|
|
||||||
validatePassword: string,
|
|
||||||
error?: Error,
|
error?: Error,
|
||||||
passwordChanged: boolean
|
passwordChanged: boolean
|
||||||
};
|
};
|
||||||
@@ -40,12 +36,6 @@ class SetUserPassword extends React.Component<Props, State> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
passwordIsValid = () => {
|
|
||||||
return !(
|
|
||||||
this.state.validatePasswordError || this.state.passwordConfirmationError
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
setLoadingState = () => {
|
setLoadingState = () => {
|
||||||
this.setState({
|
this.setState({
|
||||||
...this.state,
|
...this.state,
|
||||||
@@ -66,16 +56,13 @@ class SetUserPassword extends React.Component<Props, State> {
|
|||||||
...this.state,
|
...this.state,
|
||||||
loading: false,
|
loading: false,
|
||||||
passwordChanged: true,
|
passwordChanged: true,
|
||||||
password: "",
|
password: ""
|
||||||
validatePassword: "",
|
|
||||||
validatePasswordError: false,
|
|
||||||
passwordConfirmationError: false
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
submit = (event: Event) => {
|
submit = (event: Event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
if (this.passwordIsValid()) {
|
if (this.state.password) {
|
||||||
const { user } = this.props;
|
const { user } = this.props;
|
||||||
const { password } = this.state;
|
const { password } = this.state;
|
||||||
this.setLoadingState();
|
this.setLoadingState();
|
||||||
@@ -92,6 +79,7 @@ class SetUserPassword extends React.Component<Props, State> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
console.log("RENDER");
|
||||||
const { t } = this.props;
|
const { t } = this.props;
|
||||||
const { loading, passwordChanged, error } = this.state;
|
const { loading, passwordChanged, error } = this.state;
|
||||||
|
|
||||||
@@ -112,26 +100,12 @@ class SetUserPassword extends React.Component<Props, State> {
|
|||||||
return (
|
return (
|
||||||
<form onSubmit={this.submit}>
|
<form onSubmit={this.submit}>
|
||||||
{message}
|
{message}
|
||||||
<InputField
|
<PasswordConfirmation
|
||||||
label={t("user.password")}
|
passwordChanged={this.passwordChanged}
|
||||||
type="password"
|
key={this.state.passwordChanged ? "changed" : "unchanged"}
|
||||||
onChange={this.handlePasswordChange}
|
|
||||||
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
|
<SubmitButton
|
||||||
disabled={!this.passwordIsValid()}
|
disabled={!this.state.password}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
label={t("user-form.submit")}
|
label={t("user-form.submit")}
|
||||||
/>
|
/>
|
||||||
@@ -139,31 +113,8 @@ class SetUserPassword extends React.Component<Props, State> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
handlePasswordChange = (password: string) => {
|
passwordChanged = (password: string) => {
|
||||||
const validatePasswordError = !this.checkPasswords(
|
this.setState({ ...this.state, password });
|
||||||
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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
onClose = () => {
|
onClose = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user