AssociateModal.jsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import Modal from 'react-bootstrap/es/Modal';
  5. import { toastSuccess, toastError } from '../../util/apiNotification';
  6. import { createSubscribedElement } from '../UnstatedUtils';
  7. import AppContainer from '../../services/AppContainer';
  8. import PersonalContainer from '../../services/PersonalContainer';
  9. import LdapAuthTest from '../Admin/Security/LdapAuthTest';
  10. class AssociateModal extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. username: '',
  15. password: '',
  16. };
  17. this.onChangeUsername = this.onChangeUsername.bind(this);
  18. this.onChangePassword = this.onChangePassword.bind(this);
  19. this.onClickAddBtn = this.onClickAddBtn.bind(this);
  20. }
  21. /**
  22. * Change username
  23. */
  24. onChangeUsername(username) {
  25. this.setState({ username });
  26. }
  27. /**
  28. * Change password
  29. */
  30. onChangePassword(password) {
  31. this.setState({ password });
  32. }
  33. async onClickAddBtn() {
  34. const { t, personalContainer } = this.props;
  35. const { username, password } = this.state;
  36. try {
  37. await personalContainer.associateLdapAccount({ username, password });
  38. this.props.onClose();
  39. toastSuccess(t('security_setting.updated_general_security_setting'));
  40. }
  41. catch (err) {
  42. toastError(err);
  43. }
  44. try {
  45. await personalContainer.retrieveExternalAccounts();
  46. }
  47. catch (err) {
  48. toastError(err);
  49. }
  50. }
  51. render() {
  52. const { t } = this.props;
  53. return (
  54. <Modal show={this.props.isOpen} onHide={this.props.onClose}>
  55. <Modal.Header className="bg-info" closeButton>
  56. <Modal.Title className="text-white">
  57. { t('admin:user_management.create_external_account') }
  58. </Modal.Title>
  59. </Modal.Header>
  60. <Modal.Body>
  61. <ul className="nav nav-tabs passport-settings mb-2" role="tablist">
  62. <li className="active">
  63. <a href="#passport-ldap" data-toggle="tab" role="tab"><i className="fa fa-sitemap"></i> LDAP</a>
  64. </li>
  65. <li className="tbd disabled"><a><i className="fa fa-github"></i> (TBD) GitHub</a></li>
  66. <li className="tbd disabled"><a><i className="fa fa-google"></i> (TBD) Google OAuth</a></li>
  67. <li className="tbd disabled"><a><i className="fa fa-facebook"></i> (TBD) Facebook</a></li>
  68. <li className="tbd disabled"><a><i className="fa fa-twitter"></i> (TBD) Twitter</a></li>
  69. </ul>
  70. <LdapAuthTest
  71. username={this.state.username}
  72. password={this.state.password}
  73. onChangeUsername={this.onChangeUsername}
  74. onChangePassword={this.onChangePassword}
  75. />
  76. </Modal.Body>
  77. <Modal.Footer>
  78. <button type="button" className="btn btn-info mt-3" onClick={this.onClickAddBtn}>
  79. <i className="fa fa-plus-circle" aria-hidden="true"></i>
  80. {t('add')}
  81. </button>
  82. </Modal.Footer>
  83. </Modal>
  84. );
  85. }
  86. }
  87. const AssociateModalWrapper = (props) => {
  88. return createSubscribedElement(AssociateModal, props, [AppContainer, PersonalContainer]);
  89. };
  90. AssociateModal.propTypes = {
  91. t: PropTypes.func.isRequired, // i18next
  92. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  93. personalContainer: PropTypes.instanceOf(PersonalContainer).isRequired,
  94. isOpen: PropTypes.bool.isRequired,
  95. onClose: PropTypes.func.isRequired,
  96. };
  97. export default withTranslation()(AssociateModalWrapper);