DataSourceSettings.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. renderIsReadOnlyMessage() {
  66. return (
  67. <div className="grafana-info-box span8">
  68. This datasource was added by config and cannot be modified using the UI. Please contact your server admin to
  69. update this datasource.
  70. </div>
  71. );
  72. }
  73. render() {
  74. const { dataSource, dataSourceMeta, navModel } = this.props;
  75. return (
  76. <div>
  77. <PageHeader model={navModel} />
  78. {Object.keys(dataSource).length === 0 ? (
  79. <PageLoader pageName="Data source settings" />
  80. ) : (
  81. <div className="page-container page-body">
  82. <div>
  83. <form onSubmit={this.onSubmit}>
  84. <BasicSettings
  85. dataSourceName={this.props.dataSource.name}
  86. onChange={name => this.props.setDataSourceName(name)}
  87. />
  88. {this.shouldRenderInfoBox() && <div className="grafana-info-box">{this.getInfoText()}</div>}
  89. {this.isReadOnly()
  90. ? this.renderIsReadOnlyMessage()
  91. : dataSourceMeta.module && <PluginSettings dataSource={dataSource} dataSourceMeta={dataSourceMeta} />}
  92. <ButtonRow
  93. onSubmit={event => this.onSubmit(event)}
  94. isReadOnly={this.isReadOnly()}
  95. onDelete={event => this.onDelete(event)}
  96. />
  97. </form>
  98. </div>
  99. </div>
  100. )}
  101. </div>
  102. );
  103. }
  104. }
  105. function mapStateToProps(state) {
  106. const pageId = getRouteParamsId(state.location);
  107. const dataSource = getDataSource(state.dataSources, pageId);
  108. return {
  109. navModel: getNavModel(state.navIndex, `datasource-settings-${pageId}`),
  110. dataSource: getDataSource(state.dataSources, pageId),
  111. dataSourceMeta: getDataSourceMeta(state.dataSources, dataSource.type),
  112. pageId: pageId,
  113. };
  114. }
  115. const mapDispatchToProps = {
  116. loadDataSource,
  117. setDataSourceName,
  118. };
  119. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(DataSourceSettings));