import React, { useCallback } from 'react'; import { useTranslation } from 'next-i18next'; import PropTypes from 'prop-types'; import { apiv3Get, apiv3Put } from '~/client/util/apiv3-client'; import { toastSuccess, toastError } from '~/client/util/toastr'; import { usePersonalSettings } from '~/stores/personal-settings'; class PasswordSettings extends React.Component { constructor() { super(); this.state = { retrieveError: null, oldPassword: '', newPassword: '', newPasswordConfirm: '', isPasswordSet: false, minPasswordLength: null, }; this.submitHandler = this.submitHandler.bind(this); this.onChangeOldPassword = this.onChangeOldPassword.bind(this); } async componentDidMount() { try { const res = await apiv3Get('/personal-setting/is-password-set'); const { isPasswordSet, minPasswordLength } = res.data; this.setState({ isPasswordSet, minPasswordLength }); } catch (err) { toastError(err); this.setState({ retrieveError: err }); } } async submitHandler() { const { t, onSubmit } = this.props; const { oldPassword, newPassword, newPasswordConfirm } = this.state; try { await apiv3Put('/personal-setting/password', { oldPassword, newPassword, newPasswordConfirm, }); this.setState({ oldPassword: '', newPassword: '', newPasswordConfirm: '' }); if (onSubmit != null) { onSubmit(); } toastSuccess(t('toaster.update_successed', { target: t('Password'), ns: 'commons' })); } 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, minPasswordLength } = 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) && ( { t('personal_settings.current_password') } { this.onChangeOldPassword(e.target.value) }} /> )} {t('personal_settings.new_password') } {/* 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) }} /> {t('personal_settings.new_password_confirm') } { this.onChangeNewPasswordConfirm(e.target.value) }} /> {t('page_register.form_help.password', { target: minPasswordLength }) } {t('Update')} ); } } PasswordSettings.propTypes = { t: PropTypes.func.isRequired, // i18next onSubmit: PropTypes.func, }; const PasswordSettingsWrapperFC = (props) => { const { t } = useTranslation(); const { mutate: mutatePersonalSettings } = usePersonalSettings(); const submitHandler = useCallback(() => { mutatePersonalSettings(); }, [mutatePersonalSettings]); return ; }; export default PasswordSettingsWrapperFC;
{t('page_register.form_help.password', { target: minPasswordLength }) }