/* * SonarQube * Copyright (C) 2009-2019 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import * as React from 'react'; import { changePassword } from '../../../api/users'; import { SubmitButton } from '../../../components/ui/buttons'; import { translate } from '../../../helpers/l10n'; import { Alert } from '../../../components/ui/Alert'; interface Props { user: T.LoggedInUser; } interface State { errors?: string[]; success: boolean; } export default class Password extends React.Component { oldPassword!: HTMLInputElement; password!: HTMLInputElement; passwordConfirmation!: HTMLInputElement; state: State = { success: false }; handleSuccessfulChange = () => { this.oldPassword.value = ''; this.password.value = ''; this.passwordConfirmation.value = ''; this.setState({ success: true, errors: undefined }); }; setErrors = (errors: string[]) => { this.setState({ success: false, errors }); }; handleChangePassword = (event: React.FormEvent) => { event.preventDefault(); const { user } = this.props; const previousPassword = this.oldPassword.value; const password = this.password.value; const passwordConfirmation = this.passwordConfirmation.value; if (password !== passwordConfirmation) { this.password.focus(); this.setErrors([translate('user.password_doesnt_match_confirmation')]); } else { changePassword({ login: user.login, password, previousPassword }).then( this.handleSuccessfulChange, () => {} ); } }; render() { const { success, errors } = this.state; return (

{translate('my_profile.password.title')}

{success && {translate('my_profile.password.changed')}} {errors && errors.map((e, i) => ( {e} ))}
(this.oldPassword = elem!)} required={true} type="password" />
(this.password = elem!)} required={true} type="password" />
(this.passwordConfirmation = elem!)} required={true} type="password" />
{translate('my_profile.password.submit')}
); } }