TeamPicker.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import React, { Component } from 'react';
  2. import { AsyncSelect } from './Select';
  3. import { debounce } from 'lodash';
  4. import { getBackendSrv } from 'app/core/services/backend_srv';
  5. export interface Team {
  6. id: number;
  7. label: string;
  8. name: string;
  9. avatarUrl: string;
  10. }
  11. export interface Props {
  12. onSelected: (team: Team) => void;
  13. className?: string;
  14. }
  15. export interface State {
  16. isLoading: boolean;
  17. }
  18. export class TeamPicker extends Component<Props, State> {
  19. debouncedSearch: any;
  20. constructor(props) {
  21. super(props);
  22. this.state = { isLoading: false };
  23. this.search = this.search.bind(this);
  24. this.debouncedSearch = debounce(this.search, 300, {
  25. leading: true,
  26. trailing: true,
  27. });
  28. }
  29. search(query?: string) {
  30. const backendSrv = getBackendSrv();
  31. this.setState({ isLoading: true });
  32. return backendSrv.get(`/api/teams/search?perpage=10&page=1&query=${query}`).then(result => {
  33. const teams = result.teams.map(team => {
  34. return {
  35. id: team.id,
  36. value: team.id,
  37. label: team.name,
  38. name: team.name,
  39. imgUrl: team.avatarUrl,
  40. };
  41. });
  42. this.setState({ isLoading: false });
  43. return teams;
  44. });
  45. }
  46. render() {
  47. const { onSelected, className } = this.props;
  48. const { isLoading } = this.state;
  49. return (
  50. <div className="user-picker">
  51. <AsyncSelect
  52. isLoading={isLoading}
  53. defaultOptions={true}
  54. loadOptions={this.debouncedSearch}
  55. onChange={onSelected}
  56. className={className}
  57. placeholder="Select a team"
  58. noOptionsMessage={() => 'No teams found'}
  59. />
  60. </div>
  61. );
  62. }
  63. }