PasswordSettings.jsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. };
  18. this.onClickSubmit = this.onClickSubmit.bind(this);
  19. this.onChangeOldPassword = this.onChangeOldPassword.bind(this);
  20. }
  21. async onClickSubmit() {
  22. const { t, appContainer, personalContainer } = this.props;
  23. const { oldPassword, newPassword, newPasswordConfirm } = this.state;
  24. try {
  25. await appContainer.apiv3Put('/personal-setting/password', {
  26. oldPassword, newPassword, newPasswordConfirm,
  27. });
  28. this.setState({ oldPassword: '', newPassword: '', newPasswordConfirm: '' });
  29. await personalContainer.retrievePersonalData();
  30. toastSuccess(t('toaster.update_successed', { target: t('personal_settings.update_password') }));
  31. }
  32. catch (err) {
  33. toastError(err);
  34. }
  35. }
  36. onChangeOldPassword(oldPassword) {
  37. this.setState({ oldPassword });
  38. }
  39. onChangeNewPassword(newPassword) {
  40. this.setState({ newPassword });
  41. }
  42. onChangeNewPasswordConfirm(newPasswordConfirm) {
  43. this.setState({ newPasswordConfirm });
  44. }
  45. render() {
  46. const { t, personalContainer } = this.props;
  47. const { newPassword, newPasswordConfirm } = this.state;
  48. const isIncorrectConfirmPassword = (newPassword !== newPasswordConfirm);
  49. return (
  50. <React.Fragment>
  51. { (!personalContainer.state.isPasswordSet) && (
  52. <div className="alert alert-warning">{ t('personal_settings.password_is_not_set') }</div>
  53. ) }
  54. <div className="container-fluid my-4">
  55. {(personalContainer.state.isPasswordSet)
  56. ? <h2 className="border-bottom">{t('personal_settings.update_password')}</h2>
  57. : <h2 className="border-bottom">{t('personal_settings.set_new_password')}</h2>}
  58. </div>
  59. {(personalContainer.state.isPasswordSet)
  60. && (
  61. <div className="row mb-3">
  62. <label htmlFor="oldPassword" className="col-md-3 text-md-right">{ t('personal_settings.current_password') }</label>
  63. <div className="col-md-5">
  64. <input
  65. className="form-control"
  66. type="password"
  67. name="oldPassword"
  68. value={this.state.oldPassword}
  69. onChange={(e) => { this.onChangeOldPassword(e.target.value) }}
  70. />
  71. </div>
  72. </div>
  73. )}
  74. <div className="row mb-3">
  75. <label htmlFor="newPassword" className="col-md-3 text-md-right">{t('personal_settings.new_password') }</label>
  76. <div className="col-md-5">
  77. <input
  78. className="form-control"
  79. type="password"
  80. name="newPassword"
  81. value={this.state.newPassword}
  82. onChange={(e) => { this.onChangeNewPassword(e.target.value) }}
  83. />
  84. </div>
  85. </div>
  86. <div className={`row mb-3 ${isIncorrectConfirmPassword && 'has-error'}`}>
  87. <label htmlFor="newPasswordConfirm" className="col-md-3 text-md-right">{t('personal_settings.new_password_confirm') }</label>
  88. <div className="col-md-5">
  89. <input
  90. className="form-control"
  91. type="password"
  92. name="newPasswordConfirm"
  93. value={this.state.newPasswordConfirm}
  94. onChange={(e) => { this.onChangeNewPasswordConfirm(e.target.value) }}
  95. />
  96. <p className="form-text text-muted">{t('page_register.form_help.password') }</p>
  97. </div>
  98. </div>
  99. <div className="row my-3">
  100. <div className="offset-5">
  101. <button
  102. type="button"
  103. className="btn btn-primary"
  104. onClick={this.onClickSubmit}
  105. disabled={this.state.retrieveError != null || isIncorrectConfirmPassword}
  106. >
  107. {t('Update')}
  108. </button>
  109. </div>
  110. </div>
  111. </React.Fragment>
  112. );
  113. }
  114. }
  115. const PasswordSettingsWrapper = withUnstatedContainers(PasswordSettings, [AppContainer, PersonalContainer]);
  116. PasswordSettings.propTypes = {
  117. t: PropTypes.func.isRequired, // i18next
  118. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  119. personalContainer: PropTypes.instanceOf(PersonalContainer).isRequired,
  120. };
  121. export default withTranslation()(PasswordSettingsWrapper);