import React from 'react'; import { DropdownButton, MenuItem, Panel, Tab, Tabs } from 'react-bootstrap'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import * as Actions from '../actions'; import SqlEditor from './SqlEditor'; import shortid from 'shortid'; let queryCount = 1; class QueryEditors extends React.Component { renameTab(qe) { const newTitle = prompt('Enter a new title for the tab'); if (newTitle) { this.props.actions.queryEditorSetTitle(qe, newTitle); } } newQueryEditor() { queryCount++; const dbId = (this.props.workspaceDatabase) ? this.props.workspaceDatabase.id : null; const qe = { id: shortid.generate(), title: `Query ${queryCount}`, dbId, autorun: false, sql: 'SELECT ...', }; this.props.actions.addQueryEditor(qe); } handleSelect(key) { if (key === 'add_tab') { this.newQueryEditor(); } else { this.props.actions.setActiveQueryEditor({ id: key }); } } render() { const editors = this.props.queryEditors.map((qe, i) => { let latestQuery; this.props.queries.forEach((q) => { if (q.id === qe.latestQueryId) { latestQuery = q; } }); const state = (latestQuery) ? latestQuery.state : ''; const tabTitle = (
{qe.title} {' '} close tab rename tab
); return ( ); }); return ( {editors}  
} eventKey="add_tab" /> ); } } QueryEditors.propTypes = { actions: React.PropTypes.object, queries: React.PropTypes.array, queryEditors: React.PropTypes.array, tabHistory: React.PropTypes.array, workspaceDatabase: React.PropTypes.object, }; QueryEditors.defaultProps = { tabHistory: [], queryEditors: [], }; function mapStateToProps(state) { return { queryEditors: state.queryEditors, queries: state.queries, workspaceDatabase: state.workspaceDatabase, tabHistory: state.tabHistory, }; } function mapDispatchToProps(dispatch) { return { actions: bindActionCreators(Actions, dispatch), }; } export default connect(mapStateToProps, mapDispatchToProps)(QueryEditors);