Skip to content
Snippets Groups Projects
ResultTable.js 9.06 KiB
Newer Older
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableRow from '@material-ui/core/TableRow';
import Paper from '@material-ui/core/Paper';
import Typography from '@material-ui/core/Typography';
import CircularProgress from '@material-ui/core/CircularProgress';
import purple from '@material-ui/core/colors/purple';
import ResultTableHead from './ResultTableHead';
import { orderBy, has } from 'lodash';
import { parse } from 'query-string';
esikkala's avatar
esikkala committed
const styles = (theme) => ({
esikkala's avatar
esikkala committed
  tableContainer: {
esikkala's avatar
esikkala committed
    //marginTop: 72,
esikkala's avatar
esikkala committed
    overflow: 'auto',
esikkala's avatar
esikkala committed
    width: '100%',
    height: '100%'
esikkala's avatar
esikkala committed
  },
  table: {
    //marginTop: 72,
esikkala's avatar
esikkala committed
    //overflowX: 'auto',
esikkala's avatar
esikkala committed
    backgroundColor: theme.palette.background.paper
  paginationRow: {
    borderBottom: '1px solid lightgrey'
  },
  valueList: {
    paddingLeft: 15
  valueListNoBullets: {
    listStyle: 'none',
    paddingLeft: 0
  },
  withFilter: {
Esko Ikkala's avatar
Esko Ikkala committed
    minWidth: 170
  },
  infoIcon: {
    paddingTop: 15
esikkala's avatar
esikkala committed
    width: '100%',
    height: '100%',
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
  },
  progressTitle: {
    marginRight: 15
  },
});

class ResultTable extends React.Component {

  componentDidMount = () => {
    let page;
    if (this.props.routeProps.location.search === '') {
      page = this.props.page === -1 ? 0 : this.props.page;
      this.props.routeProps.history.replace({
        pathname: '/manuscripts/table',
        search: `?page=${page}`,
      });
    } else {
      page = parseInt(parse(this.props.routeProps.location.search).page);
    }
    this.props.updatePage(page);
    // console.log('mounted, fetching manuscripts')
  componentDidUpdate = prevProps => {
    if (prevProps.page != this.props.page) {
      this.props.routeProps.history.push({
        pathname: '/manuscripts/table',
        search: `?page=${this.props.page}`,
      });
    }
    if (prevProps.facetFilters != this.props.facetFilters) {
      // console.log('filters updated')
      this.props.updatePage(0);
      this.props.fetchManuscripts();
    }
esikkala's avatar
esikkala committed

  stringListRenderer = cell => {
    if (cell == null || cell === '-'){
      return '-';
    }
    if (Array.isArray(cell)) {
Esko Ikkala's avatar
Esko Ikkala committed
      cell = cell.sort();
      return (
        <ul className={this.props.classes.valueList}>
          {cell.map((item, i) => <li key={i}>{item}</li>)}
        </ul>
      );
    } else {
      return <span>{cell}</span>;
  objectListRenderer = (cell, makeLink, ordered) => {
Esko Ikkala's avatar
Esko Ikkala committed
    if (cell == null || cell === '-'){
      return '-';
    }
    else if (Array.isArray(cell)) {
      cell = orderBy(cell, 'prefLabel');
Esko Ikkala's avatar
Esko Ikkala committed
      const listItems = cell.map((item, i) =>
        <li key={i}>
          {makeLink &&
            <a
              target='_blank' rel='noopener noreferrer'
              href={item.dataProviderUrl}
Esko Ikkala's avatar
Esko Ikkala committed
            >
              {item.prefLabel}
            </a>
          }
          {!makeLink && item.prefLabel}
        </li>
Esko Ikkala's avatar
Esko Ikkala committed
      if (ordered) {
        return (
          <ol className={this.props.classes.valueList}>
Esko Ikkala's avatar
Esko Ikkala committed
            {listItems}
          </ol>
        );
      } else {
        return (
          <ul className={this.props.classes.valueList}>
Esko Ikkala's avatar
Esko Ikkala committed
            {listItems}
          </ul>
        );
      }
Esko Ikkala's avatar
Esko Ikkala committed
    } else if (makeLink) {
      return (
        <a
          target='_blank' rel='noopener noreferrer'
          href={cell.dataProviderUrl}
        >
          {cell.prefLabel}
        </a>
      );
Esko Ikkala's avatar
Esko Ikkala committed
    } else {
      return (
        <span>{cell.prefLabel}</span>
      );
  eventRenderer = cell => {
    if (cell == null || cell === '-'){
      return '-';
    }
    if (Array.isArray(cell)) {
      cell = orderBy(cell, 'date');
      const items = cell.map((item, i) => {
        return (
          <li key={i}>
            {item.date == null ? <span className={this.props.classes.noDate}>No date</span> : item.date}
            {' '}
            <a
              target='_blank' rel='noopener noreferrer'
              href={item.dataProviderUrl}
              {item.type === 'http://www.cidoc-crm.org/cidoc-crm/E8_Acquisition' ? 'Acquisition' : 'Observation'}
            </a>
          </li>
        );
      });
      return (
        <ul className={this.props.classes.valueList}>
          {items}
        </ul>
      );
    } else {
      return (
        <span>
          {cell.date == null ? <span className={this.props.classes.noDate}>No date</span> : cell.date}
          {' '}
          <a
            target='_blank' rel='noopener noreferrer'
            href={cell.dataProviderUrl}
            {cell.type === 'http://www.cidoc-crm.org/cidoc-crm/E8_Acquisition' ? 'Acquisition' : 'Observation'}
    if (cell == null || cell === '-'){
      return '-';
    }
    if (Array.isArray(cell)) {
      if (!has(cell[0], 'order')) {
        return this.objectListRenderer(cell, true, false);
      }
      cell.map(item => {
        Array.isArray(item.order) ? item.earliestOrder = item.order[0] : item.earliestOrder = item.order;
      });
      cell.sort((a, b) => a.earliestOrder - b.earliestOrder);

      const items = cell.map((item, i) => {
        return (
          <li key={i}>
            <span>{Array.isArray(item.order) ? item.order.toString() : item.order}. </span>
            <a
              target='_blank' rel='noopener noreferrer'
esikkala's avatar
esikkala committed
              href={item.dataProviderUrl}
            >
              {item.prefLabel}
            </a>
          </li>
        );
      });
      return (
        <ul className={this.props.classes.valueListNoBullets}>
          {items}
        </ul>
      );
    } else {
      if (!has(cell, 'order')) {
        return this.objectListRenderer(cell, true, false);
      }
      return (
        <span>{cell.date}<br />{cell.location}</span>
      );
    }
  };

    const { classes, rows } = this.props;
esikkala's avatar
esikkala committed
    // console.log(rows)
    if (this.props.fetchingManuscripts   ) {
esikkala's avatar
esikkala committed
      return (
        <Paper className={classes.progressContainer}>
          <Typography className={classes.progressTitle} variant="h4" color='primary'>Fetching manuscript data</Typography>
          <CircularProgress style={{ color: purple[500] }} thickness={5} />
esikkala's avatar
esikkala committed
        </Paper>
esikkala's avatar
esikkala committed
      return (
esikkala's avatar
esikkala committed
        <div className={classes.tableContainer}>
          <Table className={classes.table}>
            <ResultTableHead
              fetchManuscripts={this.props.fetchManuscripts}
              updatePage={this.props.updatePage}
              resultCount={this.props.resultCount}
esikkala's avatar
esikkala committed
              page={this.props.page}
              openFacetDialog={this.props.openFacetDialog}
esikkala's avatar
esikkala committed
            />
            <TableBody>
              {rows.map(row => {
                return (
                  <TableRow key={row.id}>
                    <TableCell className={classes.withFilter} >
                      {this.stringListRenderer(row.prefLabel)}
                    </TableCell>
                    <TableCell className={classes.withFilter}>
                      {this.objectListRenderer(row.author, true)}
                    </TableCell>
                    <TableCell className={classes.withFilter}>
                      {this.objectListRenderer(row.productionPlace, true)}
esikkala's avatar
esikkala committed
                    </TableCell>
                    <TableCell className={classes.withFilter}>
                      {this.objectListRenderer(row.timespan)}
                    </TableCell>
                    <TableCell className={classes.withFilter}>
                      {this.stringListRenderer(row.language)}
                    </TableCell>
                    {/*<TableCell className={classes.withFilter}>
esikkala's avatar
esikkala committed
                          {this.stringListRenderer(row.material)}
                        </TableCell>*/}
esikkala's avatar
esikkala committed
                    <TableCell className={classes.withFilter}>
                      {this.eventRenderer(row.event)}
esikkala's avatar
esikkala committed
                    </TableCell>
esikkala's avatar
esikkala committed
                    <TableCell className={classes.withFilter}>
esikkala's avatar
esikkala committed
                      {this.ownerRenderer(row.owner)}
                    </TableCell>
                    <TableCell className={classes.withFilter}>
                      {this.objectListRenderer(row.source, true)}
                    </TableCell>
esikkala's avatar
esikkala committed
                  </TableRow>
                );
              })}
            </TableBody>
          </Table>
        </div>
  }
}

ResultTable.propTypes = {
  classes: PropTypes.object.isRequired,
  rows: PropTypes.array.isRequired,
  facetFilters: PropTypes.object.isRequired,
  fetchManuscripts: PropTypes.func.isRequired,
  fetchingManuscripts: PropTypes.bool.isRequired,
  resultCount: PropTypes.number.isRequired,
  page: PropTypes.number.isRequired,
  updatePage: PropTypes.func.isRequired,
  routeProps: PropTypes.object.isRequired
};

export default withStyles(styles)(ResultTable);