UserGroupUserFormByInput.jsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import React from 'react';
  2. import { UserPicture } from '@growi/ui/dist/components';
  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 { toastSuccess, toastError } from '~/client/util/toastr';
  8. import Xss from '~/services/xss';
  9. class UserGroupUserFormByInput extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. keyword: '',
  14. inputUser: '',
  15. applicableUsers: [],
  16. isLoading: false,
  17. searchError: null,
  18. };
  19. this.xss = new Xss();
  20. this.addUserBySubmit = this.addUserBySubmit.bind(this);
  21. this.validateForm = this.validateForm.bind(this);
  22. this.handleChange = this.handleChange.bind(this);
  23. this.handleSearch = this.handleSearch.bind(this);
  24. this.onKeyDown = this.onKeyDown.bind(this);
  25. this.renderMenuItemChildren = this.renderMenuItemChildren.bind(this);
  26. this.searhApplicableUsersDebounce = debounce(1000, this.searhApplicableUsers);
  27. }
  28. async addUserBySubmit() {
  29. const { userGroup, onClickAddUserBtn } = this.props;
  30. if (this.state.inputUser.length === 0) { return }
  31. const userName = this.state.inputUser[0].username;
  32. try {
  33. await onClickAddUserBtn(userName);
  34. toastSuccess(`Added "${this.xss.process(userName)}" to "${this.xss.process(userGroup.name)}"`);
  35. this.setState({ inputUser: '' });
  36. }
  37. catch (err) {
  38. toastError(new Error(`Unable to add "${this.xss.process(userName)}" to "${this.xss.process(userGroup.name)}"`));
  39. }
  40. }
  41. validateForm() {
  42. return this.state.inputUser !== '';
  43. }
  44. async searhApplicableUsers() {
  45. const { onSearchApplicableUsers } = this.props;
  46. try {
  47. const users = await onSearchApplicableUsers(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 { isAlsoNameSearched, isAlsoMailSearched } = this.props;
  76. const user = option;
  77. return (
  78. <>
  79. <UserPicture user={user} size="sm" noLink noTooltip />
  80. <strong className="ml-2">{user.username}</strong>
  81. {isAlsoNameSearched && <span className="ml-2">{user.name}</span>}
  82. {isAlsoMailSearched && <span className="ml-2">{user.email}</span>}
  83. </>
  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="form-group row">
  94. <div className="col-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-2 pl-0">
  115. <button
  116. type="button"
  117. className="btn 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. isAlsoMailSearched: PropTypes.bool.isRequired,
  131. isAlsoNameSearched: PropTypes.bool.isRequired,
  132. onClickAddUserBtn: PropTypes.func,
  133. onSearchApplicableUsers: PropTypes.func,
  134. userGroup: PropTypes.object,
  135. };
  136. const UserGroupUserFormByInputWrapperFC = (props) => {
  137. const { t } = useTranslation();
  138. return <UserGroupUserFormByInput t={t} {...props} />;
  139. };
  140. export default UserGroupUserFormByInputWrapperFC;