NewDataSourcePage.tsx 2.9 KB

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