const $ = window.$ = require('jquery'); import React from 'react'; import Select from 'react-select'; import { Button } from 'react-bootstrap'; import TableElement from './TableElement'; import AsyncSelect from '../../components/AsyncSelect'; const propTypes = { queryEditor: React.PropTypes.object.isRequired, tables: React.PropTypes.array, actions: React.PropTypes.object, }; const defaultProps = { tables: [], actions: {}, }; class SqlEditorLeftBar extends React.PureComponent { constructor(props) { super(props); this.state = { schemaLoading: false, schemaOptions: [], tableLoading: false, tableOptions: [], }; } componentWillMount() { this.fetchSchemas(this.props.queryEditor.dbId); this.fetchTables(this.props.queryEditor.dbId, this.props.queryEditor.schema); } onChange(db) { const val = (db) ? db.value : null; this.setState({ schemaOptions: [] }); this.props.actions.queryEditorSetDb(this.props.queryEditor, val); if (!(db)) { this.setState({ tableOptions: [] }); } else { this.fetchTables(val, this.props.queryEditor.schema); this.fetchSchemas(val); } } dbMutator(data) { const options = data.result.map((db) => ({ value: db.id, label: db.database_name })); this.props.actions.setDatabases(data.result); if (data.result.length === 0) { this.props.actions.addAlert({ bsStyle: 'danger', msg: "It seems you don't have access to any database", }); } return options; } resetState() { this.props.actions.resetState(); } getTableNamesBySubStr(input) { if (!this.props.queryEditor.dbId || !input) { return Promise.resolve({ options: [] }); } const url = `/superset/tables/${this.props.queryEditor.dbId}/\ ${this.props.queryEditor.schema}/${input}`; return $.get(url).then((data) => ({ options: data.options })); } // TODO: move fetching methods to the actions. fetchTables(dbId, schema, substr) { if (dbId) { this.setState({ tableLoading: true, tableOptions: [] }); const url = `/superset/tables/${dbId}/${schema}/${substr}/`; $.get(url, (data) => { this.setState({ tableLoading: false, tableOptions: data.options, tableLength: data.tableLength, }); }); } } changeTable(tableOpt) { if (!tableOpt) { this.setState({ tableName: '' }); return; } const namePieces = tableOpt.value.split('.'); let tableName = namePieces[0]; let schemaName = this.props.queryEditor.schema; if (namePieces.length === 1) { this.setState({ tableName }); } else { schemaName = namePieces[0]; tableName = namePieces[1]; this.setState({ tableName }); this.props.actions.queryEditorSetSchema(this.props.queryEditor, schemaName); this.fetchTables(this.props.queryEditor.dbId, schemaName); } this.setState({ tableLoading: true }); // TODO: handle setting the tableLoading state depending on success or // failure of the addTable async call in the action. this.props.actions.addTable(this.props.queryEditor, tableName, schemaName); this.setState({ tableLoading: false }); } changeSchema(schemaOpt) { const schema = (schemaOpt) ? schemaOpt.value : null; this.props.actions.queryEditorSetSchema(this.props.queryEditor, schema); this.fetchTables(this.props.queryEditor.dbId, schema); } fetchSchemas(dbId) { const actualDbId = dbId || this.props.queryEditor.dbId; if (actualDbId) { this.setState({ schemaLoading: true }); const url = `/superset/schemas/${actualDbId}/`; $.get(url, (data) => { const schemaOptions = data.schemas.map((s) => ({ value: s, label: s })); this.setState({ schemaOptions }); this.setState({ schemaLoading: false }); }); } } closePopover(ref) { this.refs[ref].hide(); } render() { const shouldShowReset = window.location.search === '?reset=1'; return (
(
Database: {o.label}
)} mutator={this.dbMutator.bind(this)} placeholder="Select a database" />
} {!this.props.queryEditor.schema && }

{this.props.tables.map((table) => ( ))}
{shouldShowReset && }
); } } SqlEditorLeftBar.propTypes = propTypes; SqlEditorLeftBar.defaultProps = defaultProps; export default SqlEditorLeftBar;