PasswordSettings.jsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 { createSubscribedElement } 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) && <div className="alert alert-warning m-t-10">{ t('Password is not set') }</div>}
  52. <div className="container-fluid my-4">
  53. {(personalContainer.state.isPasswordSet)
  54. ? <h2 className="border-bottom">{t('personal_settings.update_password')}</h2>
  55. : <h2 className="border-bottom">{t('personal_settings.set_new_password')}</h2>}
  56. </div>
  57. {(personalContainer.state.isPasswordSet)
  58. && (
  59. <div className="row mb-3">
  60. <label htmlFor="oldPassword" className="col-md-3 text-md-right">{ t('personal_settings.current_password') }</label>
  61. <div className="col-md-5">
  62. <input
  63. className="form-control"
  64. type="password"
  65. name="oldPassword"
  66. value={this.state.oldPassword}
  67. onChange={(e) => { this.onChangeOldPassword(e.target.value) }}
  68. />
  69. </div>
  70. </div>
  71. )}
  72. <div className="row mb-3">
  73. <label htmlFor="newPassword" className="col-md-3 text-md-right">{t('personal_settings.new_password') }</label>
  74. <div className="col-md-5">
  75. <input
  76. className="form-control"
  77. type="password"
  78. name="newPassword"
  79. value={this.state.newPassword}
  80. onChange={(e) => { this.onChangeNewPassword(e.target.value) }}
  81. />
  82. </div>
  83. </div>
  84. <div className={`row mb-3 ${isIncorrectConfirmPassword && 'has-error'}`}>
  85. <label htmlFor="newPasswordConfirm" className="col-md-3 text-md-right">{t('personal_settings.new_password_confirm') }</label>
  86. <div className="col-md-5">
  87. <input
  88. className="form-control"
  89. type="password"
  90. name="newPasswordConfirm"
  91. value={this.state.newPasswordConfirm}
  92. onChange={(e) => { this.onChangeNewPasswordConfirm(e.target.value) }}
  93. />
  94. <p className="form-text text-muted">{t('page_register.form_help.password') }</p>
  95. </div>
  96. </div>
  97. <div className="row my-3">
  98. <div className="offset-5">
  99. <button
  100. type="button"
  101. className="btn btn-primary"
  102. onClick={this.onClickSubmit}
  103. disabled={this.state.retrieveError != null || isIncorrectConfirmPassword}
  104. >
  105. {t('Update')}
  106. </button>
  107. </div>
  108. </div>
  109. </React.Fragment>
  110. );
  111. }
  112. }
  113. const PasswordSettingsWrapper = (props) => {
  114. return createSubscribedElement(PasswordSettings, props, [AppContainer, PersonalContainer]);
  115. };
  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);