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

130 lines
2.8 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 {
SubmitButton,
2018-11-05 16:28:46 +01:00
Notification,
2018-11-08 13:27:34 +01:00
ErrorNotification,
PasswordConfirmation
2018-11-05 16:14:54 +01:00
} from "@scm-manager/ui-components";
import { translate } from "react-i18next";
2018-11-08 13:27:34 +01:00
import { setPassword } from "./setPassword";
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,
2018-11-05 16:14:54 +01:00
error?: Error,
2018-12-06 12:00:30 +01:00
passwordChanged: boolean,
passwordValid: 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: "",
2018-12-06 12:00:30 +01:00
passwordChanged: false,
passwordValid: false
};
}
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: ""
2018-11-05 16:28:46 +01:00
});
};
submit = (event: Event) => {
event.preventDefault();
if (this.state.password) {
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("single-user.password.set-password-successful")}
2018-11-05 16:14:54 +01:00
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}
<PasswordConfirmation
passwordChanged={this.passwordChanged}
key={this.state.passwordChanged ? "changed" : "unchanged"}
/>
<SubmitButton
2018-12-06 12:00:30 +01:00
disabled={!this.state.passwordValid}
loading={loading}
label={t("single-user.password.button")}
/>
</form>
2018-11-05 15:08:58 +01:00
);
}
2018-12-06 12:00:30 +01:00
passwordChanged = (password: string, passwordValid: boolean) => {
this.setState({ ...this.state, password, passwordValid: (!!password && passwordValid) });
};
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);