NewDataSourcePage.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 { DataSourceType, NavModel } from 'app/types';
  7. import { addDataSource, loadDataSourceTypes } from './state/actions';
  8. import { updateLocation } from '../../core/actions';
  9. import { getNavModel } from 'app/core/selectors/navModel';
  10. export interface Props {
  11. navModel: NavModel;
  12. dataSourceTypes: DataSourceType[];
  13. addDataSource: typeof addDataSource;
  14. loadDataSourceTypes: typeof loadDataSourceTypes;
  15. updateLocation: typeof updateLocation;
  16. }
  17. export interface State {
  18. name: string;
  19. type: { value: string; label: string };
  20. }
  21. class NewDataSourcePage extends PureComponent<Props, State> {
  22. state = {
  23. name: '',
  24. type: null,
  25. };
  26. componentDidMount() {
  27. this.props.loadDataSourceTypes();
  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 = event => {
  40. event.preventDefault();
  41. if (!this.isFieldsEmpty()) {
  42. this.props.addDataSource(this.state.name, this.state.type.value);
  43. }
  44. };
  45. goBack = () => {
  46. this.props.updateLocation({ path: '/datasources' });
  47. };
  48. isFieldsEmpty = () => {
  49. const { name, type } = this.state;
  50. if (name === '' && !type) {
  51. return true;
  52. } else if (name !== '' && !type) {
  53. return true;
  54. } else {
  55. return !!(name === '' && type);
  56. }
  57. };
  58. render() {
  59. const { navModel, dataSourceTypes } = this.props;
  60. const { name, type } = this.state;
  61. return (
  62. <div>
  63. <PageHeader model={navModel} />
  64. <div className="page-container page-body">
  65. <h3 className="page-sub-heading">New Data source</h3>
  66. <form onSubmit={this.submitForm}>
  67. <div className="gf-form max-width-30">
  68. <span className="gf-form-label width-7">Name</span>
  69. <input
  70. className="gf-form-input max-width-23"
  71. type="text"
  72. value={name}
  73. onChange={this.onChangeName}
  74. placeholder="Name"
  75. />
  76. </div>
  77. <div className="gf-form max-width-30">
  78. <span className="gf-form-label width-7">Type</span>
  79. <Select
  80. valueKey="type"
  81. labelKey="name"
  82. options={dataSourceTypes}
  83. value={type}
  84. onChange={this.onTypeChanged}
  85. autoSize={true}
  86. className="width-23"
  87. />
  88. </div>
  89. <div className="gf-form-button-row">
  90. <button type="submit" className="btn btn-success width-7" disabled={this.isFieldsEmpty()}>
  91. <i className="fa fa-save" />
  92. {` Create`}
  93. </button>
  94. <button className="btn btn-danger" onClick={this.goBack}>
  95. Cancel
  96. </button>
  97. </div>
  98. </form>
  99. </div>
  100. </div>
  101. );
  102. }
  103. }
  104. function mapStateToProps(state) {
  105. return {
  106. navModel: getNavModel(state.navIndex, 'datasources'),
  107. dataSourceTypes: state.dataSources.dataSourceTypes,
  108. };
  109. }
  110. const mapDispatchToProps = {
  111. addDataSource,
  112. loadDataSourceTypes,
  113. updateLocation,
  114. };
  115. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(NewDataSourcePage));