import React from 'react'; import PropTypes from 'prop-types'; import { withTranslation } from 'react-i18next'; import { toastSuccess, toastError } from '../../util/apiNotification'; import { withUnstatedContainers } from '../UnstatedUtils'; import AppContainer from '../../services/AppContainer'; import PersonalContainer from '../../services/PersonalContainer'; class PasswordSettings extends React.Component { constructor(appContainer) { super(); this.appContainer = appContainer; this.state = { retrieveError: null, oldPassword: '', newPassword: '', newPasswordConfirm: '', isPasswordSet: false, }; this.onClickSubmit = this.onClickSubmit.bind(this); this.onChangeOldPassword = this.onChangeOldPassword.bind(this); } async componentDidMount() { const { appContainer } = this.props; try { const res = await appContainer.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, appContainer, personalContainer } = this.props; const { oldPassword, newPassword, newPasswordConfirm } = this.state; try { await appContainer.apiv3Put('/personal-setting/password', { oldPassword, newPassword, newPasswordConfirm, }); this.setState({ oldPassword: '', newPassword: '', newPasswordConfirm: '' }); await personalContainer.retrievePersonalData(); toastSuccess(t('toaster.update_successed', { target: t('personal_settings.update_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) }} />
)}
{ this.onChangeNewPassword(e.target.value) }} />
{ this.onChangeNewPasswordConfirm(e.target.value) }} />

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

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