import React from 'react'; import { ButtonGroup } from 'react-bootstrap'; import Link from './Link'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import * as Actions from '../actions'; import shortid from 'shortid'; class TableElement extends React.Component { setSelectStar() { this.props.actions.queryEditorSetSql(this.props.queryEditor, this.selectStar()); } selectStar() { let cols = ''; this.props.table.columns.forEach((col, i) => { cols += col.name; if (i < this.props.table.columns.length - 1) { cols += ', '; } }); let tableName = this.props.table.name; if (this.props.table.schema) { tableName = this.props.table.schema + '.' + tableName; } return `SELECT ${cols}\nFROM ${tableName}`; } popSelectStar() { const qe = { id: shortid.generate(), title: this.props.table.name, dbId: this.props.table.dbId, autorun: true, sql: this.selectStar(), }; this.props.actions.addQueryEditor(qe); } collapseTable(e) { e.preventDefault(); this.props.actions.collapseTable.bind(this, this.props.table)(); } expandTable(e) { e.preventDefault(); this.props.actions.expandTable.bind(this, this.props.table)(); } render() { let metadata = null; let buttonToggle; if (this.props.table.expanded) { buttonToggle = ( { this.collapseTable(e); }} > {this.props.table.name} ); metadata = (
{this.props.table.columns.map((col) => (
{col.name}
{col.type}
))}
); } else { buttonToggle = ( { this.expandTable(e); }} > {this.props.table.name} ); } return (
{buttonToggle}
{metadata}
); } } TableElement.propTypes = { table: React.PropTypes.object, queryEditor: React.PropTypes.object, actions: React.PropTypes.object, }; TableElement.defaultProps = { table: null, }; function mapDispatchToProps(dispatch) { return { actions: bindActionCreators(Actions, dispatch), }; } export default connect(null, mapDispatchToProps)(TableElement);