2022-08-22 10:51:33 -05:00
|
|
|
import { debounce, DebouncedFuncLeading, isNil } from 'lodash';
|
2022-04-22 08:33:13 -05:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
|
2021-06-01 07:47:29 -05:00
|
|
|
import { SelectableValue } from '@grafana/data';
|
2020-01-21 03:08:07 -06:00
|
|
|
import { getBackendSrv } from '@grafana/runtime';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { AsyncSelect } from '@grafana/ui';
|
2021-06-01 07:47:29 -05:00
|
|
|
import { Team } from 'app/types';
|
2018-01-17 09:52:18 -06:00
|
|
|
|
2018-07-11 13:23:07 -05:00
|
|
|
export interface Props {
|
2021-06-01 07:47:29 -05:00
|
|
|
onSelected: (team: SelectableValue<Team>) => void;
|
2018-01-31 09:44:14 -06:00
|
|
|
className?: string;
|
2023-10-16 10:02:12 -05:00
|
|
|
teamId?: number;
|
2018-01-17 09:52:18 -06:00
|
|
|
}
|
|
|
|
|
2018-07-11 13:23:07 -05:00
|
|
|
export interface State {
|
2018-10-07 14:08:22 -05:00
|
|
|
isLoading: boolean;
|
2023-10-16 10:02:12 -05:00
|
|
|
value?: SelectableValue<Team>;
|
2018-01-17 09:52:18 -06:00
|
|
|
}
|
|
|
|
|
2018-07-11 13:23:07 -05:00
|
|
|
export class TeamPicker extends Component<Props, State> {
|
2022-08-22 10:51:33 -05:00
|
|
|
debouncedSearch: DebouncedFuncLeading<typeof this.search>;
|
2018-01-17 09:52:18 -06:00
|
|
|
|
2019-05-12 07:15:23 -05:00
|
|
|
constructor(props: Props) {
|
2018-01-17 09:52:18 -06:00
|
|
|
super(props);
|
2018-07-11 13:23:07 -05:00
|
|
|
this.state = { isLoading: false };
|
2018-01-17 09:52:18 -06:00
|
|
|
this.search = this.search.bind(this);
|
2018-01-18 08:49:15 -06:00
|
|
|
|
2018-01-17 09:52:18 -06:00
|
|
|
this.debouncedSearch = debounce(this.search, 300, {
|
|
|
|
leading: true,
|
2018-10-08 04:50:08 -05:00
|
|
|
trailing: true,
|
2018-01-17 09:52:18 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-10-16 10:02:12 -05:00
|
|
|
componentDidMount(): void {
|
|
|
|
const { teamId } = this.props;
|
|
|
|
if (!teamId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
getBackendSrv()
|
|
|
|
.get(`/api/teams/${teamId}`)
|
|
|
|
.then((team: Team) => {
|
|
|
|
this.setState({
|
|
|
|
value: {
|
|
|
|
value: team,
|
|
|
|
label: team.name,
|
|
|
|
imgUrl: team.avatarUrl,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-17 09:52:18 -06:00
|
|
|
search(query?: string) {
|
2018-07-11 13:23:07 -05:00
|
|
|
this.setState({ isLoading: true });
|
2018-01-17 09:52:18 -06:00
|
|
|
|
2021-04-21 02:38:00 -05:00
|
|
|
if (isNil(query)) {
|
2018-12-17 07:31:43 -06:00
|
|
|
query = '';
|
|
|
|
}
|
|
|
|
|
2020-01-21 03:08:07 -06:00
|
|
|
return getBackendSrv()
|
|
|
|
.get(`/api/teams/search?perpage=100&page=1&query=${query}`)
|
2021-06-01 07:47:29 -05:00
|
|
|
.then((result: { teams: Team[] }) => {
|
|
|
|
const teams: Array<SelectableValue<Team>> = result.teams.map((team) => {
|
2020-01-21 03:08:07 -06:00
|
|
|
return {
|
2021-06-01 07:47:29 -05:00
|
|
|
value: team,
|
2020-01-21 03:08:07 -06:00
|
|
|
label: team.name,
|
|
|
|
imgUrl: team.avatarUrl,
|
|
|
|
};
|
|
|
|
});
|
2018-01-17 09:52:18 -06:00
|
|
|
|
2020-01-21 03:08:07 -06:00
|
|
|
this.setState({ isLoading: false });
|
|
|
|
return teams;
|
|
|
|
});
|
2018-01-17 09:52:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-10-07 14:08:22 -05:00
|
|
|
const { onSelected, className } = this.props;
|
2023-10-16 10:02:12 -05:00
|
|
|
const { isLoading, value } = this.state;
|
2018-01-17 09:52:18 -06:00
|
|
|
return (
|
2020-09-21 08:33:50 -05:00
|
|
|
<div className="user-picker" data-testid="teamPicker">
|
2018-10-07 14:08:22 -05:00
|
|
|
<AsyncSelect
|
2018-01-17 09:52:18 -06:00
|
|
|
isLoading={isLoading}
|
2018-10-07 14:08:22 -05:00
|
|
|
defaultOptions={true}
|
2018-01-17 09:52:18 -06:00
|
|
|
loadOptions={this.debouncedSearch}
|
2023-10-16 10:02:12 -05:00
|
|
|
value={value}
|
2018-07-11 13:23:07 -05:00
|
|
|
onChange={onSelected}
|
2018-12-14 07:19:39 -06:00
|
|
|
className={className}
|
2018-07-11 13:23:07 -05:00
|
|
|
placeholder="Select a team"
|
2020-11-02 05:55:34 -06:00
|
|
|
noOptionsMessage="No teams found"
|
2021-10-20 08:54:00 -05:00
|
|
|
aria-label="Team picker"
|
2018-01-17 09:52:18 -06:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|