UserGroupUserFormByInput.jsx 4.9 KB

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