All files / javascripts/explorev2/components ExploreViewContainer.jsx

0% Statements 0/39
0% Branches 0/8
0% Functions 0/4
0% Lines 0/38
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147                                                                                                                                                                                                                                                                                                     
/* eslint camelcase: 0 */
import React from 'react';
import { bindActionCreators } from 'redux';
import * as actions from '../actions/exploreActions';
import { connect } from 'react-redux';
import ChartContainer from './ChartContainer';
import ControlPanelsContainer from './ControlPanelsContainer';
import SaveModal from './SaveModal';
import QueryAndSaveBtns from '../../explore/components/QueryAndSaveBtns';
import { autoQueryFields } from '../stores/fields';
import { getParamObject } from '../exploreUtils';
 
const $ = require('jquery');
 
const propTypes = {
  form_data: React.PropTypes.object.isRequired,
  actions: React.PropTypes.object.isRequired,
  datasource_type: React.PropTypes.string.isRequired,
};
 
 
class ExploreViewContainer extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      height: this.getHeight(),
      showModal: false,
    };
  }
 
  componentDidMount() {
    window.addEventListener('resize', this.handleResize.bind(this));
  }
 
  componentWillReceiveProps(nextProps) {
    const refreshChart = Object.keys(nextProps.form_data).some((field) => (
      nextProps.form_data[field] !== this.props.form_data[field]
      && autoQueryFields.indexOf(field) !== -1)
    );
    if (refreshChart) {
      this.onQuery(nextProps.form_data);
    }
  }
 
  componentWillUnmount() {
    window.removeEventListener('resize', this.handleResize.bind(this));
  }
 
  onQuery(form_data) {
    const data = getParamObject(form_data, this.props.datasource_type);
    this.queryFormData(data);
 
    const params = $.param(data, true);
    this.updateUrl(params);
    // remove alerts when query
    this.props.actions.removeControlPanelAlert();
    this.props.actions.removeChartAlert();
  }
 
  getHeight() {
    const navHeight = 90;
    return `${window.innerHeight - navHeight}px`;
  }
 
  updateUrl(params) {
    const baseUrl =
      `/superset/explore/${this.props.datasource_type}/${this.props.form_data.datasource}/`;
    const newEndpoint = `${baseUrl}?${params}`;
    history.pushState({}, document.title, newEndpoint);
  }
 
  queryFormData(data) {
    this.props.actions.updateExplore(
      this.props.datasource_type, this.props.form_data.datasource, data);
  }
  handleResize() {
    this.setState({ height: this.getHeight() });
  }
 
  toggleModal() {
    this.setState({ showModal: !this.state.showModal });
  }
 
  render() {
    return (
      <div
        id="explore-container"
        className="container-fluid"
        style={{
          height: this.state.height,
          overflow: 'hidden',
        }}
      >
      {this.state.showModal &&
        <SaveModal
          onHide={this.toggleModal.bind(this)}
          actions={this.props.actions}
          form_data={this.props.form_data}
          datasource_type={this.props.datasource_type}
        />
      }
        <div className="row">
          <div className="col-sm-4">
            <QueryAndSaveBtns
              canAdd="True"
              onQuery={this.onQuery.bind(this, this.props.form_data)}
              onSave={this.toggleModal.bind(this)}
            />
            <br /><br />
            <ControlPanelsContainer
              actions={this.props.actions}
              form_data={this.props.form_data}
              datasource_type={this.props.datasource_type}
              onQuery={this.onQuery.bind(this, this.props.form_data)}
            />
          </div>
          <div className="col-sm-8">
            <ChartContainer
              actions={this.props.actions}
              height={this.state.height}
              actions={this.props.actions}
            />
          </div>
        </div>
      </div>
    );
  }
}
 
ExploreViewContainer.propTypes = propTypes;
 
function mapStateToProps(state) {
  return {
    datasource_type: state.datasource_type,
    form_data: state.viz.form_data,
  };
}
 
function mapDispatchToProps(dispatch) {
  return {
    actions: bindActionCreators(actions, dispatch),
  };
}
 
export { ExploreViewContainer };
export default connect(mapStateToProps, mapDispatchToProps)(ExploreViewContainer);