Files
grafana/public/app/core/components/Select/FolderPicker.tsx
Peter Holmberg ec743cf9a7 Migration: Migrate Dashboard Import to React (#22338)
* first things

* introduce headers and moving buttons

* adding reducer and action for gcom dashboard

* action working

* continue building on import form

* change dashboard title

* add prop to not render a label

* first things

* introduce headers and moving buttons

* adding reducer and action for gcom dashboard

* action working

* continue building on import form

* change dashboard title

* add prop to not render a label

* import form layout

* break out form to component

* add actions and reader for file upload

* fix upload issue

* modified data types to handle both gcom and file upload

* import dashboard json

* save dashboard

* start change uid

* change dashboard uid

* fix spacing and date format

* fix import from json

* handle uid and title change

* revert change in panelinspect

* redo fileupload component

* after review

* redo forms to use Forms functionality

* first attempt on async validation

* use ternary on uid input

* removed unused actions, fixed async validation on form

* post form if invalid, break out form to component

* sync file with master

* fix after merge

* nits

* export formapi type

* redo page to use forms validation

* fix inputs and validation

* readd post

* add guards on data source and constants

* type checks and strict nulls

* strict nulls

* validate onchange and fix import button when valid

* shorten validate call

* reexport OnSubmit type

* add comment for overwrite useEffect

* move validation functions to util

* fix button imports

* remove angular import

* move title and uid validation
2020-03-31 16:29:44 +02:00

188 lines
5.4 KiB
TypeScript

import React, { PureComponent } from 'react';
import { Forms } from '@grafana/ui';
import { AppEvents, SelectableValue } from '@grafana/data';
import { debounce } from 'lodash';
import appEvents from '../../app_events';
import { getBackendSrv } from '@grafana/runtime';
import { contextSrv } from 'app/core/services/context_srv';
import { DashboardSearchHit } from '../../../types';
export interface Props {
onChange: ($folder: { title: string; id: number }) => void;
enableCreateNew?: boolean;
rootName?: string;
enableReset?: boolean;
dashboardId?: any;
initialTitle?: string;
initialFolderId?: number;
useNewForms?: boolean;
}
interface State {
folder: SelectableValue<number>;
}
export class FolderPicker extends PureComponent<Props, State> {
debouncedSearch: any;
constructor(props: Props) {
super(props);
this.state = {
folder: {},
};
this.debouncedSearch = debounce(this.getOptions, 300, {
leading: true,
trailing: true,
});
}
static defaultProps = {
rootName: 'General',
enableReset: false,
initialTitle: '',
enableCreateNew: false,
useInNextGenForms: false,
};
componentDidMount = async () => {
await this.loadInitialValue();
};
getOptions = async (query: string) => {
const { rootName, enableReset, initialTitle } = this.props;
const params = {
query,
type: 'dash-folder',
permission: 'Edit',
};
// TODO: move search to BackendSrv interface
// @ts-ignore
const searchHits = (await getBackendSrv().search(params)) as DashboardSearchHit[];
const options: Array<SelectableValue<number>> = searchHits.map(hit => ({ label: hit.title, value: hit.id }));
if (contextSrv.isEditor && rootName?.toLowerCase().startsWith(query.toLowerCase())) {
options.unshift({ label: rootName, value: 0 });
}
if (enableReset && query === '' && initialTitle !== '') {
options.unshift({ label: initialTitle, value: undefined });
}
return options;
};
onFolderChange = (newFolder: SelectableValue<number>) => {
if (!newFolder) {
newFolder = { value: 0, label: this.props.rootName };
}
this.setState(
{
folder: newFolder,
},
() => this.props.onChange({ id: newFolder.value!, title: newFolder.label! })
);
};
createNewFolder = async (folderName: string) => {
// @ts-ignore
const newFolder = await getBackendSrv().createFolder({ title: folderName });
let folder = { value: -1, label: 'Not created' };
if (newFolder.id > -1) {
appEvents.emit(AppEvents.alertSuccess, ['Folder Created', 'OK']);
folder = { value: newFolder.id, label: newFolder.title };
await this.onFolderChange(folder);
} else {
appEvents.emit(AppEvents.alertError, ['Folder could not be created']);
}
return folder;
};
private loadInitialValue = async () => {
const { initialTitle, rootName, initialFolderId, enableReset, dashboardId } = this.props;
const resetFolder: SelectableValue<number> = { label: initialTitle, value: undefined };
const rootFolder: SelectableValue<number> = { label: rootName, value: 0 };
const options = await this.getOptions('');
let folder: SelectableValue<number> = { value: -1 };
if (initialFolderId !== undefined && initialFolderId > -1) {
folder = options.find(option => option.value === initialFolderId) || { value: -1 };
} else if (enableReset && initialTitle) {
folder = resetFolder;
}
if (folder.value === -1) {
if (contextSrv.isEditor) {
folder = rootFolder;
} else {
// We shouldn't assign a random folder without the user actively choosing it on a persisted dashboard
const isPersistedDashBoard = !!dashboardId;
if (isPersistedDashBoard) {
folder = resetFolder;
} else {
folder = options.length > 0 ? options[0] : resetFolder;
}
}
}
this.setState(
{
folder,
},
() => {
// if this is not the same as our initial value notify parent
if (folder.value !== initialFolderId) {
this.props.onChange({ id: folder.value!, title: folder.text });
}
}
);
};
render() {
const { folder } = this.state;
const { enableCreateNew, useNewForms } = this.props;
return (
<>
{useNewForms && (
<Forms.AsyncSelect
loadingMessage="Loading folders..."
defaultOptions
defaultValue={folder}
value={folder}
allowCustomValue={enableCreateNew}
loadOptions={this.debouncedSearch}
onChange={this.onFolderChange}
onCreateOption={this.createNewFolder}
size="sm"
menuPosition="fixed"
/>
)}
{!useNewForms && (
<div className="gf-form-inline">
<div className="gf-form">
<label className="gf-form-label width-7">Folder</label>
<Forms.AsyncSelect
loadingMessage="Loading folders..."
defaultOptions
defaultValue={folder}
value={folder}
allowCustomValue={enableCreateNew}
loadOptions={this.debouncedSearch}
onChange={this.onFolderChange}
onCreateOption={this.createNewFolder}
size="sm"
/>
</div>
</div>
)}
</>
);
}
}