import React, { FormEvent, PureComponent } from 'react'; import { MapDispatchToProps, MapStateToProps } from 'react-redux'; import { css } from 'emotion'; import { AppEvents, NavModel } from '@grafana/data'; import { Button, stylesFactory, Input, TextArea, Field, Form, Legend, FileUpload } from '@grafana/ui'; import Page from 'app/core/components/Page/Page'; import { connectWithCleanUp } from 'app/core/components/connectWithCleanUp'; import { ImportDashboardOverview } from './components/ImportDashboardOverview'; 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 { onFileUpload = (event: FormEvent) => { 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 ( <>
Upload JSON file
Import via grafana.com
{({ register, errors }) => ( Load} /> )}
Import via panel json
{({ register, errors }) => ( <>