diff --git a/scm-ui/public/locales/en/users.json b/scm-ui/public/locales/en/users.json index 34f2a9deb4..b5b98fc915 100644 --- a/scm-ui/public/locales/en/users.json +++ b/scm-ui/public/locales/en/users.json @@ -55,6 +55,9 @@ "passwordValidation-invalid": "Passwords have to be the same", "validatePassword": "Please validate password here" }, + "password": { + "set-password-successful": "Password is set successful" + }, "help": { "usernameHelpText": "Unique name of the user.", "displayNameHelpText": "Display name of the user.", diff --git a/scm-ui/src/users/components/SetUserPassword.js b/scm-ui/src/users/components/SetUserPassword.js index d47336bdef..0ceab52878 100644 --- a/scm-ui/src/users/components/SetUserPassword.js +++ b/scm-ui/src/users/components/SetUserPassword.js @@ -1,9 +1,14 @@ // @flow import React from "react"; 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 { translate } from "react-i18next"; +import { updatePassword } from "./updatePassword"; type Props = { user: User, @@ -15,7 +20,9 @@ type State = { loading: boolean, passwordValidationError: boolean, validatePasswordError: boolean, - validatePassword: string + validatePassword: string, + error?: Error, + passwordChanged: boolean }; class SetUserPassword extends React.Component { @@ -27,7 +34,8 @@ class SetUserPassword extends React.Component { loading: false, passwordValidationError: false, validatePasswordError: false, - validatePassword: "" + validatePassword: "", + passwordChanged: false }; } @@ -38,22 +46,57 @@ class SetUserPassword extends React.Component { }; submit = (event: Event) => { + //TODO: set loading event.preventDefault(); 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() { const { user, t } = this.props; - const { loading } = this.state; + const { loading, passwordChanged } = this.state; + + let passwordChangedSuccessful = null; + + if (passwordChanged) { + passwordChangedSuccessful = ( + this.onClose()} + /> + ); + } + return (
+ {passwordChangedSuccessful} { checkPasswords = (password1: string, password2: string) => { return password1 === password2; }; + + onClose = () => { + this.setState({ + ...this.state, + passwordChanged: false + }); + }; } export default translate("users")(SetUserPassword); diff --git a/scm-ui/src/users/components/updatePassword.js b/scm-ui/src/users/components/updatePassword.js new file mode 100644 index 0000000000..fb5adfaf2a --- /dev/null +++ b/scm-ui/src/users/components/updatePassword.js @@ -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 }; + }); +} diff --git a/scm-ui/src/users/components/updatePassword.test.js b/scm-ui/src/users/components/updatePassword.test.js new file mode 100644 index 0000000000..a5762406b2 --- /dev/null +++ b/scm-ui/src/users/components/updatePassword.test.js @@ -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(); + }); + }); +});