PasswordSettings.jsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import { toastSuccess, toastError } from '~/client/util/apiNotification';
  5. import { withUnstatedContainers } from '../UnstatedUtils';
  6. import AppContainer from '~/client/services/AppContainer';
  7. import PersonalContainer from '~/client/services/PersonalContainer';
  8. class PasswordSettings extends React.Component {
  9. constructor(appContainer) {
  10. super();
  11. this.appContainer = appContainer;
  12. this.state = {
  13. retrieveError: null,
  14. oldPassword: '',
  15. newPassword: '',
  16. newPasswordConfirm: '',
  17. isPasswordSet: false,
  18. };
  19. this.onClickSubmit = this.onClickSubmit.bind(this);
  20. this.onChangeOldPassword = this.onChangeOldPassword.bind(this);
  21. }
  22. async componentDidMount() {
  23. const { appContainer } = this.props;
  24. try {
  25. const res = await appContainer.apiv3Get('/personal-setting/is-password-set');
  26. const { isPasswordSet } = res.data;
  27. this.setState({ isPasswordSet });
  28. }
  29. catch (err) {
  30. toastError(err);
  31. this.setState({ retrieveError: err });
  32. }
  33. }
  34. async onClickSubmit() {
  35. const { t, appContainer, personalContainer } = this.props;
  36. const { oldPassword, newPassword, newPasswordConfirm } = this.state;
  37. try {
  38. await appContainer.apiv3Put('/personal-setting/password', {
  39. oldPassword, newPassword, newPasswordConfirm,
  40. });
  41. this.setState({ oldPassword: '', newPassword: '', newPasswordConfirm: '' });
  42. await personalContainer.retrievePersonalData();
  43. toastSuccess(t('toaster.update_successed', { target: t('Password') }));
  44. }
  45. catch (err) {
  46. toastError(err);
  47. }
  48. }
  49. onChangeOldPassword(oldPassword) {
  50. this.setState({ oldPassword });
  51. }
  52. onChangeNewPassword(newPassword) {
  53. this.setState({ newPassword });
  54. }
  55. onChangeNewPasswordConfirm(newPasswordConfirm) {
  56. this.setState({ newPasswordConfirm });
  57. }
  58. render() {
  59. const { t } = this.props;
  60. const { newPassword, newPasswordConfirm } = this.state;
  61. const isIncorrectConfirmPassword = (newPassword !== newPasswordConfirm);
  62. if (this.state.retrieveError != null) {
  63. throw new Error(this.state.retrieveError.message);
  64. }
  65. return (
  66. <React.Fragment>
  67. { (!this.state.isPasswordSet) && (
  68. <div className="alert alert-warning">{ t('personal_settings.password_is_not_set') }</div>
  69. ) }
  70. {(this.state.isPasswordSet)
  71. ? <h2 className="border-bottom my-4">{t('personal_settings.update_password')}</h2>
  72. : <h2 className="border-bottom my-4">{t('personal_settings.set_new_password')}</h2>}
  73. {(this.state.isPasswordSet)
  74. && (
  75. <div className="row mb-3">
  76. <label htmlFor="oldPassword" className="col-md-3 text-md-right">{ t('personal_settings.current_password') }</label>
  77. <div className="col-md-5">
  78. <input
  79. className="form-control"
  80. type="password"
  81. name="oldPassword"
  82. value={this.state.oldPassword}
  83. onChange={(e) => { this.onChangeOldPassword(e.target.value) }}
  84. />
  85. </div>
  86. </div>
  87. )}
  88. <div className="row mb-3">
  89. <label htmlFor="newPassword" className="col-md-3 text-md-right">{t('personal_settings.new_password') }</label>
  90. <div className="col-md-5">
  91. <input
  92. className="form-control"
  93. type="password"
  94. name="newPassword"
  95. value={this.state.newPassword}
  96. onChange={(e) => { this.onChangeNewPassword(e.target.value) }}
  97. />
  98. </div>
  99. </div>
  100. <div className={`row mb-3 ${isIncorrectConfirmPassword && 'has-error'}`}>
  101. <label htmlFor="newPasswordConfirm" className="col-md-3 text-md-right">{t('personal_settings.new_password_confirm') }</label>
  102. <div className="col-md-5">
  103. <input
  104. className="form-control"
  105. type="password"
  106. name="newPasswordConfirm"
  107. value={this.state.newPasswordConfirm}
  108. onChange={(e) => { this.onChangeNewPasswordConfirm(e.target.value) }}
  109. />
  110. <p className="form-text text-muted">{t('page_register.form_help.password') }</p>
  111. </div>
  112. </div>
  113. <div className="row my-3">
  114. <div className="offset-5">
  115. <button
  116. type="button"
  117. className="btn btn-primary"
  118. onClick={this.onClickSubmit}
  119. disabled={isIncorrectConfirmPassword}
  120. >
  121. {t('Update')}
  122. </button>
  123. </div>
  124. </div>
  125. </React.Fragment>
  126. );
  127. }
  128. }
  129. const PasswordSettingsWrapper = withUnstatedContainers(PasswordSettings, [AppContainer, PersonalContainer]);
  130. PasswordSettings.propTypes = {
  131. t: PropTypes.func.isRequired, // i18next
  132. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  133. personalContainer: PropTypes.instanceOf(PersonalContainer).isRequired,
  134. };
  135. export default withTranslation()(PasswordSettingsWrapper);