NewDataSourcePage.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import React, { PureComponent } from 'react';
  2. import { connect } from 'react-redux';
  3. import { hot } from 'react-hot-loader';
  4. import Select from 'react-select';
  5. import PageHeader from 'app/core/components/PageHeader/PageHeader';
  6. import { NavModel } from 'app/types';
  7. import { addDataSource } from './state/actions';
  8. import { getNavModel } from 'app/core/selectors/navModel';
  9. export interface Props {
  10. navModel: NavModel;
  11. addDataSource: typeof addDataSource;
  12. }
  13. export interface State {
  14. name: string;
  15. type: { value: string; label: string };
  16. dataSourceOptions: Array<{ value: string; label: string }>;
  17. }
  18. class NewDataSourcePage extends PureComponent<Props, State> {
  19. state = {
  20. name: '',
  21. type: null,
  22. dataSourceOptions: [
  23. { value: 'prometheus', label: 'Prometheus' },
  24. { value: 'graphite', label: 'Graphite' },
  25. { value: 'mysql', label: 'MySql' },
  26. { value: 'influxdb', label: 'InfluxDB' },
  27. ],
  28. };
  29. onChangeName = event => {
  30. this.setState({
  31. name: event.target.value,
  32. });
  33. };
  34. onTypeChanged = type => {
  35. this.setState({
  36. type: type,
  37. });
  38. };
  39. submitForm = () => {
  40. if (!this.isFieldsEmpty()) {
  41. // post
  42. }
  43. };
  44. isFieldsEmpty = () => {
  45. const { name, type } = this.state;
  46. if (name === '' && !type) {
  47. return true;
  48. } else if (name !== '' && !type) {
  49. return true;
  50. } else {
  51. return !!(name === '' && type);
  52. }
  53. };
  54. render() {
  55. const { navModel } = this.props;
  56. const { dataSourceOptions, name, type } = this.state;
  57. return (
  58. <div>
  59. <PageHeader model={navModel} />
  60. <div className="page-container page-body">
  61. <h3 className="page-sub-heading">New Data source</h3>
  62. <form onSubmit={this.submitForm}>
  63. <div className="gf-form max-width-30">
  64. <span className="gf-form-label width-7">Name</span>
  65. <input
  66. className="gf-form-input max-width-23"
  67. type="text"
  68. value={name}
  69. onChange={this.onChangeName}
  70. placeholder="Name"
  71. />
  72. </div>
  73. <div className="gf-form max-width-30">
  74. <span className="gf-form-label width-7">Type</span>
  75. <Select
  76. options={dataSourceOptions}
  77. value={type}
  78. onChange={this.onTypeChanged}
  79. autoSize={true}
  80. className="width-23"
  81. />
  82. </div>
  83. <div className="gf-form-button-row">
  84. <button type="submit" className="btn btn-success width-7" disabled={this.isFieldsEmpty()}>
  85. <i className="fa fa-save" />
  86. {` Create`}
  87. </button>
  88. <button className="btn btn-danger">Cancel</button>
  89. </div>
  90. </form>
  91. </div>
  92. </div>
  93. );
  94. }
  95. }
  96. function mapStateToProps(state) {
  97. return {
  98. navModel: getNavModel(state.navIndex, 'datasources'),
  99. };
  100. }
  101. const mapDispatchToProps = {
  102. addDataSource,
  103. };
  104. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(NewDataSourcePage));