UserGroupUserFormByInput.jsx 4.8 KB

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