DataSourceSettings.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 DataSourcePluginSettings from './DataSourcePluginSettings';
  7. import { loadDataSource, setDataSourceName } from './state/actions';
  8. import { getNavModel } from '../../core/selectors/navModel';
  9. import { getRouteParamsId } from '../../core/selectors/location';
  10. import { Label } from '../../core/components/Forms/Forms';
  11. import PageLoader from '../../core/components/PageLoader/PageLoader';
  12. import { getDataSource } from './state/selectors';
  13. export interface Props {
  14. navModel: NavModel;
  15. dataSource: DataSource;
  16. dataSourceMeta: Plugin;
  17. pageId: number;
  18. loadDataSource: typeof loadDataSource;
  19. setDataSourceName: typeof setDataSourceName;
  20. }
  21. interface State {
  22. name: string;
  23. showNamePopover: boolean;
  24. }
  25. enum DataSourceStates {
  26. Alpha = 'alpha',
  27. Beta = 'beta',
  28. }
  29. export class DataSourceSettings extends PureComponent<Props, State> {
  30. async componentDidMount() {
  31. const { loadDataSource, pageId } = this.props;
  32. await loadDataSource(pageId);
  33. }
  34. onSubmit = event => {
  35. event.preventDefault();
  36. console.log(event);
  37. };
  38. onDelete = event => {
  39. console.log(event);
  40. };
  41. isReadyOnly() {
  42. return this.props.dataSource.readOnly === true;
  43. }
  44. shouldRenderInfoBox() {
  45. const { state } = this.props.dataSourceMeta;
  46. return state === DataSourceStates.Alpha || state === DataSourceStates.Beta;
  47. }
  48. getInfoText() {
  49. const { dataSourceMeta } = this.props;
  50. switch (dataSourceMeta.state) {
  51. case DataSourceStates.Alpha:
  52. return (
  53. 'This plugin is marked as being in alpha state, which means it is in early development phase and updates' +
  54. ' will include breaking changes.'
  55. );
  56. case DataSourceStates.Beta:
  57. return (
  58. 'This plugin is marked as being in a beta development state. This means it is in currently in active' +
  59. ' development and could be missing important features.'
  60. );
  61. }
  62. return null;
  63. }
  64. render() {
  65. const { dataSource, dataSourceMeta, navModel } = this.props;
  66. return (
  67. <div>
  68. <PageHeader model={navModel} />
  69. {Object.keys(dataSource).length === 0 && Object.keys(dataSourceMeta).length === 0 ? (
  70. <PageLoader pageName="Data source settings" />
  71. ) : (
  72. <div className="page-container page-body">
  73. <div>
  74. <form onSubmit={this.onSubmit}>
  75. <div className="gf-form-group">
  76. <div className="gf-form max-width-30">
  77. <Label
  78. tooltip={
  79. 'The name is used when you select the data source in panels. The Default data source is' +
  80. 'preselected in new panels.'
  81. }
  82. >
  83. Name
  84. </Label>
  85. <input
  86. className="gf-form-input max-width-23"
  87. type="text"
  88. value={this.props.dataSource.name}
  89. placeholder="Name"
  90. onChange={event => this.props.setDataSourceName(event.target.value)}
  91. required
  92. />
  93. </div>
  94. </div>
  95. {this.shouldRenderInfoBox() && <div className="grafana-info-box">{this.getInfoText()}</div>}
  96. {this.isReadyOnly() ? (
  97. <div className="grafana-info-box span8">
  98. This datasource was added by config and cannot be modified using the UI. Please contact your server
  99. admin to update this datasource.
  100. </div>
  101. ) : (
  102. <DataSourcePluginSettings dataSource={dataSource} dataSourceMeta={dataSourceMeta} />
  103. )}
  104. <div className="gf-form-button-row">
  105. <button
  106. type="submit"
  107. className="btn btn-success"
  108. disabled={this.isReadyOnly()}
  109. onClick={this.onSubmit}
  110. >
  111. Save &amp; Test
  112. </button>
  113. <button
  114. type="submit"
  115. className="btn btn-danger"
  116. disabled={this.isReadyOnly()}
  117. onClick={this.onDelete}
  118. >
  119. Delete
  120. </button>
  121. <a className="btn btn-inverse" href="/datasources">
  122. Back
  123. </a>
  124. </div>
  125. </form>
  126. </div>
  127. </div>
  128. )}
  129. </div>
  130. );
  131. }
  132. }
  133. function mapStateToProps(state) {
  134. const pageId = getRouteParamsId(state.location);
  135. return {
  136. navModel: getNavModel(state.navIndex, `datasource-settings-${pageId}`),
  137. dataSource: getDataSource(state.dataSources, pageId),
  138. dataSourceMeta: state.dataSources.dataSourceMeta,
  139. pageId: pageId,
  140. };
  141. }
  142. const mapDispatchToProps = {
  143. loadDataSource,
  144. setDataSourceName,
  145. };
  146. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(DataSourceSettings));