grafana/public/app/features/manage-dashboards/DashboardImportPage.tsx

161 lines
5.2 KiB
TypeScript
Raw Normal View History

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 09:29:44 -05:00
import React, { FormEvent, PureComponent } from 'react';
import { MapDispatchToProps, MapStateToProps } from 'react-redux';
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 09:29:44 -05:00
import { css } from 'emotion';
import { AppEvents, NavModel } from '@grafana/data';
import { Button, stylesFactory, Input, TextArea, Field, Form, Legend } from '@grafana/ui';
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 09:29:44 -05:00
import Page from 'app/core/components/Page/Page';
import { connectWithCleanUp } from 'app/core/components/connectWithCleanUp';
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 09:29:44 -05:00
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}>
<Legend>Import via grafana.com</Legend>
<Form onSubmit={this.getGcomDashboard} defaultValues={{ gcomDashboard: '' }}>
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 09:29:44 -05:00
{({ register, errors }) => (
<Field invalid={!!errors.gcomDashboard} error={errors.gcomDashboard && errors.gcomDashboard.message}>
<Input
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 09:29:44 -05:00
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>}
/>
</Field>
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 09:29:44 -05:00
)}
</Form>
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 09:29:44 -05:00
</div>
<div className={styles.option}>
<Legend>Import via panel json</Legend>
<Form onSubmit={this.getDashboardFromJson} defaultValues={{ dashboardJson: '' }}>
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 09:29:44 -05:00
{({ register, errors }) => (
<>
<Field invalid={!!errors.dashboardJson} error={errors.dashboardJson && errors.dashboardJson.message}>
<TextArea
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 09:29:44 -05:00
name="dashboardJson"
ref={register({
required: 'Need a dashboard json model',
validate: validateDashboardJson,
})}
rows={10}
/>
</Field>
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 09:29:44 -05:00
<Button type="submit">Load</Button>
</>
)}
</Form>
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 09:29:44 -05:00
</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 = connectWithCleanUp(
mapStateToProps,
mapDispatchToProps,
state => state.importDashboard
)(DashboardImportUnConnected);
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 09:29:44 -05:00
export default DashboardImportPage;
DashboardImportPage.displayName = 'DashboardImport';
const importStyles = stylesFactory(() => {
return {
option: css`
margin-bottom: 32px;
`,
};
});