all files / src/SqlLab/components/ App.jsx

86.27% Statements 44/51
50% Branches 6/12
66.67% Functions 6/9
86% Lines 43/50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103                                                                                                                        
import React from 'react';
import PropTypes from 'prop-types';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
 
import TabbedSqlEditors from './TabbedSqlEditors';
import QueryAutoRefresh from './QueryAutoRefresh';
import QuerySearch from './QuerySearch';
import AlertsWrapper from '../../components/AlertsWrapper';
import * as Actions from '../actions';
 
const $ = window.$ = require('jquery');
 
class App extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      hash: window.location.hash,
      contentHeight: '0px',
    };
  }
  componentDidMount() {
    /* eslint-disable react/no-did-mount-set-state */
    this.setState({ contentHeight: this.getHeight() });
    window.addEventListener('hashchange', this.onHashChanged.bind(this));
    window.addEventListener('resize', this.handleResize.bind(this));
  }
  componentWillUnmount() {
    window.removeEventListener('hashchange', this.onHashChanged.bind(this));
    window.removeEventListener('resize', this.handleResize.bind(this));
  }
  onHashChanged() {
    this.setState({ hash: window.location.hash });
  }
  getHeight() {
    const warningEl = $('#navbar-warning');
    const tabsEl = $('.nav-tabs');
    const searchHeaderEl = $('#search-header');
    const alertEl = $('#sqllab-alerts');
    const headerEl = $('header .navbar');
    const headerHeight = headerEl.outerHeight() + parseInt(headerEl.css('marginBottom'), 10);
    const searchHeaderHeight = searchHeaderEl.length > 0 ?
      searchHeaderEl.outerHeight() + parseInt(searchHeaderEl.css('marginBottom'), 10) : 0;
    const tabsHeight = tabsEl.length > 0 ? tabsEl.outerHeight() : searchHeaderHeight;
    const warningHeight = warningEl.length > 0 ? warningEl.outerHeight() : 0;
    const alertHeight = alertEl.length > 0 ? alertEl.outerHeight() : 0;
    return `${window.innerHeight - headerHeight - tabsHeight - warningHeight - alertHeight}px`;
  }
  handleResize() {
    this.setState({ contentHeight: this.getHeight() });
  }
  render() {
    let content;
    if (Ithis.state.hash) {
      content = (
        <div className="container-fluid">
          <div className="row">
            <div className="col-md-12">
              <QuerySearch height={this.state.contentHeight} actions={this.props.actions} />
            </div>
          </div>
        </div>
      );
    } else {
      content = (
        <div>
          <QueryAutoRefresh />
          <TabbedSqlEditors getHeight={this.getHeight} />
        </div>
      );
    }
    return (
      <div className="App SqlLab">
        <AlertsWrapper initMessages={this.props.initMessages} />
        <div className="container-fluid">
          {content}
        </div>
      </div>
    );
  }
}
 
App.propTypes = {
  alerts: PropTypes.array,
  actions: PropTypes.object,
  initMessages: PropTypes.array,
};
 
function mapStateToProps(state) {
  return {
    alerts: state.alerts,
    initMessages: state.flash_messages,
  };
}
function mapDispatchToProps(dispatch) {
  return {
    actions: bindActionCreators(Actions, dispatch),
  };
}
 
export { App };
export default connect(mapStateToProps, mapDispatchToProps)(App);