DataSourceSettings.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import React, { PureComponent } from 'react';
  2. import { hot } from 'react-hot-loader';
  3. import { connect } from 'react-redux';
  4. import { DataSource, NavModel, Plugin } from 'app/types/';
  5. import PageHeader from '../../../core/components/PageHeader/PageHeader';
  6. import PageLoader from '../../../core/components/PageLoader/PageLoader';
  7. import PluginSettings from './PluginSettings';
  8. import BasicSettings from './BasicSettings';
  9. import ButtonRow from './ButtonRow';
  10. import { loadDataSource, setDataSourceName } from '../state/actions';
  11. import { getNavModel } from '../../../core/selectors/navModel';
  12. import { getRouteParamsId } from '../../../core/selectors/location';
  13. import { getDataSource, getDataSourceMeta } from '../state/selectors';
  14. export interface Props {
  15. navModel: NavModel;
  16. dataSource: DataSource;
  17. dataSourceMeta: Plugin;
  18. pageId: number;
  19. loadDataSource: typeof loadDataSource;
  20. setDataSourceName: typeof setDataSourceName;
  21. }
  22. interface State {
  23. name: string;
  24. showNamePopover: boolean;
  25. }
  26. enum DataSourceStates {
  27. Alpha = 'alpha',
  28. Beta = 'beta',
  29. }
  30. export class DataSourceSettings extends PureComponent<Props, State> {
  31. async componentDidMount() {
  32. const { loadDataSource, pageId } = this.props;
  33. await loadDataSource(pageId);
  34. }
  35. onSubmit = event => {
  36. event.preventDefault();
  37. console.log(event);
  38. };
  39. onDelete = event => {
  40. console.log(event);
  41. };
  42. isReadOnly() {
  43. return this.props.dataSource.readOnly === true;
  44. }
  45. shouldRenderInfoBox() {
  46. const { state } = this.props.dataSourceMeta;
  47. return state === DataSourceStates.Alpha || state === DataSourceStates.Beta;
  48. }
  49. getInfoText() {
  50. const { dataSourceMeta } = this.props;
  51. switch (dataSourceMeta.state) {
  52. case DataSourceStates.Alpha:
  53. return (
  54. 'This plugin is marked as being in alpha state, which means it is in early development phase and updates' +
  55. ' will include breaking changes.'
  56. );
  57. case DataSourceStates.Beta:
  58. return (
  59. 'This plugin is marked as being in a beta development state. This means it is in currently in active' +
  60. ' development and could be missing important features.'
  61. );
  62. }
  63. return null;
  64. }
  65. render() {
  66. const { dataSource, dataSourceMeta, navModel } = this.props;
  67. return (
  68. <div>
  69. <PageHeader model={navModel} />
  70. {Object.keys(dataSource).length === 0 && Object.keys(dataSourceMeta).length === 0 ? (
  71. <PageLoader pageName="Data source settings" />
  72. ) : (
  73. <div className="page-container page-body">
  74. <div>
  75. <form onSubmit={this.onSubmit}>
  76. <BasicSettings
  77. dataSourceName={this.props.dataSource.name}
  78. onChange={name => this.props.setDataSourceName(name)}
  79. />
  80. {this.shouldRenderInfoBox() && <div className="grafana-info-box">{this.getInfoText()}</div>}
  81. {this.isReadOnly() ? (
  82. <div className="grafana-info-box span8">
  83. This datasource was added by config and cannot be modified using the UI. Please contact your server
  84. admin to update this datasource.
  85. </div>
  86. ) : (
  87. dataSourceMeta.module && <PluginSettings dataSource={dataSource} dataSourceMeta={dataSourceMeta} />
  88. )}
  89. <ButtonRow
  90. onSubmit={event => this.onSubmit(event)}
  91. isReadOnly={this.isReadOnly()}
  92. onDelete={event => this.onDelete(event)}
  93. />
  94. </form>
  95. </div>
  96. </div>
  97. )}
  98. </div>
  99. );
  100. }
  101. }
  102. function mapStateToProps(state) {
  103. const pageId = getRouteParamsId(state.location);
  104. const dataSource = getDataSource(state.dataSources, pageId);
  105. return {
  106. navModel: getNavModel(state.navIndex, `datasource-settings-${pageId}`),
  107. dataSource: getDataSource(state.dataSources, pageId),
  108. dataSourceMeta: getDataSourceMeta(state.dataSources, dataSource.type),
  109. pageId: pageId,
  110. };
  111. }
  112. const mapDispatchToProps = {
  113. loadDataSource,
  114. setDataSourceName,
  115. };
  116. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(DataSourceSettings));