show error and loading state

This commit is contained in:
Maren Süwer
2018-11-05 16:28:46 +01:00
parent c6bc385b2f
commit 3c63d994c6
2 changed files with 41 additions and 22 deletions

View File

@@ -4,7 +4,8 @@ import type { User } from "@scm-manager/ui-types";
import { import {
InputField, InputField,
SubmitButton, SubmitButton,
Notification Notification,
ErrorNotification
} from "@scm-manager/ui-components"; } from "@scm-manager/ui-components";
import * as userValidator from "./userValidation"; import * as userValidator from "./userValidation";
import { translate } from "react-i18next"; import { translate } from "react-i18next";
@@ -45,28 +46,46 @@ class SetUserPassword extends React.Component<Props, State> {
); );
}; };
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,
passwordValidationError: false
});
};
submit = (event: Event) => { submit = (event: Event) => {
//TODO: set loading //TODO: set loading
event.preventDefault(); event.preventDefault();
if (this.isValid()) { if (this.isValid()) {
const { user } = this.props; const { user } = this.props;
const { password } = this.state; const { password } = this.state;
this.setLoadingState();
updatePassword(user._links.password.href, password) updatePassword(user._links.password.href, password)
.then(result => { .then(result => {
if (result.error || result.status !== 204) { if (result.error) {
this.setState({ this.setErrorState(result.error);
...this.state,
error: result.error,
loading: false
});
} else { } else {
this.setState({ this.setSuccessfulState();
...this.state,
loading: false,
passwordChanged: true,
password: "",
validatePassword: ""
});
} }
}) })
.catch(err => {}); .catch(err => {});
@@ -74,24 +93,26 @@ class SetUserPassword extends React.Component<Props, State> {
}; };
render() { render() {
const { user, t } = this.props; const { t } = this.props;
const { loading, passwordChanged } = this.state; const { loading, passwordChanged, error } = this.state;
let passwordChangedSuccessful = null; let message = null;
if (passwordChanged) { if (passwordChanged) {
passwordChangedSuccessful = ( message = (
<Notification <Notification
type={"success"} type={"success"}
children={t("password.set-password-successful")} children={t("password.set-password-successful")}
onClose={() => this.onClose()} onClose={() => this.onClose()}
/> />
); );
} else if (error) {
message = <ErrorNotification error={error} />;
} }
return ( return (
<form onSubmit={this.submit}> <form onSubmit={this.submit}>
{passwordChangedSuccessful} {message}
<InputField <InputField
label={t("user.password")} label={t("user.password")}
type="password" type="password"

View File

@@ -6,9 +6,7 @@ export function updatePassword(url: string, password: string) {
return apiClient return apiClient
.put(url, { newPassword: password }, CONTENT_TYPE_USER) .put(url, { newPassword: password }, CONTENT_TYPE_USER)
.then(response => { .then(response => {
return { return response;
status: response.status
};
}) })
.catch(err => { .catch(err => {
return { error: err }; return { error: err };