import React from 'react'; import PropTypes from 'prop-types'; import { withTranslation } from 'react-i18next'; import PersonalContainer from '~/client/services/PersonalContainer'; import { toastSuccess, toastError } from '~/client/util/apiNotification'; import { apiv3Get, apiv3Put } from '~/client/util/apiv3-client'; import { withUnstatedContainers } from '../UnstatedUtils'; class PasswordSettings extends React.Component { constructor() { super(); this.state = { retrieveError: null, oldPassword: '', newPassword: '', newPasswordConfirm: '', isPasswordSet: false, }; this.onClickSubmit = this.onClickSubmit.bind(this); this.onChangeOldPassword = this.onChangeOldPassword.bind(this); } async componentDidMount() { try { const res = await apiv3Get('/personal-setting/is-password-set'); const { isPasswordSet } = res.data; this.setState({ isPasswordSet }); } catch (err) { toastError(err); this.setState({ retrieveError: err }); } } async onClickSubmit() { const { t, personalContainer } = this.props; const { oldPassword, newPassword, newPasswordConfirm } = this.state; try { await apiv3Put('/personal-setting/password', { oldPassword, newPassword, newPasswordConfirm, }); this.setState({ oldPassword: '', newPassword: '', newPasswordConfirm: '' }); await personalContainer.retrievePersonalData(); toastSuccess(t('toaster.update_successed', { target: t('Password') })); } catch (err) { toastError(err); } } onChangeOldPassword(oldPassword) { this.setState({ oldPassword }); } onChangeNewPassword(newPassword) { this.setState({ newPassword }); } onChangeNewPasswordConfirm(newPasswordConfirm) { this.setState({ newPasswordConfirm }); } render() { const { t } = this.props; const { newPassword, newPasswordConfirm } = this.state; const isIncorrectConfirmPassword = (newPassword !== newPasswordConfirm); if (this.state.retrieveError != null) { throw new Error(this.state.retrieveError.message); } return ( { (!this.state.isPasswordSet) && (
{ t('personal_settings.password_is_not_set') }
) } {(this.state.isPasswordSet) ?

{t('personal_settings.update_password')}

:

{t('personal_settings.set_new_password')}

} {(this.state.isPasswordSet) && (
{ this.onChangeOldPassword(e.target.value) }} />
)}
{/* to prevent autocomplete username into userForm[email] in BasicInfoSettings component */} {/* https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion */} { this.onChangeNewPassword(e.target.value) }} />
{ this.onChangeNewPasswordConfirm(e.target.value) }} />

{t('page_register.form_help.password') }

); } } const PasswordSettingsWrapper = withUnstatedContainers(PasswordSettings, [PersonalContainer]); PasswordSettings.propTypes = { t: PropTypes.func.isRequired, // i18next personalContainer: PropTypes.instanceOf(PersonalContainer).isRequired, }; export default withTranslation()(PasswordSettingsWrapper);