<commit_before>import update from 'immutability-helper'
import { LOCATION_CHANGE } from 'connected-react-router'

import ActionTypes from '../actionTypes'
import randomId from '../utils/randomId'

const initialState = {
  errorMessage: '',
  nextRandomId: randomId(),
  lists: {},
}

export const initialResourceListState = {
  isDirty: false,
  isError: false,
  isLoading: true,
  list: {},
}

function updateResourceList(state = initialState, action, isLoading, isDirty) {
  const { resourceType } = action.meta
  const { lists } = state

  let previousResourceState = initialResourceListState
  if (resourceType in lists) {
    previousResourceState = lists[resourceType]
  }

  const resourceState = {}

  if (typeof isLoading !== 'undefined') {
    resourceState.isLoading = { $set: isLoading }
  }

  if (typeof isDirty !== 'undefined') {
    resourceState.isDirty = { $set: isDirty }
  }

  const data = action.payload
  if (data && !action.error) {
    resourceState.list = { $set: data }
  }

  let errorMessage = ''
  if (action.error) {
    resourceState.isError = { $set: true }
    if ('message' in action.error) {
      errorMessage = action.error.message
    }
  } else {
    resourceState.isError = { $set: false }
  }

  return update(state, {
    errorMessage: { $set: errorMessage },
    lists: {
      $set: update(state.lists, {
        [resourceType]: {
          $set: update(state.lists[resourceType] || {}, {
            $set: update(previousResourceState, resourceState),
          }),
        },
      }),
    },
  })
}

export default (state = initialState, action) => {
  switch (action.type) {
  case LOCATION_CHANGE:
    return update(state, {
      errorMessage: { $set: '' },
    })
  case ActionTypes.RESOURCE_LIST_REQUEST: {
    return updateResourceList(state, action, true)
  }
  case ActionTypes.RESOURCE_LIST_SUCCESS: {
    return updateResourceList(state, action, false, false)
  }
  case ActionTypes.RESOURCE_LIST_FAILURE: {
    return updateResourceList(state, action, false)
  }
  default:
    return state
  }
}
<commit_msg>Tidy reducer<commit_after>import update from 'immutability-helper'

import ActionTypes from '../actionTypes'

const initialState = {
  isLoading: false,
  resourceListItems: [],
}

export default (state = initialState, action) => {
  switch (action.type) {
  case ActionTypes.RESOURCE_LIST_REQUEST:
    return update(state, {
      isLoading: { $set: true },
    })
  case ActionTypes.RESOURCE_LIST_SUCCESS:
    return update(state, {
      isLoading: { $set: false },
      resourceListItems: { $set: action.payload.data },
    })
  case ActionTypes.RESOURCE_LIST_FAILURE:
    return update(state, {
      isLoading: { $set: false },
    })
  default:
    return state
  }
}

{'fraction_non_alphanumeric': 0.12386156648451731, 'fraction_numerical': 0.0, 'mean_word_length': 3.28393135725429, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3612186', 'n_tokens_mistral': 855, 'n_tokens_neox': 849, 'n_words': 192}