all files / src/profile/components/ TableLoader.jsx

67.65% Statements 23/34
41.67% Branches 5/12
42.86% Functions 3/7
68.75% Lines 22/32
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               19× 19×         19×               19× 19× 19×     19× 19× 19× 19× 19×                                          
import React from 'react';
import PropTypes from 'prop-types';
import { Table, Tr, Td } from 'reactable';
import $ from 'jquery';
 
import '../../../stylesheets/reactable-pagination.css';
 
const propTypes = {
  dataEndpoint: PropTypes.string.isRequired,
  mutator: PropTypes.func,
  columns: PropTypes.arrayOf(PropTypes.string),
};
 
export default class TableLoader extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      isLoading: true,
      data: [],
    };
  }
  componentWillMount() {
    $.get(this.props.dataEndpoint, (data) => {
      let actualData = data;
      if (this.props.mutator) {
        actualData = this.props.mutator(data);
      }
      this.setState({ data: actualData, isLoading: false });
    });
  }
  render() {
    const tableProps = Object.assign({}, this.props);
    let { columns } = this.props;
    if (I!columns && this.state.data.length > 0) {
      columns = Object.keys(this.state.data[0]).filter(col => col[0] !== '_');
    }
    delete tableProps.dataEndpoint;
    delete tableProps.mutator;
    delete tableProps.columns;
    if (Ethis.state.isLoading) {
      return <img alt="loading" width="25" src="/static/assets/images/loading.gif" />;
    }
    return (
      <Table {...tableProps} className="table" itemsPerPage={50} style={{ textTransform: 'capitalize' }}>
        {this.state.data.map((row, i) => (
          <Tr key={i}>
            {columns.map((col) => {
              if (row.hasOwnProperty('_' + col)) {
                return (
                  <Td key={col} column={col} value={row['_' + col]}>
                    {row[col]}
                  </Td>);
              }
              return <Td key={col} column={col}>{row[col]}</Td>;
            })}
          </Tr>
        ))}
      </Table>
    );
  }
}
TableLoader.propTypes = propTypes;