UserPicker.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import React, { Component } from "react";
  2. import { debounce } from "lodash";
  3. import Select from "react-select";
  4. import UserPickerOption from "./UserPickerOption";
  5. export interface IProps {
  6. backendSrv: any;
  7. teamId: string;
  8. refreshList: any;
  9. }
  10. export interface User {
  11. id: number;
  12. name: string;
  13. login: string;
  14. email: string;
  15. }
  16. class UserPicker extends Component<IProps, any> {
  17. debouncedSearchUsers: any;
  18. backendSrv: any;
  19. teamId: string;
  20. refreshList: any;
  21. constructor(props) {
  22. super(props);
  23. this.backendSrv = this.props.backendSrv;
  24. this.teamId = this.props.teamId;
  25. this.refreshList = this.props.refreshList;
  26. this.searchUsers = this.searchUsers.bind(this);
  27. this.handleChange = this.handleChange.bind(this);
  28. this.addUser = this.addUser.bind(this);
  29. this.toggleLoading = this.toggleLoading.bind(this);
  30. this.debouncedSearchUsers = debounce(this.searchUsers, 300, {
  31. leading: true,
  32. trailing: false
  33. });
  34. this.state = {
  35. multi: false,
  36. isLoading: false
  37. };
  38. }
  39. componentWillReceiveProps(nextProps) {
  40. console.log("componentWillReceiveProps", nextProps);
  41. }
  42. handleChange(user) {
  43. console.log("user", user);
  44. this.addUser(user.id);
  45. }
  46. toggleLoading(isLoading) {
  47. this.setState(prevState => {
  48. return {
  49. ...prevState,
  50. isLoading: isLoading
  51. };
  52. });
  53. }
  54. addUser(userId) {
  55. this.toggleLoading(true);
  56. this.backendSrv
  57. .post(`/api/teams/${this.teamId}/members`, { userId: userId })
  58. .then(() => {
  59. this.refreshList(); // this.get() in the angular controller
  60. this.toggleLoading(false);
  61. // this.$scope.$broadcast('user-picker-reset'); // TODO?
  62. });
  63. }
  64. searchUsers(query) {
  65. this.toggleLoading(true);
  66. return this.backendSrv
  67. .get(`/api/users/search?perpage=10&page=1&query=${query}`)
  68. .then(result => {
  69. const users = result.users.map(user => {
  70. return {
  71. id: user.id,
  72. label: `${user.login} - ${user.email}`,
  73. avatarUrl: user.avatarUrl
  74. };
  75. });
  76. this.toggleLoading(false);
  77. return { options: users };
  78. });
  79. }
  80. render() {
  81. const AsyncComponent = this.state.creatable
  82. ? Select.AsyncCreatable
  83. : Select.Async;
  84. return (
  85. <div className="user-picker">
  86. <AsyncComponent
  87. valueKey="id"
  88. multi={this.state.multi}
  89. labelKey="label"
  90. cache={false}
  91. isLoading={this.state.isLoading}
  92. loadOptions={this.debouncedSearchUsers}
  93. loadingPlaceholder="Loading..."
  94. onChange={this.handleChange}
  95. className="width-8 gf-form-input gf-form-input--form-dropdown"
  96. optionComponent={UserPickerOption}
  97. placeholder="Choose"
  98. />
  99. </div>
  100. );
  101. }
  102. }
  103. export default UserPicker;