DataSourceSettings.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import React, { PureComponent } from 'react';
  2. import { hot } from 'react-hot-loader';
  3. import { connect } from 'react-redux';
  4. import { DataSource, DataSourceTest, 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 appEvents from '../../../core/app_events';
  11. import { deleteDataSource, loadDataSource, setDataSourceName, updateDataSource } from '../state/actions';
  12. import { getNavModel } from '../../../core/selectors/navModel';
  13. import { getRouteParamsId } from '../../../core/selectors/location';
  14. import { getDataSource, getDataSourceMeta } from '../state/selectors';
  15. export interface Props {
  16. navModel: NavModel;
  17. dataSource: DataSource;
  18. dataSourceMeta: Plugin;
  19. pageId: number;
  20. testing: DataSourceTest;
  21. deleteDataSource: typeof deleteDataSource;
  22. loadDataSource: typeof loadDataSource;
  23. setDataSourceName: typeof setDataSourceName;
  24. updateDataSource: typeof updateDataSource;
  25. }
  26. interface State {
  27. dataSource: DataSource;
  28. }
  29. enum DataSourceStates {
  30. Alpha = 'alpha',
  31. Beta = 'beta',
  32. }
  33. export class DataSourceSettings extends PureComponent<Props, State> {
  34. state = {
  35. dataSource: {} as DataSource,
  36. };
  37. async componentDidMount() {
  38. const { loadDataSource, pageId } = this.props;
  39. await loadDataSource(pageId);
  40. }
  41. onSubmit = event => {
  42. event.preventDefault();
  43. this.props.updateDataSource({ ...this.state.dataSource, name: this.props.dataSource.name });
  44. };
  45. onDelete = () => {
  46. appEvents.emit('confirm-modal', {
  47. title: 'Delete',
  48. text: 'Are you sure you want to delete this data source?',
  49. yesText: 'Delete',
  50. icon: 'fa-trash',
  51. onConfirm: () => {
  52. this.confirmDelete();
  53. },
  54. });
  55. };
  56. confirmDelete = () => {
  57. this.props.deleteDataSource();
  58. };
  59. onModelChange = dataSource => {
  60. this.setState({
  61. dataSource: dataSource,
  62. });
  63. };
  64. isReadOnly() {
  65. return this.props.dataSource.readOnly === true;
  66. }
  67. shouldRenderInfoBox() {
  68. const { state } = this.props.dataSourceMeta;
  69. return state === DataSourceStates.Alpha || state === DataSourceStates.Beta;
  70. }
  71. getInfoText() {
  72. const { dataSourceMeta } = this.props;
  73. switch (dataSourceMeta.state) {
  74. case DataSourceStates.Alpha:
  75. return (
  76. 'This plugin is marked as being in alpha state, which means it is in early development phase and updates' +
  77. ' will include breaking changes.'
  78. );
  79. case DataSourceStates.Beta:
  80. return (
  81. 'This plugin is marked as being in a beta development state. This means it is in currently in active' +
  82. ' development and could be missing important features.'
  83. );
  84. }
  85. return null;
  86. }
  87. renderIsReadOnlyMessage() {
  88. return (
  89. <div className="grafana-info-box span8">
  90. This datasource was added by config and cannot be modified using the UI. Please contact your server admin to
  91. update this datasource.
  92. </div>
  93. );
  94. }
  95. render() {
  96. const { dataSource, dataSourceMeta, navModel, testing } = this.props;
  97. return (
  98. <div>
  99. <PageHeader model={navModel} />
  100. {Object.keys(dataSource).length === 0 ? (
  101. <PageLoader pageName="Data source settings" />
  102. ) : (
  103. <div className="page-container page-body">
  104. <div>
  105. <form onSubmit={this.onSubmit}>
  106. <BasicSettings
  107. dataSourceName={this.props.dataSource.name}
  108. onChange={name => this.props.setDataSourceName(name)}
  109. />
  110. {this.shouldRenderInfoBox() && <div className="grafana-info-box">{this.getInfoText()}</div>}
  111. {this.isReadOnly() && this.renderIsReadOnlyMessage()}
  112. {dataSourceMeta.module && (
  113. <PluginSettings
  114. dataSource={dataSource}
  115. dataSourceMeta={dataSourceMeta}
  116. onModelChange={this.onModelChange}
  117. />
  118. )}
  119. <div className="gf-form-group section">
  120. {testing.inProgress && (
  121. <h5>
  122. Testing.... <i className="fa fa-spiner fa-spin" />
  123. </h5>
  124. )}
  125. {!testing.inProgress &&
  126. testing.status && (
  127. <div className={`alert-${testing.status} alert`}>
  128. <div className="alert-icon">
  129. {testing.status === 'error' ? (
  130. <i className="fa fa-exclamation-triangle" />
  131. ) : (
  132. <i className="fa fa-check" />
  133. )}
  134. </div>
  135. <div className="alert-body">
  136. <div className="alert-title">{testing.message}</div>
  137. </div>
  138. </div>
  139. )}
  140. </div>
  141. <ButtonRow
  142. onSubmit={event => this.onSubmit(event)}
  143. isReadOnly={this.isReadOnly()}
  144. onDelete={this.onDelete}
  145. />
  146. </form>
  147. </div>
  148. </div>
  149. )}
  150. </div>
  151. );
  152. }
  153. }
  154. function mapStateToProps(state) {
  155. const pageId = getRouteParamsId(state.location);
  156. const dataSource = getDataSource(state.dataSources, pageId);
  157. return {
  158. navModel: getNavModel(state.navIndex, `datasource-settings-${pageId}`),
  159. dataSource: getDataSource(state.dataSources, pageId),
  160. dataSourceMeta: getDataSourceMeta(state.dataSources, dataSource.type),
  161. pageId: pageId,
  162. testing: state.dataSources.testing,
  163. };
  164. }
  165. const mapDispatchToProps = {
  166. deleteDataSource,
  167. loadDataSource,
  168. setDataSourceName,
  169. updateDataSource,
  170. };
  171. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(DataSourceSettings));