UserGroupUserFormByInput.jsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import { AsyncTypeahead } from 'react-bootstrap-typeahead';
  5. import { debounce } from 'throttle-debounce';
  6. import { createSubscribedElement } from '../../UnstatedUtils';
  7. import AppContainer from '../../../services/AppContainer';
  8. import AdminUserGroupDetailContainer from '../../../services/AdminUserGroupDetailContainer';
  9. import { toastSuccess, toastError } from '../../../util/apiNotification';
  10. import UserPicture from '../../User/UserPicture';
  11. class UserGroupUserFormByInput extends React.Component {
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. keyword: '',
  16. inputUser: '',
  17. applicableUsers: [],
  18. isLoading: false,
  19. searchError: null,
  20. };
  21. this.xss = window.xss;
  22. this.addUserBySubmit = this.addUserBySubmit.bind(this);
  23. this.validateForm = this.validateForm.bind(this);
  24. this.handleChange = this.handleChange.bind(this);
  25. this.handleSearch = this.handleSearch.bind(this);
  26. this.onKeyDown = this.onKeyDown.bind(this);
  27. this.renderMenuItemChildren = this.renderMenuItemChildren.bind(this);
  28. this.searhApplicableUsersDebounce = debounce(1000, this.searhApplicableUsers);
  29. }
  30. async addUserBySubmit() {
  31. const { adminUserGroupDetailContainer } = this.props;
  32. const { userGroup } = adminUserGroupDetailContainer.state;
  33. if (this.state.inputUser.length === 0) { return }
  34. const userName = this.state.inputUser[0].username;
  35. try {
  36. await adminUserGroupDetailContainer.addUserByUsername(userName);
  37. toastSuccess(`Added "${this.xss.process(userName)}" to "${this.xss.process(userGroup.name)}"`);
  38. this.setState({ inputUser: '' });
  39. }
  40. catch (err) {
  41. toastError(new Error(`Unable to add "${this.xss.process(userName)}" to "${this.xss.process(userGroup.name)}"`));
  42. }
  43. }
  44. validateForm() {
  45. return this.state.inputUser !== '';
  46. }
  47. async searhApplicableUsers() {
  48. const { adminUserGroupDetailContainer } = this.props;
  49. try {
  50. const users = await adminUserGroupDetailContainer.fetchApplicableUsers(this.state.keyword);
  51. this.setState({ applicableUsers: users, isLoading: false });
  52. }
  53. catch (err) {
  54. toastError(err);
  55. }
  56. }
  57. /**
  58. * Reflect when forecast is clicked
  59. * @param {object} inputUser
  60. */
  61. handleChange(inputUser) {
  62. this.setState({ inputUser });
  63. }
  64. handleSearch(keyword) {
  65. if (keyword === '') {
  66. return;
  67. }
  68. this.setState({ keyword, isLoading: true });
  69. this.searhApplicableUsersDebounce();
  70. }
  71. onKeyDown(event) {
  72. // 13 is Enter key
  73. if (event.keyCode === 13) {
  74. this.addUserBySubmit();
  75. }
  76. }
  77. renderMenuItemChildren(option) {
  78. const { adminUserGroupDetailContainer } = this.props;
  79. const user = option;
  80. return (
  81. <React.Fragment>
  82. <UserPicture user={user} size="sm" noLink noTooltip />
  83. <strong className="ml-2">{user.username}</strong>
  84. {adminUserGroupDetailContainer.state.isAlsoNameSearched && <span className="ml-2">{user.name}</span>}
  85. {adminUserGroupDetailContainer.state.isAlsoMailSearched && <span className="ml-2">{user.email}</span>}
  86. </React.Fragment>
  87. );
  88. }
  89. getEmptyLabel() {
  90. return (this.state.searchError !== null) && 'Error on searching.';
  91. }
  92. render() {
  93. const { t } = this.props;
  94. const inputProps = { autoComplete: 'off' };
  95. return (
  96. <div className="form-group row">
  97. <div className="col-8 pr-0">
  98. <AsyncTypeahead
  99. {...this.props}
  100. id="name-typeahead-asynctypeahead"
  101. ref={(c) => { this.typeahead = c }}
  102. inputProps={inputProps}
  103. isLoading={this.state.isLoading}
  104. labelKey={user => `${user.username} ${user.name} ${user.email}`}
  105. minLength={0}
  106. options={this.state.applicableUsers} // Search result
  107. searchText={(this.state.isLoading ? 'Searching...' : this.getEmptyLabel())}
  108. renderMenuItemChildren={this.renderMenuItemChildren}
  109. align="left"
  110. onChange={this.handleChange}
  111. onSearch={this.handleSearch}
  112. onKeyDown={this.onKeyDown}
  113. caseSensitive={false}
  114. clearButton
  115. />
  116. </div>
  117. <div className="col-2 pl-0">
  118. <button
  119. type="button"
  120. className="btn btn-success"
  121. disabled={!this.validateForm()}
  122. onClick={this.addUserBySubmit}
  123. >
  124. {t('add')}
  125. </button>
  126. </div>
  127. </div>
  128. );
  129. }
  130. }
  131. UserGroupUserFormByInput.propTypes = {
  132. t: PropTypes.func.isRequired, // i18next
  133. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  134. adminUserGroupDetailContainer: PropTypes.instanceOf(AdminUserGroupDetailContainer).isRequired,
  135. };
  136. /**
  137. * Wrapper component for using unstated
  138. */
  139. const UserGroupUserFormByInputWrapper = (props) => {
  140. return createSubscribedElement(UserGroupUserFormByInput, props, [AppContainer, AdminUserGroupDetailContainer]);
  141. };
  142. export default withTranslation()(UserGroupUserFormByInputWrapper);