PasswordSettings.jsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import { toastSuccess, toastError } from '../../util/apiNotification';
  5. import { withUnstatedContainers } from '../UnstatedUtils';
  6. import AppContainer from '../../services/AppContainer';
  7. import PersonalContainer from '../../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('personal_settings.update_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. <div className="container-fluid my-4">
  71. {(this.state.isPasswordSet)
  72. ? <h2 className="border-bottom">{t('personal_settings.update_password')}</h2>
  73. : <h2 className="border-bottom">{t('personal_settings.set_new_password')}</h2>}
  74. </div>
  75. {(this.state.isPasswordSet)
  76. && (
  77. <div className="row mb-3">
  78. <label htmlFor="oldPassword" className="col-md-3 text-md-right">{ t('personal_settings.current_password') }</label>
  79. <div className="col-md-5">
  80. <input
  81. className="form-control"
  82. type="password"
  83. name="oldPassword"
  84. value={this.state.oldPassword}
  85. onChange={(e) => { this.onChangeOldPassword(e.target.value) }}
  86. />
  87. </div>
  88. </div>
  89. )}
  90. <div className="row mb-3">
  91. <label htmlFor="newPassword" className="col-md-3 text-md-right">{t('personal_settings.new_password') }</label>
  92. <div className="col-md-5">
  93. <input
  94. className="form-control"
  95. type="password"
  96. name="newPassword"
  97. value={this.state.newPassword}
  98. onChange={(e) => { this.onChangeNewPassword(e.target.value) }}
  99. />
  100. </div>
  101. </div>
  102. <div className={`row mb-3 ${isIncorrectConfirmPassword && 'has-error'}`}>
  103. <label htmlFor="newPasswordConfirm" className="col-md-3 text-md-right">{t('personal_settings.new_password_confirm') }</label>
  104. <div className="col-md-5">
  105. <input
  106. className="form-control"
  107. type="password"
  108. name="newPasswordConfirm"
  109. value={this.state.newPasswordConfirm}
  110. onChange={(e) => { this.onChangeNewPasswordConfirm(e.target.value) }}
  111. />
  112. <p className="form-text text-muted">{t('page_register.form_help.password') }</p>
  113. </div>
  114. </div>
  115. <div className="row my-3">
  116. <div className="offset-5">
  117. <button
  118. type="button"
  119. className="btn btn-primary"
  120. onClick={this.onClickSubmit}
  121. disabled={isIncorrectConfirmPassword}
  122. >
  123. {t('Update')}
  124. </button>
  125. </div>
  126. </div>
  127. </React.Fragment>
  128. );
  129. }
  130. }
  131. const PasswordSettingsWrapper = withUnstatedContainers(PasswordSettings, [AppContainer, PersonalContainer]);
  132. PasswordSettings.propTypes = {
  133. t: PropTypes.func.isRequired, // i18next
  134. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  135. personalContainer: PropTypes.instanceOf(PersonalContainer).isRequired,
  136. };
  137. export default withTranslation()(PasswordSettingsWrapper);