all files / src/dashboard/components/ SliceAdder.jsx

32.31% Statements 21/65
0% Branches 0/32
0% Functions 0/13
32.81% Lines 21/64
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220                                                                                                                                                                                                                                                                                                                                                                                                              
import React from 'react';
import $ from 'jquery';
import PropTypes from 'prop-types';
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';
 
import ModalTrigger from '../../components/ModalTrigger';
import { t } from '../../locales';
 
require('react-bootstrap-table/css/react-bootstrap-table.css');
 
const propTypes = {
  dashboard: PropTypes.object.isRequired,
  triggerNode: PropTypes.node.isRequired,
  userId: PropTypes.string.isRequired,
  addSlicesToDashboard: PropTypes.func,
};
 
class SliceAdder extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      slices: [],
      slicesLoaded: false,
      selectionMap: {},
    };
 
    this.options = {
      defaultSortOrder: 'desc',
      defaultSortName: 'modified',
      sizePerPage: 10,
    };
 
    this.addSlices = this.addSlices.bind(this);
    this.toggleSlice = this.toggleSlice.bind(this);
 
    this.selectRowProp = {
      mode: 'checkbox',
      clickToSelect: true,
      onSelect: this.toggleSlice,
    };
  }
 
  componentWillUnmount() {
    if (this.slicesRequest) {
      this.slicesRequest.abort();
    }
  }
 
  onEnterModal() {
    const uri = `/sliceaddview/api/read?_flt_0_created_by=${this.props.userId}`;
    this.slicesRequest = $.ajax({
      url: uri,
      type: 'GET',
      success: (response) => {
        // Prepare slice data for table
        const slices = response.result.map(slice => ({
          id: slice.id,
          sliceName: slice.slice_name,
          vizType: slice.viz_type,
          datasourceLink: slice.datasource_link,
          modified: slice.modified,
        }));
 
        this.setState({
          slices,
          selectionMap: {},
          slicesLoaded: true,
        });
      },
      error: (error) => {
        this.errored = true;
        this.setState({
          errorMsg: t('Sorry, there was an error fetching charts to this dashboard: ') +
          this.getAjaxErrorMsg(error),
        });
      },
    });
  }
 
  getAjaxErrorMsg(error) {
    const respJSON = error.responseJSON;
    return (respJSON && respJSON.message) ? respJSON.message :
      error.responseText;
  }
 
  addSlices() {
    const adder = this;
    this.props.addSlicesToDashboard(Object.keys(this.state.selectionMap))
      // if successful, page will be reloaded.
      .fail((error) => {
        adder.errored = true;
        adder.setState({
          errorMsg: t('Sorry, there was an error adding charts to this dashboard: ') +
          this.getAjaxErrorMsg(error),
        });
      });
  }
 
  toggleSlice(slice) {
    const selectionMap = Object.assign({}, this.state.selectionMap);
    selectionMap[slice.id] = !selectionMap[slice.id];
    this.setState({ selectionMap });
  }
 
  modifiedDateComparator(a, b, order) {
    if (order === 'desc') {
      if (a.changed_on > b.changed_on) {
        return -1;
      } else if (a.changed_on < b.changed_on) {
        return 1;
      }
      return 0;
    }
 
    if (a.changed_on < b.changed_on) {
      return -1;
    } else if (a.changed_on > b.changed_on) {
      return 1;
    }
    return 0;
  }
 
  render() {
    const hideLoad = this.state.slicesLoaded || this.errored;
    let enableAddSlice = this.state.selectionMap && Object.keys(this.state.selectionMap);
    if (enableAddSlice) {
      enableAddSlice = enableAddSlice.some(function (key) {
        return this.state.selectionMap[key];
      }, this);
    }
    const modalContent = (
      <div>
        <img
          src="/static/assets/images/loading.gif"
          className={'loading ' + (hideLoad ? 'hidden' : '')}
          alt={hideLoad ? '' : 'loading'}
        />
        <div className={this.errored ? '' : 'hidden'}>
          {this.state.errorMsg}
        </div>
        <div className={this.state.slicesLoaded ? '' : 'hidden'}>
          <BootstrapTable
            ref="table"
            data={this.state.slices}
            selectRow={this.selectRowProp}
            options={this.options}
            hover
            search
            pagination
            condensed
            height="auto"
          >
            <TableHeaderColumn
              dataField="id"
              isKey
              dataSort
              hidden
            />
            <TableHeaderColumn
              dataField="sliceName"
              dataSort
            >
              {t('Name')}
            </TableHeaderColumn>
            <TableHeaderColumn
              dataField="vizType"
              dataSort
            >
              {t('Viz')}
            </TableHeaderColumn>
            <TableHeaderColumn
              dataField="datasourceLink"
              dataSort
              // Will cause react-bootstrap-table to interpret the HTML returned
              dataFormat={datasourceLink => datasourceLink}
            >
              {t('Datasource')}
            </TableHeaderColumn>
            <TableHeaderColumn
              dataField="modified"
              dataSort
              sortFunc={this.modifiedDateComparator}
              // Will cause react-bootstrap-table to interpret the HTML returned
              dataFormat={modified => modified}
            >
              {t('Modified')}
            </TableHeaderColumn>
          </BootstrapTable>
          <button
            type="button"
            className="btn btn-default"
            data-dismiss="modal"
            onClick={this.addSlices}
            disabled={!enableAddSlice}
          >
            {t('Add Charts')}
          </button>
        </div>
      </div>
    );
 
    return (
      <ModalTrigger
        triggerNode={this.props.triggerNode}
        tooltip={t('Add a new chart to the dashboard')}
        beforeOpen={this.onEnterModal.bind(this)}
        isMenuItem
        modalBody={modalContent}
        bsSize="large"
        setModalAsTriggerChildren
        modalTitle={t('Add Charts to Dashboard')}
      />
    );
  }
}
 
SliceAdder.propTypes = propTypes;
 
export default SliceAdder;