UserGroupUserFormByInput.jsx 5.1 KB

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