Files
SCM-Manager/scm-ui/src/containers/ChangeUserPassword.js

148 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-11-07 11:52:30 +01:00
// @flow
import React from "react";
import {
ErrorNotification,
2018-11-08 13:27:34 +01:00
InputField,
2018-11-09 15:36:35 +01:00
Notification,
PasswordConfirmation,
SubmitButton
2018-11-08 13:27:34 +01:00
} from "@scm-manager/ui-components";
import { translate } from "react-i18next";
import type { Me } from "@scm-manager/ui-types";
import { changePassword } from "../modules/changePassword";
2018-11-07 11:52:30 +01:00
type Props = {
2018-11-07 17:05:46 +01:00
me: Me,
2018-11-07 11:52:30 +01:00
t: string => string
};
type State = {
oldPassword: string,
password: string,
loading: boolean,
error?: Error,
2018-12-06 12:00:30 +01:00
passwordChanged: boolean,
passwordValid: boolean
2018-11-07 11:52:30 +01:00
};
class ChangeUserPassword extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
oldPassword: "",
password: "",
loading: false,
passwordConfirmationError: false,
validatePasswordError: false,
validatePassword: "",
2018-12-06 12:00:30 +01:00
passwordChanged: false,
passwordValid: false
2018-11-07 11:52:30 +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,
oldPassword: "",
password: ""
});
};
submit = (event: Event) => {
event.preventDefault();
if (this.state.password) {
const { oldPassword, password } = this.state;
this.setLoadingState();
2018-11-08 13:27:34 +01:00
changePassword(this.props.me._links.password.href, oldPassword, password)
2018-11-07 11:52:30 +01:00
.then(result => {
if (result.error) {
this.setErrorState(result.error);
} else {
this.setSuccessfulState();
}
})
2018-11-09 15:36:35 +01:00
.catch(err => {
this.setErrorState(err);
});
2018-11-07 11:52:30 +01:00
}
};
2018-12-06 12:00:30 +01:00
isValid = () => {
return this.state.oldPassword && this.state.passwordValid;
};
2018-11-07 11:52:30 +01:00
render() {
const { t } = this.props;
const { loading, passwordChanged, error } = this.state;
let message = null;
if (passwordChanged) {
message = (
<Notification
type={"success"}
2018-11-09 15:43:36 +01:00
children={t("password.changedSuccessfully")}
2018-11-07 11:52:30 +01:00
onClose={() => this.onClose()}
/>
);
} else if (error) {
message = <ErrorNotification error={error} />;
}
return (
<form onSubmit={this.submit}>
{message}
<InputField
2018-11-09 15:43:36 +01:00
label={t("password.currentPassword")}
2018-11-07 11:52:30 +01:00
type="password"
onChange={oldPassword =>
this.setState({ ...this.state, oldPassword })
}
value={this.state.oldPassword ? this.state.oldPassword : ""}
2018-11-09 15:43:36 +01:00
helpText={t("password.currentPasswordHelpText")}
2018-11-07 11:52:30 +01:00
/>
<PasswordConfirmation
passwordChanged={this.passwordChanged}
key={this.state.passwordChanged ? "changed" : "unchanged"}
/>
<SubmitButton
2018-12-06 12:00:30 +01:00
disabled={!this.isValid()}
2018-11-07 11:52:30 +01:00
loading={loading}
2018-11-09 15:43:36 +01:00
label={t("password.submit")}
2018-11-07 11:52:30 +01:00
/>
</form>
);
}
2018-12-06 12:00:30 +01:00
passwordChanged = (password: string, passwordValid: boolean) => {
this.setState({ ...this.state, password, passwordValid: (!!password && passwordValid) });
2018-11-07 11:52:30 +01:00
};
onClose = () => {
this.setState({
...this.state,
passwordChanged: false
});
};
}
2018-11-09 15:43:36 +01:00
export default translate("commons")(ChangeUserPassword);