DataSourceDashboards.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import { hot } from 'react-hot-loader';
  4. import { connect } from 'react-redux';
  5. // Components
  6. import PageHeader from 'app/core/components/PageHeader/PageHeader';
  7. import DashboardTable from './DashboardsTable';
  8. // Actions & Selectors
  9. import { getNavModel } from 'app/core/selectors/navModel';
  10. import { getRouteParamsId } from 'app/core/selectors/location';
  11. import { loadDataSource } from './state/actions';
  12. import { loadPluginDashboards } from '../plugins/state/actions';
  13. import { importDashboard, removeDashboard } from '../dashboard/state/actions';
  14. import { getDataSource } from './state/selectors';
  15. // Types
  16. import { NavModel, PluginDashboard } from 'app/types';
  17. import { DataSourceSettings } from '@grafana/ui/src/types';
  18. export interface Props {
  19. navModel: NavModel;
  20. dashboards: PluginDashboard[];
  21. dataSource: DataSourceSettings;
  22. pageId: number;
  23. importDashboard: typeof importDashboard;
  24. loadDataSource: typeof loadDataSource;
  25. loadPluginDashboards: typeof loadPluginDashboards;
  26. removeDashboard: typeof removeDashboard;
  27. }
  28. export class DataSourceDashboards extends PureComponent<Props> {
  29. async componentDidMount() {
  30. const { loadDataSource, pageId } = this.props;
  31. await loadDataSource(pageId);
  32. this.props.loadPluginDashboards();
  33. }
  34. onImport = (dashboard: PluginDashboard, overwrite: boolean) => {
  35. const { dataSource, importDashboard } = this.props;
  36. const data = {
  37. pluginId: dashboard.pluginId,
  38. path: dashboard.path,
  39. overwrite: overwrite,
  40. inputs: [],
  41. };
  42. if (dataSource) {
  43. data.inputs.push({
  44. name: '*',
  45. type: 'datasource',
  46. pluginId: dataSource.type,
  47. value: dataSource.name,
  48. });
  49. }
  50. importDashboard(data, dashboard.title);
  51. };
  52. onRemove = (dashboard: PluginDashboard) => {
  53. this.props.removeDashboard(dashboard.importedUri);
  54. };
  55. render() {
  56. const { dashboards, navModel } = this.props;
  57. return (
  58. <div>
  59. <PageHeader model={navModel} />
  60. <div className="page-container page-body">
  61. <DashboardTable
  62. dashboards={dashboards}
  63. onImport={(dashboard, overwrite) => this.onImport(dashboard, overwrite)}
  64. onRemove={dashboard => this.onRemove(dashboard)}
  65. />
  66. </div>
  67. </div>
  68. );
  69. }
  70. }
  71. function mapStateToProps(state) {
  72. const pageId = getRouteParamsId(state.location);
  73. return {
  74. navModel: getNavModel(state.navIndex, `datasource-dashboards-${pageId}`),
  75. pageId: pageId,
  76. dashboards: state.plugins.dashboards,
  77. dataSource: getDataSource(state.dataSources, pageId),
  78. };
  79. }
  80. const mapDispatchToProps = {
  81. importDashboard,
  82. loadDataSource,
  83. loadPluginDashboards,
  84. removeDashboard,
  85. };
  86. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(DataSourceDashboards));