<commit_before>const $ = window.$ = require('jquery');
import React from 'react';
import Select from 'react-select';

class DatabaseSelect extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      databaseLoading: false,
      databaseOptions: [],
      databaseId: null,
    };
  }
  componentWillMount() {
    this.fetchDatabaseOptions();
  }
  changeDb(db) {
    const val = (db) ? db.value : null;
    this.setState({ databaseId: val });
    this.props.onChange(db);
  }
  fetchDatabaseOptions() {
    this.setState({ databaseLoading: true });
    const url = '/databaseasync/api/read?_flt_0_expose_in_sqllab=1';
    $.get(url, (data) => {
      const options = data.result.map((db) => ({ value: db.id, label: db.database_name }));
      this.setState({ databaseOptions: options, databaseLoading: false });
    });
  }
  render() {
    return (
      <div>
        <Select
          name="select-db"
          placeholder={`Select a database (${this.state.databaseOptions.length})`}
          options={this.state.databaseOptions}
          value={this.state.databaseId}
          isLoading={this.state.databaseLoading}
          autosize={false}
          onChange={this.changeDb.bind(this)}
        />
      </div>
    );
  }
}

DatabaseSelect.propTypes = {
  onChange: React.PropTypes.func,
};

export default DatabaseSelect;<commit_msg>[SQLLab] Fix updating the database state. (#1225)<commit_after>const $ = window.$ = require('jquery');
import React from 'react';
import { bindActionCreators } from 'redux';
import Select from 'react-select';
import { connect } from 'react-redux';
import * as Actions from '../actions';

class DatabaseSelect extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      databaseLoading: false,
      databaseOptions: [],
      databaseId: null,
    };
  }
  componentDidMount() {
    this.fetchDatabaseOptions();
  }
  changeDb(db) {
    const val = (db) ? db.value : null;
    this.setState({ databaseId: val });
    this.props.onChange(db);
  }
  fetchDatabaseOptions() {
    this.setState({ databaseLoading: true });
    const url = '/databaseasync/api/read?_flt_0_expose_in_sqllab=1';
    $.get(url, (data) => {
      const options = data.result.map((db) => ({ value: db.id, label: db.database_name }));
      this.setState({ databaseOptions: options, databaseLoading: false });
      this.props.actions.setDatabases(data.result);
    });
  }
  render() {
    return (
      <div>
        <Select
          name="select-db"
          placeholder={`Select a database (${this.state.databaseOptions.length})`}
          options={this.state.databaseOptions}
          value={this.state.databaseId}
          isLoading={this.state.databaseLoading}
          autosize={false}
          onChange={this.changeDb.bind(this)}
        />
      </div>
    );
  }
}

DatabaseSelect.propTypes = {
  onChange: React.PropTypes.func,
  actions: React.PropTypes.object,
};

function mapDispatchToProps(dispatch) {
  return {
    actions: bindActionCreators(Actions, dispatch),
  };
}

export default connect(null, mapDispatchToProps)(DatabaseSelect);
{'fraction_non_alphanumeric': 0.14440203562340967, 'fraction_numerical': 0.002544529262086514, 'mean_word_length': 3.037227214377407, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1217468', 'n_tokens_mistral': 949, 'n_tokens_neox': 952, 'n_words': 182}