2018-12-14 19:38:36 +08:00
|
|
|
// Libraries
|
2018-01-17 23:52:18 +08:00
|
|
|
import React, { Component } from 'react';
|
2018-12-17 21:31:43 +08:00
|
|
|
import _ from 'lodash';
|
2018-12-14 19:38:36 +08:00
|
|
|
|
|
|
|
|
// Components
|
2020-04-02 16:57:35 +08:00
|
|
|
import { LegacyForms } from '@grafana/ui';
|
|
|
|
|
const { AsyncSelect } = LegacyForms;
|
2018-12-14 19:38:36 +08:00
|
|
|
|
|
|
|
|
// Utils & Services
|
2018-01-17 19:49:02 +08:00
|
|
|
import { debounce } from 'lodash';
|
2020-01-21 17:08:07 +08:00
|
|
|
import { getBackendSrv } from '@grafana/runtime';
|
2018-12-14 19:38:36 +08:00
|
|
|
|
|
|
|
|
// Types
|
2018-09-26 16:31:43 +08:00
|
|
|
import { User } from 'app/types';
|
2018-01-10 20:14:43 +08:00
|
|
|
|
2018-07-12 02:23:07 +08:00
|
|
|
export interface Props {
|
|
|
|
|
onSelected: (user: User) => void;
|
2018-01-31 23:44:14 +08:00
|
|
|
className?: string;
|
2017-12-20 23:52:43 +08:00
|
|
|
}
|
|
|
|
|
|
2018-07-12 02:23:07 +08:00
|
|
|
export interface State {
|
|
|
|
|
isLoading: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class UserPicker extends Component<Props, State> {
|
2018-01-17 23:52:18 +08:00
|
|
|
debouncedSearch: any;
|
2017-12-20 23:52:43 +08:00
|
|
|
|
2019-05-12 20:15:23 +08:00
|
|
|
constructor(props: Props) {
|
2017-12-20 23:52:43 +08:00
|
|
|
super(props);
|
2018-07-12 02:23:07 +08:00
|
|
|
this.state = { isLoading: false };
|
2018-01-17 23:52:18 +08:00
|
|
|
this.search = this.search.bind(this);
|
2018-01-18 22:49:15 +08:00
|
|
|
|
2018-01-17 23:52:18 +08:00
|
|
|
this.debouncedSearch = debounce(this.search, 300, {
|
2017-12-20 23:52:43 +08:00
|
|
|
leading: true,
|
2018-02-09 17:42:37 +08:00
|
|
|
trailing: true,
|
2017-12-20 23:52:43 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-17 23:52:18 +08:00
|
|
|
search(query?: string) {
|
2018-07-12 02:23:07 +08:00
|
|
|
this.setState({ isLoading: true });
|
|
|
|
|
|
2018-12-17 21:31:43 +08:00
|
|
|
if (_.isNil(query)) {
|
|
|
|
|
query = '';
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-21 17:08:07 +08:00
|
|
|
return getBackendSrv()
|
2019-08-13 02:03:48 +08:00
|
|
|
.get(`/api/org/users/lookup?query=${query}&limit=10`)
|
2019-05-12 20:15:23 +08:00
|
|
|
.then((result: any) => {
|
|
|
|
|
return result.map((user: any) => ({
|
2018-10-08 03:08:22 +08:00
|
|
|
id: user.userId,
|
2018-12-14 19:38:36 +08:00
|
|
|
value: user.userId,
|
2019-08-13 02:03:48 +08:00
|
|
|
label: user.login,
|
2018-12-12 05:17:32 +08:00
|
|
|
imgUrl: user.avatarUrl,
|
2018-10-08 03:08:22 +08:00
|
|
|
login: user.login,
|
|
|
|
|
}));
|
2018-07-12 02:23:07 +08:00
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
this.setState({ isLoading: false });
|
2017-12-20 23:52:43 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2018-10-08 03:08:22 +08:00
|
|
|
const { className, onSelected } = this.props;
|
2018-07-12 02:23:07 +08:00
|
|
|
const { isLoading } = this.state;
|
|
|
|
|
|
2017-12-20 23:52:43 +08:00
|
|
|
return (
|
|
|
|
|
<div className="user-picker">
|
2018-10-08 03:08:22 +08:00
|
|
|
<AsyncSelect
|
2018-12-14 19:38:36 +08:00
|
|
|
className={className}
|
2018-01-17 23:52:18 +08:00
|
|
|
isLoading={isLoading}
|
2018-10-08 03:08:22 +08:00
|
|
|
defaultOptions={true}
|
2018-01-17 23:52:18 +08:00
|
|
|
loadOptions={this.debouncedSearch}
|
2018-10-08 03:08:22 +08:00
|
|
|
onChange={onSelected}
|
2018-07-12 02:23:07 +08:00
|
|
|
placeholder="Select user"
|
2018-10-08 03:08:22 +08:00
|
|
|
noOptionsMessage={() => 'No users found'}
|
2017-12-20 23:52:43 +08:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|