AssociateModal.jsx 3.4 KB

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