update password function

This commit is contained in:
Maren Süwer
2018-11-05 16:14:54 +01:00
parent 4e444c1449
commit c6bc385b2f
4 changed files with 98 additions and 6 deletions

View File

@@ -55,6 +55,9 @@
"passwordValidation-invalid": "Passwords have to be the same", "passwordValidation-invalid": "Passwords have to be the same",
"validatePassword": "Please validate password here" "validatePassword": "Please validate password here"
}, },
"password": {
"set-password-successful": "Password is set successful"
},
"help": { "help": {
"usernameHelpText": "Unique name of the user.", "usernameHelpText": "Unique name of the user.",
"displayNameHelpText": "Display name of the user.", "displayNameHelpText": "Display name of the user.",

View File

@@ -1,9 +1,14 @@
// @flow // @flow
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 { InputField, SubmitButton } from "@scm-manager/ui-components"; import {
InputField,
SubmitButton,
Notification
} 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";
import { updatePassword } from "./updatePassword";
type Props = { type Props = {
user: User, user: User,
@@ -15,7 +20,9 @@ type State = {
loading: boolean, loading: boolean,
passwordValidationError: boolean, passwordValidationError: boolean,
validatePasswordError: boolean, validatePasswordError: boolean,
validatePassword: string validatePassword: string,
error?: Error,
passwordChanged: boolean
}; };
class SetUserPassword extends React.Component<Props, State> { class SetUserPassword extends React.Component<Props, State> {
@@ -27,7 +34,8 @@ class SetUserPassword extends React.Component<Props, State> {
loading: false, loading: false,
passwordValidationError: false, passwordValidationError: false,
validatePasswordError: false, validatePasswordError: false,
validatePassword: "" validatePassword: "",
passwordChanged: false
}; };
} }
@@ -38,22 +46,57 @@ class SetUserPassword extends React.Component<Props, State> {
}; };
submit = (event: Event) => { submit = (event: Event) => {
//TODO: set loading
event.preventDefault(); event.preventDefault();
if (this.isValid()) { if (this.isValid()) {
//TODO:hier update pw! const { user } = this.props;
const { password } = this.state;
updatePassword(user._links.password.href, password)
.then(result => {
if (result.error || result.status !== 204) {
this.setState({
...this.state,
error: result.error,
loading: false
});
} else {
this.setState({
...this.state,
loading: false,
passwordChanged: true,
password: "",
validatePassword: ""
});
}
})
.catch(err => {});
} }
}; };
render() { render() {
const { user, t } = this.props; const { user, t } = this.props;
const { loading } = this.state; const { loading, passwordChanged } = this.state;
let passwordChangedSuccessful = null;
if (passwordChanged) {
passwordChangedSuccessful = (
<Notification
type={"success"}
children={t("password.set-password-successful")}
onClose={() => this.onClose()}
/>
);
}
return ( return (
<form onSubmit={this.submit}> <form onSubmit={this.submit}>
{passwordChangedSuccessful}
<InputField <InputField
label={t("user.password")} label={t("user.password")}
type="password" type="password"
onChange={this.handlePasswordChange} onChange={this.handlePasswordChange}
value={user ? user.password : ""} value={this.state.password ? this.state.password : ""}
validationError={this.state.validatePasswordError} validationError={this.state.validatePasswordError}
errorMessage={t("validation.password-invalid")} errorMessage={t("validation.password-invalid")}
helpText={t("help.passwordHelpText")} helpText={t("help.passwordHelpText")}
@@ -102,6 +145,13 @@ class SetUserPassword extends React.Component<Props, State> {
checkPasswords = (password1: string, password2: string) => { checkPasswords = (password1: string, password2: string) => {
return password1 === password2; return password1 === password2;
}; };
onClose = () => {
this.setState({
...this.state,
passwordChanged: false
});
};
} }
export default translate("users")(SetUserPassword); export default translate("users")(SetUserPassword);

View File

@@ -0,0 +1,16 @@
//@flow
import { apiClient } from "@scm-manager/ui-components";
const CONTENT_TYPE_USER = "application/vnd.scmm-passwordOverwrite+json;v=2";
export function updatePassword(url: string, password: string) {
return apiClient
.put(url, { newPassword: password }, CONTENT_TYPE_USER)
.then(response => {
return {
status: response.status
};
})
.catch(err => {
return { error: err };
});
}

View File

@@ -0,0 +1,23 @@
//@flow
import fetchMock from "fetch-mock";
import { updatePassword } from "./updatePassword";
describe("get content type", () => {
const PASSWORD_URL = "/users/testuser/password";
const password = "testpw123";
afterEach(() => {
fetchMock.reset();
fetchMock.restore();
});
it("should update password", done => {
fetchMock.put("/api/v2" + PASSWORD_URL, 204);
updatePassword(PASSWORD_URL, password).then(content => {
done();
});
});
});