Skip to content
Snippets Groups Projects
ChipsArray.js 3.71 KiB
Newer Older
import React from 'react'
import PropTypes from 'prop-types'
import { withStyles } from '@material-ui/core/styles'
import Chip from '@material-ui/core/Chip'
import Tooltip from '@material-ui/core/Tooltip'
import { ISOStringToYear } from './FacetHelpers'

const styles = theme => ({
  root: {
    display: 'flex',
    justifyContent: 'center',
    margin: theme.spacing(0.5)
  }
})
/**
 * A component for displaying the active facet selections an array of Material-UI's Chips.
 * Redux is used for keeping track of the selections.
 */
const ChipsArray = props => {
  const handleDelete = item => () => {
    if (!props.someFacetIsFetching && !props.fetchingResultCount) {
      switch (item.filterType) {
esikkala's avatar
esikkala committed
          props.updateFacetOption({
            facetClass: props.facetClass,
            facetID: item.facetID,
            option: item.filterType,
            value: item.value
        case 'textFilter':
esikkala's avatar
esikkala committed
          props.updateFacetOption({
            facetClass: props.facetClass,
            facetID: item.facetID,
            option: item.filterType,
            value: null
        case 'timespanFilter':
esikkala's avatar
esikkala committed
        case 'integerFilter':
esikkala's avatar
esikkala committed
          props.updateFacetOption({
            facetClass: props.facetClass,
            facetID: item.facetID,
            option: item.filterType,
            value: null
esikkala's avatar
esikkala committed
          props.fetchFacet({
            facetClass: props.facetClass,
            facetID: item.facetID
          })
  const generateLabel = (facetLabel, valueLabel, filterType) => {
    return filterType !== 'timespanFilter' &&
      filterType !== 'integerFilter' &&
      valueLabel.length > 18
esikkala's avatar
esikkala committed
      ? `${facetLabel}: ${valueLabel.substring(0, 18)}...`
      : `${facetLabel}: ${valueLabel}`
  const { classes, data } = props
  return (
    <div className={classes.root}>
      {data !== null && data.map(item => {
        const icon = null
        let key = null
        let valueLabel = null
        if (item.filterType === 'uriFilter') {
          key = item.value.node.id
          valueLabel = item.value.node.prefLabel
        }
        if (item.filterType === 'textFilter') {
          key = item.value
          valueLabel = item.value
        }
        if (item.filterType === 'timespanFilter') {
          key = item.facetID
          valueLabel = `${ISOStringToYear(item.value.start)} to
            ${ISOStringToYear(item.value.end)}`
        }
        if (item.filterType === 'integerFilter') {
          const { start, end } = item.value
          key = item.facetID
          // valueLabel = `${item.value.start} to ${item.value.end}`;
          valueLabel = `
              ${start !== '' ? start : '-'}
              to ${end !== '' ? end : '-'}`
        }
        return (
          <Tooltip key={key} title={`${item.facetLabel}: ${valueLabel}`}>
            <Chip
              key={key}
              icon={icon}
              label={generateLabel(item.facetLabel, valueLabel, item.filterType)}
              className={classes.chip}
              disabled={props.someFacetIsFetching || props.fetchingResultCount}
              onDelete={handleDelete(item)}
              color='primary'
            />
          </Tooltip>
        )
      })}
    </div>
  )
}

ChipsArray.propTypes = {
  classes: PropTypes.object.isRequired,
  data: PropTypes.array.isRequired,
  facetClass: PropTypes.string.isRequired,
  updateFacetOption: PropTypes.func.isRequired,
  someFacetIsFetching: PropTypes.bool.isRequired,
  fetchingResultCount: PropTypes.bool.isRequired,
  fetchFacet: PropTypes.func.isRequired
esikkala's avatar
esikkala committed
export const ChipsArrayComponent = ChipsArray

export default withStyles(styles)(ChipsArray)