<commit_before>import { connect } from 'react-redux'
import toPairs from 'lodash/toPairs'
import values from 'lodash/values'
import keys from 'lodash/keys'
import settings from '../../../lib/settings'
import { pluralize } from '../../../lib/inflection'
import loadAjaxData from '../../../Backend/actions/loadAjaxData'
import addEntities from '../../../Backend/actions/addEntities'
import OverviewTable from '../components/OverviewTable'

const ALL = 'all'

const mapStateToProps = (state, ownProps) => {
  const stateKey = `statisticsOverview_${ownProps.model}`
  const states = (state.ajax[stateKey] && state.ajax[stateKey].states) || []
  const selectedCity = state.rform[stateKey] && state.rform[stateKey].city
  const data =
    (state.entities.count && state.entities.count[ownProps.model] &&
      state.entities.count[ownProps.model][selectedCity || ALL]) || {}
  const sections =
    values(state.entities.filters).filter(obj => obj.type == 'SectionFilter')
  const loadedCities =
    (state.entities.count && keys(state.entities.count[ownProps.model])) || []

  return {
    data,
    states,
    stateKey,
    sections,
    selectedCity,
    loadedCities,
  }
}

const mapDispatchToProps = (dispatch, ownProps) => ({
  dispatch
})

const mergeProps = (stateProps, dispatchProps, ownProps) => {
  const { model, cityAssociationName } = ownProps
  const { dispatch } = dispatchProps

  const entryCountGrabberTransformer = function(aasm_state, section, cityId) {
    return function(json) {
      const sectionKey = section.identifier || section
      const stateKey = aasm_state || 'total'
      const cityKey = cityId || ALL

      let obj = {}
      obj[model] = {}
      obj[model][cityKey] = {}
      obj[model][cityKey][stateKey] = {}
      obj[model][cityKey][stateKey][sectionKey] = json.meta.total_entries
      return { count: obj }
    }
  }

  const entryCountGrabberParams = function(aasm_state, section, cityId) {
    let params = { per_page: 1 }
    if (cityId) params[`filters[${cityAssociationName}.id]`] = cityId
    if (aasm_state)
      params[`filters[${pluralize(model)}.aasm_state]`] = aasm_state
    if (typeof section == 'object')
      params['filters[section_filters.id]'] = section.id
    return params
  }

  const dispatchDataLoad = function(aasm_state, section, cityId) {
    dispatch(
      loadAjaxData(
        model + 's',
        entryCountGrabberParams(aasm_state, section, cityId),
        'lastData',
        entryCountGrabberTransformer(aasm_state, section, cityId)
      )
    )
  }

  const loadData = function(states, cityId) {
    for (let aasm_state of states.concat(null)) { // null for the "total" row
      for (let section of stateProps.sections) {
        dispatchDataLoad(aasm_state, section, cityId)
      }
      dispatchDataLoad(aasm_state, 'total', cityId)
    }
  }

  return({
    ...stateProps,
    ...dispatchProps,
    ...ownProps,

    loadData,

    loadStates() {
      dispatch(
        loadAjaxData(
          `states/${model}`, {}, stateProps.stateKey, () => ({})
        )
      )
    },

    onCityChange(selected) {
      const city = (selected && selected.value) || ALL
      if (!stateProps.loadedCities.includes(city))
        loadData(stateProps.states, selected.value)
    }
  })
}

export default connect(mapStateToProps, mapDispatchToProps, mergeProps)(
  OverviewTable
)
<commit_msg>Hotfix for clarat-org/clarat#1067 - changed state access in code (now in .data.attributes)<commit_after>import { connect } from 'react-redux'
import toPairs from 'lodash/toPairs'
import values from 'lodash/values'
import keys from 'lodash/keys'
import settings from '../../../lib/settings'
import { pluralize } from '../../../lib/inflection'
import loadAjaxData from '../../../Backend/actions/loadAjaxData'
import addEntities from '../../../Backend/actions/addEntities'
import OverviewTable from '../components/OverviewTable'

const ALL = 'all'

const mapStateToProps = (state, ownProps) => {
  const stateKey = `statisticsOverview_${ownProps.model}`
  const states =
    (state.ajax[stateKey] && state.ajax[stateKey].data.attributes.states) || []
  const selectedCity = state.rform[stateKey] && state.rform[stateKey].city
  const data =
    (state.entities.count && state.entities.count[ownProps.model] &&
      state.entities.count[ownProps.model][selectedCity || ALL]) || {}
  const sections =
    values(state.entities.filters).filter(obj => obj.type == 'SectionFilter')
  const loadedCities =
    (state.entities.count && keys(state.entities.count[ownProps.model])) || []

  return {
    data,
    states,
    stateKey,
    sections,
    selectedCity,
    loadedCities,
  }
}

const mapDispatchToProps = (dispatch, ownProps) => ({
  dispatch
})

const mergeProps = (stateProps, dispatchProps, ownProps) => {
  const { model, cityAssociationName } = ownProps
  const { dispatch } = dispatchProps

  const entryCountGrabberTransformer = function(aasm_state, section, cityId) {
    return function(json) {
      const sectionKey = section.identifier || section
      const stateKey = aasm_state || 'total'
      const cityKey = cityId || ALL

      let obj = {}
      obj[model] = {}
      obj[model][cityKey] = {}
      obj[model][cityKey][stateKey] = {}
      obj[model][cityKey][stateKey][sectionKey] = json.meta.total_entries
      return { count: obj }
    }
  }

  const entryCountGrabberParams = function(aasm_state, section, cityId) {
    let params = { per_page: 1 }
    if (cityId) params[`filters[${cityAssociationName}.id]`] = cityId
    if (aasm_state)
      params[`filters[${pluralize(model)}.aasm_state]`] = aasm_state
    if (typeof section == 'object')
      params['filters[section_filters.id]'] = section.id
    return params
  }

  const dispatchDataLoad = function(aasm_state, section, cityId) {
    dispatch(
      loadAjaxData(
        model + 's',
        entryCountGrabberParams(aasm_state, section, cityId),
        'lastData',
        entryCountGrabberTransformer(aasm_state, section, cityId)
      )
    )
  }

  const loadData = function(states, cityId) {
    for (let aasm_state of states.concat(null)) { // null for the "total" row
      for (let section of stateProps.sections) {
        dispatchDataLoad(aasm_state, section, cityId)
      }
      dispatchDataLoad(aasm_state, 'total', cityId)
    }
  }

  return({
    ...stateProps,
    ...dispatchProps,
    ...ownProps,

    loadData,

    loadStates() {
      dispatch(
        loadAjaxData(
          `states/${model}`, {}, stateProps.stateKey, () => ({})
        )
      )
    },

    onCityChange(selected) {
      const city = (selected && selected.value) || ALL
      if (!stateProps.loadedCities.includes(city))
        loadData(stateProps.states, selected.value)
    }
  })
}

export default connect(mapStateToProps, mapDispatchToProps, mergeProps)(
  OverviewTable
)

{'fraction_non_alphanumeric': 0.13698229181911312, 'fraction_numerical': 0.0008780916142250841, 'mean_word_length': 3.252644679527069, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '12105', 'n_tokens_mistral': 2132, 'n_tokens_neox': 2119, 'n_words': 438}