grafana/public/app/features/datasources/DataSourceDashboards.tsx

94 lines
2.7 KiB
TypeScript
Raw Normal View History

2018-10-17 07:36:18 -05:00
import React, { PureComponent } from 'react';
import { hot } from 'react-hot-loader';
import { connect } from 'react-redux';
import PageHeader from 'app/core/components/PageHeader/PageHeader';
import DashboardTable from './DashboardsTable';
2018-10-17 08:56:34 -05:00
import { DataSource, NavModel, PluginDashboard } from 'app/types';
2018-10-17 07:36:18 -05:00
import { getNavModel } from 'app/core/selectors/navModel';
import { getRouteParamsId } from 'app/core/selectors/location';
import { loadDataSource } from './state/actions';
import { loadPluginDashboards } from '../plugins/state/actions';
2018-10-17 08:56:34 -05:00
import { importDashboard, removeDashboard } from '../dashboard/state/actions';
import { getDataSource } from './state/selectors';
2018-10-17 07:36:18 -05:00
export interface Props {
navModel: NavModel;
dashboards: PluginDashboard[];
2018-10-17 08:56:34 -05:00
dataSource: DataSource;
2018-10-17 07:36:18 -05:00
pageId: number;
2018-10-17 08:56:34 -05:00
importDashboard: typeof importDashboard;
2018-10-17 07:36:18 -05:00
loadDataSource: typeof loadDataSource;
loadPluginDashboards: typeof loadPluginDashboards;
2018-10-17 08:56:34 -05:00
removeDashboard: typeof removeDashboard;
2018-10-17 07:36:18 -05:00
}
export class DataSourceDashboards extends PureComponent<Props> {
async componentDidMount() {
const { loadDataSource, pageId } = this.props;
await loadDataSource(pageId);
this.props.loadPluginDashboards();
}
2018-10-17 08:56:34 -05:00
onImport = (dashboard: PluginDashboard, overwrite: boolean) => {
const { dataSource, importDashboard } = this.props;
const data = {
pluginId: dashboard.pluginId,
path: dashboard.path,
overwrite: overwrite,
inputs: [],
};
2018-10-17 07:36:18 -05:00
2018-10-17 08:56:34 -05:00
if (dataSource) {
data.inputs.push({
name: '*',
type: 'datasource',
pluginId: dataSource.type,
value: dataSource.name,
});
}
importDashboard(data, dashboard.title);
};
onRemove = (dashboard: PluginDashboard) => {
this.props.removeDashboard(dashboard.importedUri);
};
2018-10-17 07:36:18 -05:00
render() {
const { dashboards, navModel } = this.props;
return (
<div>
<PageHeader model={navModel} />
<div className="page-container page-body">
<DashboardTable
dashboards={dashboards}
onImport={(dashboard, overwrite) => this.onImport(dashboard, overwrite)}
onRemove={dashboard => this.onRemove(dashboard)}
/>
</div>
</div>
);
}
}
function mapStateToProps(state) {
const pageId = getRouteParamsId(state.location);
return {
navModel: getNavModel(state.navIndex, `datasource-dashboards-${pageId}`),
pageId: pageId,
dashboards: state.plugins.dashboards,
2018-10-17 08:56:34 -05:00
dataSource: getDataSource(state.dataSources, pageId),
2018-10-17 07:36:18 -05:00
};
}
const mapDispatchToProps = {
2018-10-17 08:56:34 -05:00
importDashboard,
2018-10-17 07:36:18 -05:00
loadDataSource,
loadPluginDashboards,
2018-10-17 08:56:34 -05:00
removeDashboard,
2018-10-17 07:36:18 -05:00
};
export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(DataSourceDashboards));