all files / src/explore/components/ ControlPanelsContainer.jsx

90.77% Statements 59/65
72.97% Branches 27/37
81.25% Functions 13/16
92.86% Lines 52/56
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 148 149 150 151 152 153 154 155 156 157 158                       27×   27×     27× 27×       27× 10×   17×         20× 29×                       20×       29×                                 10×   13×                                                                                                          
EI/* eslint camelcase: 0 */
import React from 'react';
import PropTypes from 'prop-types';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { Alert, Tab, Tabs } from 'react-bootstrap';
import visTypes, { sectionsToRender } from '../visTypes';
import ControlPanelSection from './ControlPanelSection';
import ControlRow from './ControlRow';
import Control from './Control';
import controls from '../controls';
import * as actions from '../actions/exploreActions';
 
const propTypes = {
  actions: PropTypes.object.isRequired,
  alert: PropTypes.string,
  datasource_type: PropTypes.string.isRequired,
  exploreState: PropTypes.object.isRequired,
  controls: PropTypes.object.isRequired,
  form_data: PropTypes.object.isRequired,
  isDatasourceMetaLoading: PropTypes.bool.isRequired,
};
 
class ControlPanelsContainer extends React.Component {
  constructor(props) {
    super(props);
    this.removeAlert = this.removeAlert.bind(this);
    this.getControlData = this.getControlData.bind(this);
    this.renderControlPanelSection = this.renderControlPanelSection.bind(this);
  }
  getControlData(controlName) {
    const control = this.props.controls[controlName];
    // Identifying mapStateToProps function to apply (logic can't be in store)
    let mapF = controls[controlName].mapStateToProps;
 
    // Looking to find mapStateToProps override for this viz type
    const controlOverrides = visTypes[this.props.controls.viz_type.value].controlOverrides || {};
    if (IcontrolOverrides[controlName] && controlOverrides[controlName].mapStateToProps) {
      mapF = controlOverrides[controlName].mapStateToProps;
    }
    // Applying mapStateToProps if needed
    if (mapF) {
      return Object.assign({}, control, mapF(this.props.exploreState, control));
    }
    return control;
  }
  sectionsToRender() {
    return sectionsToRender(this.props.form_data.viz_type, this.props.datasource_type);
  }
  removeAlert() {
    this.props.actions.removeControlPanelAlert();
  }
  renderControlPanelSection(section) {
    const ctrls = this.props.controls;
    const hasErrors = section.controlSetRows.some(rows => rows.some(s => (
        ctrls[s] &&
        ctrls[s].validationErrors &&
        (ctrls[s].validationErrors.length > 0)
    )));
    return (
      <ControlPanelSection
        key={section.label}
        label={section.label}
        startExpanded={section.expanded}
        hasErrors={hasErrors}
        description={section.description}
      >
        {section.controlSetRows.map((controlSets, i) => (
          <ControlRow
            key={`controlsetrow-${i}`}
            className="control-row"
            controls={controlSets.map(controlName => (
              controlName &&
              ctrls[controlName] &&
                <Control
                  name={controlName}
                  key={`control-${controlName}`}
                  value={this.props.form_data[controlName]}
                  validationErrors={ctrls[controlName].validationErrors}
                  actions={this.props.actions}
                  formData={ctrls[controlName].provideFormDataToProps ? this.props.form_data : null}
                  {...this.getControlData(controlName)}
                />
            ))}
          />
        ))}
      </ControlPanelSection>
    );
  }
  render() {
    const allSectionsToRender = this.sectionsToRender();
    const querySectionsToRender = [];
    const displaySectionsToRender = [];
    allSectionsToRender.forEach((section) => {
      if (section.controlSetRows.some(rows => rows.some(
        control => (
          controls[control] &&
          (
            !controls[control].renderTrigger ||
            controls[control].tabOverride === 'data'
          )
        )))) {
        querySectionsToRender.push(section);
      } else {
        displaySectionsToRender.push(section);
      }
    });
 
    return (
      <div className="scrollbar-container">
        <div className="scrollbar-content">
          {this.props.alert &&
            <Alert bsStyle="warning">
              {this.props.alert}
              <i
                className="fa fa-close pull-right"
                onClick={this.removeAlert}
                style={{ cursor: 'pointer' }}
              />
            </Alert>
          }
          <Tabs id="controlSections">
            <Tab eventKey="query" title="Data">
              {querySectionsToRender.map(this.renderControlPanelSection)}
            </Tab>
            {displaySectionsToRender.length > 0 &&
              <Tab eventKey="display" title="Style">
                {displaySectionsToRender.map(this.renderControlPanelSection)}
              </Tab>
            }
          </Tabs>
        </div>
      </div>
    );
  }
}
 
ControlPanelsContainer.propTypes = propTypes;
 
function mapStateToProps({ explore }) {
  return {
    alert: explore.controlPanelAlert,
    isDatasourceMetaLoading: explore.isDatasourceMetaLoading,
    controls: explore.controls,
    exploreState: explore,
  };
}
 
function mapDispatchToProps(dispatch) {
  return {
    actions: bindActionCreators(actions, dispatch),
  };
}
 
export { ControlPanelsContainer };
 
export default connect(mapStateToProps, mapDispatchToProps)(ControlPanelsContainer);