<commit_before>import React, {Component, PropTypes} from 'react'
import {connect} from 'react-redux'
import RegionSelect from './RegionSelect'
import {dimensionLabelTitle} from '../../lib/labels'
import capitalize from 'lodash.capitalize'

import cx from 'classnames'

import {_t} from '../../lib/translate'

function renderRegion(region) {
  return `${region.name} ${_t(region.type)}`
}

// Compares contents of two arrays and returns true if values + indexes match
function valuesEqual(value) {
  if (!Array.isArray(value)) {
    return other => value === other
  }
  return other => {
    if (value.length !== other.length) {
      return false
    }
    return value.every((item, i) => item === other[i])
  }
}

// Todo: remove duplication
const RegionType = PropTypes.shape({
  code: PropTypes.string,
  type: PropTypes.string,
  name: PropTypes.string
})

class FilterBar extends Component {
  static propTypes = {
    querySpec: PropTypes.array,
    query: PropTypes.object,
    onChange: PropTypes.func,
    regionGroups: PropTypes.shape({
      recommended: PropTypes.arrayOf(RegionType),
      similar: PropTypes.arrayOf(RegionType),
      choices: PropTypes.arrayOf(RegionType)
    })
  }

  static defaultProps = {}

  constructor(props) {
    super()
    this.state = {
      isRegionSelectOpen: false,
      regionsValue: null
    }
  }

  renderRegionFilter() {
    const comparisonRegions = this.getSelectedComparisonRegions()
    const hasComparisonRegions = comparisonRegions.length > 0

    return (
      <button type="button"
        className={`subtle-select__button subtle-select__button--${hasComparisonRegions ? 'expanded' : 'add'}`}
        onClick={() => this.setState({isRegionSelectOpen: true})}
      >
        {hasComparisonRegions && comparisonRegions.map(renderRegion).join(', ')}
        {!hasComparisonRegions && 'Legg til sted'}
      </button>
    )
  }

  getSelectedComparisonRegions() {
    const {query, regionGroups: {choices}} = this.props
    return (query.comparisonRegions || []).map(prefixedCode => choices.find(choice => prefixedCode === choice.prefixedCode))
  }

  renderRegionSelectLightbox() {
    const {querySpec, regionGroups: {similar, recommended, choices}} = this.props

    const comparisonRegionsFilter = querySpec.find(dimension => dimension.name == 'comparisonRegions')

    const handleApplyRegionFilter = newValue => {
      this.handleFilterChange(comparisonRegionsFilter, newValue.map(region => region.prefixedCode))
      this.setState({isRegionSelectOpen: false})
    }
    const handleCancelRegionFilter = () => this.setState({isRegionSelectOpen: false})
    return (
      <li>
        <RegionSelect
          onCancel={handleCancelRegionFilter}
          onApply={handleApplyRegionFilter}
          similar={similar}
          recommended={recommended}
          choices={choices}
          value={this.getSelectedComparisonRegions()}
        />
      </li>
    )
  }

  handleFilterChange(dimension, newValue) {
    this.props.onChange(dimension.name, newValue)
  }

  getDimensionValueFromQuery(dimensionName) {
    const {query} = this.props

    if (query.hasOwnProperty(dimensionName)) {
      return query[dimensionName]
    }

    return query.dimensions.find(dim => dim.name == dimensionName).variables
  }

  renderFilter(dimension) {
    const {choices} = dimension

    const onChange = event => {
      const value = choices[event.target.value]
      this.handleFilterChange(dimension, value)
    }

    const selectContainerClassName = cx({
      select: true,
      'subtle-select__select': true,
      'subtle-select__select--disabled': !dimension.enabled
    })

    const value = this.getDimensionValueFromQuery(dimension.name)
    const index = choices.findIndex(valuesEqual(value))<commit_msg>Fix all dropdowns got disabled state<commit_after>import React, {Component, PropTypes} from 'react'
import {connect} from 'react-redux'
import RegionSelect from './RegionSelect'
import {dimensionLabelTitle} from '../../lib/labels'
import capitalize from 'lodash.capitalize'

import cx from 'classnames'

import {_t} from '../../lib/translate'

function renderRegion(region) {
  return `${region.name} ${_t(region.type)}`
}

// Compares contents of two arrays and returns true if values + indexes match
function valuesEqual(value) {
  if (!Array.isArray(value)) {
    return other => value === other
  }
  return other => {
    if (value.length !== other.length) {
      return false
    }
    return value.every((item, i) => item === other[i])
  }
}

// Todo: remove duplication
const RegionType = PropTypes.shape({
  code: PropTypes.string,
  type: PropTypes.string,
  name: PropTypes.string
})

class FilterBar extends Component {
  static propTypes = {
    querySpec: PropTypes.array,
    query: PropTypes.object,
    onChange: PropTypes.func,
    regionGroups: PropTypes.shape({
      recommended: PropTypes.arrayOf(RegionType),
      similar: PropTypes.arrayOf(RegionType),
      choices: PropTypes.arrayOf(RegionType)
    })
  }

  static defaultProps = {}

  constructor(props) {
    super()
    this.state = {
      isRegionSelectOpen: false,
      regionsValue: null
    }
  }

  renderRegionFilter() {
    const comparisonRegions = this.getSelectedComparisonRegions()
    const hasComparisonRegions = comparisonRegions.length > 0

    return (
      <button type="button"
        className={`subtle-select__button subtle-select__button--${hasComparisonRegions ? 'expanded' : 'add'}`}
        onClick={() => this.setState({isRegionSelectOpen: true})}
      >
        {hasComparisonRegions && comparisonRegions.map(renderRegion).join(', ')}
        {!hasComparisonRegions && 'Legg til sted'}
      </button>
    )
  }

  getSelectedComparisonRegions() {
    const {query, regionGroups: {choices}} = this.props
    return (query.comparisonRegions || []).map(prefixedCode => choices.find(choice => prefixedCode === choice.prefixedCode))
  }

  renderRegionSelectLightbox() {
    const {querySpec, regionGroups: {similar, recommended, choices}} = this.props

    const comparisonRegionsFilter = querySpec.find(dimension => dimension.name == 'comparisonRegions')

    const handleApplyRegionFilter = newValue => {
      this.handleFilterChange(comparisonRegionsFilter, newValue.map(region => region.prefixedCode))
      this.setState({isRegionSelectOpen: false})
    }
    const handleCancelRegionFilter = () => this.setState({isRegionSelectOpen: false})
    return (
      <li>
        <RegionSelect
          onCancel={handleCancelRegionFilter}
          onApply={handleApplyRegionFilter}
          similar={similar}
          recommended={recommended}
          choices={choices}
          value={this.getSelectedComparisonRegions()}
        />
      </li>
    )
  }

  handleFilterChange(dimension, newValue) {
    this.props.onChange(dimension.name, newValue)
  }

  getDimensionValueFromQuery(dimensionName) {
    const {query} = this.props

    if (query.hasOwnProperty(dimensionName)) {
      return query[dimensionName]
    }

    return query.dimensions.find(dim => dim.name == dimensionName).variables
  }

  renderFilter(dimension) {
    const {choices} = dimension

    const onChange = event => {
      const value = choices[event.target.value]
      this.handleFilterChange(dimension, value)
    }

    const selectContainerClassName = cx({
      select: true,
      'subtle-select__select': true,
      'subtle-select__select--disabled': dimension.locked
    })

    const value = this.getDimensionValueFromQuery(dimension.name)
    const index = choices.findIndex(valuesEqual(value))
{'fraction_non_alphanumeric': 0.11981020166073547, 'fraction_numerical': 0.00026360880453407144, 'mean_word_length': 3.505938242280285, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1539461', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2155, 'n_words': 450}