DataSourceSettings.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import React, { createRef, PureComponent } from 'react';
  2. import { connect } from 'react-redux';
  3. import { DataSource, Plugin } from 'app/types';
  4. export interface Props {
  5. dataSource: DataSource;
  6. dataSourceMeta: Plugin;
  7. }
  8. interface State {
  9. name: string;
  10. showNamePopover: boolean;
  11. }
  12. enum DataSourceStates {
  13. Alpha = 'alpha',
  14. Beta = 'beta',
  15. }
  16. export class DataSourceSettings extends PureComponent<Props, State> {
  17. settingsElement = createRef<HTMLDivElement>();
  18. state = {
  19. name: this.props.dataSource.name,
  20. showNamePopover: false,
  21. };
  22. componentDidMount() {
  23. // importPluginModule(this.props.dataSourceMeta.module).then(pluginExports => {
  24. // console.log(pluginExports);
  25. // });
  26. }
  27. onNameChange = event => {
  28. this.setState({
  29. name: event.target.value,
  30. });
  31. };
  32. onSubmit = event => {
  33. event.preventDefault();
  34. console.log(event);
  35. };
  36. onDelete = event => {
  37. console.log(event);
  38. };
  39. onTogglePopover = () => {
  40. this.setState(prevState => ({
  41. showNamePopover: !prevState.showNamePopover,
  42. }));
  43. };
  44. isReadyOnly() {
  45. return this.props.dataSource.readOnly === true;
  46. }
  47. shouldRenderInfoBox() {
  48. const { state } = this.props.dataSourceMeta;
  49. return state === DataSourceStates.Alpha || state === DataSourceStates.Beta;
  50. }
  51. getInfoText() {
  52. const { dataSourceMeta } = this.props;
  53. switch (dataSourceMeta.state) {
  54. case DataSourceStates.Alpha:
  55. return (
  56. 'This plugin is marked as being in alpha state, which means it is in early development phase and updates' +
  57. ' will include breaking changes.'
  58. );
  59. case DataSourceStates.Beta:
  60. return (
  61. 'This plugin is marked as being in a beta development state. This means it is in currently in active' +
  62. ' development and could be missing important features.'
  63. );
  64. }
  65. return null;
  66. }
  67. render() {
  68. const { name, showNamePopover } = this.state;
  69. return (
  70. <div>
  71. <form onSubmit={this.onSubmit}>
  72. <div className="gf-form-group">
  73. <div className="gf-form-inline">
  74. <div className="gf-form max-width-30">
  75. <span className="gf-form-label width-10">Name</span>
  76. <input
  77. className="gf-form-input max-width-23"
  78. type="text"
  79. value={name}
  80. placeholder="Name"
  81. onChange={this.onNameChange}
  82. required
  83. />
  84. <div onClick={this.onTogglePopover}>
  85. <i className="fa fa-info-circle" />
  86. </div>
  87. {showNamePopover && (
  88. <div
  89. style={{
  90. position: 'absolute',
  91. left: '450px',
  92. top: '-20px',
  93. padding: '10px',
  94. backgroundColor: 'black',
  95. zIndex: 2,
  96. width: '300px',
  97. border: '1px solid gray',
  98. borderRadius: '3px',
  99. }}
  100. >
  101. The name is used when you select the data source in panels. The <em>Default</em> data source is
  102. preselected in new panels.
  103. </div>
  104. )}
  105. </div>
  106. </div>
  107. </div>
  108. {this.shouldRenderInfoBox() && <div className="grafana-info-box">{this.getInfoText()}</div>}
  109. {this.isReadyOnly() && (
  110. <div className="grafana-info-box span8">
  111. This datasource was added by config and cannot be modified using the UI. Please contact your server admin
  112. to update this datasource.
  113. </div>
  114. )}
  115. <div ref={this.settingsElement} />
  116. <div className="gf-form-button-row">
  117. <button type="submit" className="btn btn-success" disabled={this.isReadyOnly()} onClick={this.onSubmit}>
  118. Save &amp; Test
  119. </button>
  120. <button type="submit" className="btn btn-danger" disabled={this.isReadyOnly()} onClick={this.onDelete}>
  121. Delete
  122. </button>
  123. <a className="btn btn-inverse" href="datasources">
  124. Back
  125. </a>
  126. </div>
  127. </form>
  128. </div>
  129. );
  130. }
  131. }
  132. function mapStateToProps(state) {
  133. return {
  134. dataSource: state.dataSources.dataSource,
  135. dataSourceMeta: state.dataSources.dataSourceMeta,
  136. };
  137. }
  138. export default connect(mapStateToProps)(DataSourceSettings);