From 1caab8adbf1e334516cafedc715c16023091ff1b Mon Sep 17 00:00:00 2001 From: Philipp Czora Date: Wed, 7 Nov 2018 11:52:30 +0100 Subject: [PATCH] Bootstrapped ChangeUserPassword.js --- scm-ui/public/locales/en/users.json | 4 +- scm-ui/src/containers/Main.js | 6 + .../users/components/ChangeUserPassword.js | 143 ++++++++++++++++++ 3 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 scm-ui/src/users/components/ChangeUserPassword.js diff --git a/scm-ui/public/locales/en/users.json b/scm-ui/public/locales/en/users.json index 7199cb2135..ea285a1cec 100644 --- a/scm-ui/public/locales/en/users.json +++ b/scm-ui/public/locales/en/users.json @@ -56,14 +56,16 @@ "validatePassword": "Confirm password" }, "password": { + "current-password": "Current password", "set-password-successful": "Password successfully set" }, "help": { "usernameHelpText": "Unique name of the user.", "displayNameHelpText": "Display name of the user.", "mailHelpText": "Email address of the user.", + "currentPasswordHelpText": "Enter your current password", "passwordHelpText": "Plain text password of the user.", - "passwordConfirmHelpText": "Repeat the password for validation.", + "passwordConfirmHelpText": "Repeat the password for confirmation.", "adminHelpText": "An administrator is able to create, modify and delete repositories, groups and users.", "activeHelpText": "Activate or deactive the user." } diff --git a/scm-ui/src/containers/Main.js b/scm-ui/src/containers/Main.js index a971bab54d..538d03ca48 100644 --- a/scm-ui/src/containers/Main.js +++ b/scm-ui/src/containers/Main.js @@ -19,6 +19,7 @@ import SingleGroup from "../groups/containers/SingleGroup"; import AddGroup from "../groups/containers/AddGroup"; import Config from "../config/containers/Config"; +import ChangeUserPassword from "../users/components/ChangeUserPassword"; type Props = { authenticated?: boolean @@ -78,6 +79,11 @@ class Main extends React.Component { path="/user/:name" component={SingleUser} /> + string +}; + +type State = { + oldPassword: string, + password: string, + loading: boolean, + error?: Error, + passwordChanged: boolean +}; + +class ChangeUserPassword extends React.Component { + constructor(props: Props) { + super(props); + + this.state = { + oldPassword: "", + password: "", + loading: false, + passwordConfirmationError: false, + validatePasswordError: false, + validatePassword: "", + passwordChanged: false + }; + } + + 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(); + updatePassword( + "http://localhost:8081/scm/api/v2/me/password", // TODO: Change this, as soon we have a profile component + oldPassword, + password + ) + .then(result => { + if (result.error) { + this.setErrorState(result.error); + } else { + this.setSuccessfulState(); + } + }) + .catch(err => {}); + } + }; + + render() { + const { t } = this.props; + const { loading, passwordChanged, error } = this.state; + + let message = null; + + if (passwordChanged) { + message = ( + this.onClose()} + /> + ); + } else if (error) { + message = ; + } + + return ( +
+ {message} + + this.setState({ ...this.state, oldPassword }) + } + value={this.state.oldPassword ? this.state.oldPassword : ""} + helpText={t("help.currentPasswordHelpText")} + /> + + + + ); + } + + passwordChanged = (password: string) => { + this.setState({ ...this.state, password }); + }; + + onClose = () => { + this.setState({ + ...this.state, + passwordChanged: false + }); + }; +} + +export default translate("users")(ChangeUserPassword);