grafana/public/app/features/manage-dashboards/DashboardImportPage.tsx
Tobias Skarhed c11ea0b973
Form migrations: TextArea from Forms namespace (#23436)
* Move from Forms namespace and move folder

* Figrate pages from Forms

* Make Enterprise mergable
2020-04-09 09:16:10 +02:00

163 lines
5.3 KiB
TypeScript

import React, { FormEvent, PureComponent } from 'react';
import { connect, MapDispatchToProps, MapStateToProps } from 'react-redux';
import { css } from 'emotion';
import { AppEvents, NavModel } from '@grafana/data';
import { Button, Forms, stylesFactory, Input, TextArea } from '@grafana/ui';
import Page from 'app/core/components/Page/Page';
import { ImportDashboardOverview } from './components/ImportDashboardOverview';
import { DashboardFileUpload } from './components/DashboardFileUpload';
import { validateDashboardJson, validateGcomDashboard } from './utils/validation';
import { fetchGcomDashboard, importDashboardJson } from './state/actions';
import appEvents from 'app/core/app_events';
import { getNavModel } from 'app/core/selectors/navModel';
import { StoreState } from 'app/types';
interface OwnProps {}
interface ConnectedProps {
navModel: NavModel;
isLoaded: boolean;
}
interface DispatchProps {
fetchGcomDashboard: typeof fetchGcomDashboard;
importDashboardJson: typeof importDashboardJson;
}
type Props = OwnProps & ConnectedProps & DispatchProps;
class DashboardImportUnConnected extends PureComponent<Props> {
onFileUpload = (event: FormEvent<HTMLInputElement>) => {
const { importDashboardJson } = this.props;
const file = event.currentTarget.files && event.currentTarget.files.length > 0 && event.currentTarget.files[0];
if (file) {
const reader = new FileReader();
const readerOnLoad = () => {
return (e: any) => {
let dashboard: any;
try {
dashboard = JSON.parse(e.target.result);
} catch (error) {
appEvents.emit(AppEvents.alertError, [
'Import failed',
'JSON -> JS Serialization failed: ' + error.message,
]);
return;
}
importDashboardJson(dashboard);
};
};
reader.onload = readerOnLoad();
reader.readAsText(file);
}
};
getDashboardFromJson = (formData: { dashboardJson: string }) => {
this.props.importDashboardJson(JSON.parse(formData.dashboardJson));
};
getGcomDashboard = (formData: { gcomDashboard: string }) => {
let dashboardId;
const match = /(^\d+$)|dashboards\/(\d+)/.exec(formData.gcomDashboard);
if (match && match[1]) {
dashboardId = match[1];
} else if (match && match[2]) {
dashboardId = match[2];
}
if (dashboardId) {
this.props.fetchGcomDashboard(dashboardId);
}
};
renderImportForm() {
const styles = importStyles();
return (
<>
<div className={styles.option}>
<DashboardFileUpload onFileUpload={this.onFileUpload} />
</div>
<div className={styles.option}>
<Forms.Legend>Import via grafana.com</Forms.Legend>
<Forms.Form onSubmit={this.getGcomDashboard} defaultValues={{ gcomDashboard: '' }}>
{({ register, errors }) => (
<Forms.Field
invalid={!!errors.gcomDashboard}
error={errors.gcomDashboard && errors.gcomDashboard.message}
>
<Input
size="md"
name="gcomDashboard"
placeholder="Grafana.com dashboard url or id"
type="text"
ref={register({
required: 'A Grafana dashboard url or id is required',
validate: validateGcomDashboard,
})}
addonAfter={<Button type="submit">Load</Button>}
/>
</Forms.Field>
)}
</Forms.Form>
</div>
<div className={styles.option}>
<Forms.Legend>Import via panel json</Forms.Legend>
<Forms.Form onSubmit={this.getDashboardFromJson} defaultValues={{ dashboardJson: '' }}>
{({ register, errors }) => (
<>
<Forms.Field
invalid={!!errors.dashboardJson}
error={errors.dashboardJson && errors.dashboardJson.message}
>
<TextArea
name="dashboardJson"
ref={register({
required: 'Need a dashboard json model',
validate: validateDashboardJson,
})}
rows={10}
/>
</Forms.Field>
<Button type="submit">Load</Button>
</>
)}
</Forms.Form>
</div>
</>
);
}
render() {
const { isLoaded, navModel } = this.props;
return (
<Page navModel={navModel}>
<Page.Contents>{isLoaded ? <ImportDashboardOverview /> : this.renderImportForm()}</Page.Contents>
</Page>
);
}
}
const mapStateToProps: MapStateToProps<ConnectedProps, OwnProps, StoreState> = (state: StoreState) => ({
navModel: getNavModel(state.navIndex, 'import', undefined, true),
isLoaded: state.importDashboard.isLoaded,
});
const mapDispatchToProps: MapDispatchToProps<DispatchProps, Props> = {
fetchGcomDashboard,
importDashboardJson,
};
export const DashboardImportPage = connect(mapStateToProps, mapDispatchToProps)(DashboardImportUnConnected);
export default DashboardImportPage;
DashboardImportPage.displayName = 'DashboardImport';
const importStyles = stylesFactory(() => {
return {
option: css`
margin-bottom: 32px;
`,
};
});