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
  }
}

@@ -38,1497 +38,118 @@
 er'%0A
-import %7B LOCATION_CHANGE %7D from 'connected-react-router'%0A%0Aimport ActionTypes from '../actionTypes'%0Aimport randomId from '../utils/randomId'%0A%0Aconst initialState = %7B%0A  errorMessage: '',%0A  nextRandomId: randomId(),%0A  lists: %7B%7D,%0A%7D%0A%0Aexport const initialResourceListState = %7B%0A  isDirty: false,%0A  isError: false,%0A  isLoading: true,%0A  list: %7B%7D,%0A%7D%0A%0Afunction updateResourceList(state = initialState, action, isLoading, isDirty) %7B%0A  const %7B resourceType %7D = action.meta%0A  const %7B lists %7D = state%0A%0A  let previousResourceState = initialResourceListState%0A  if (resourceType in lists) %7B%0A    previousResourceState = lists%5BresourceType%5D%0A  %7D%0A%0A  const resourceState = %7B%7D%0A%0A  if (typeof isLoading !== 'undefined') %7B%0A    resourceState.isLoading = %7B $set: isLoading %7D%0A  %7D%0A%0A  if (typeof isDirty !== 'undefined') %7B%0A    resourceState.isDirty = %7B $set: isDirty %7D%0A  %7D%0A%0A  const data = action.payload%0A  if (data && !action.error) %7B%0A    resourceState.list = %7B $set: data %7D%0A  %7D%0A%0A  let errorMessage = ''%0A  if (action.error) %7B%0A    resourceState.isError = %7B $set: true %7D%0A    if ('message' in action.error) %7B%0A      errorMessage = action.error.message%0A    %7D%0A  %7D else %7B%0A    resourceState.isError = %7B $set: false %7D%0A  %7D%0A%0A  return update(state, %7B%0A    errorMessage: %7B $set: errorMessage %7D,%0A    lists: %7B%0A      $set: update(state.lists, %7B%0A        %5BresourceType%5D: %7B%0A          $set: update(state.lists%5BresourceType%5D %7C%7C %7B%7D, %7B%0A            $set: update(previousResourceState, resourceState),%0A          %7D),%0A        %7D,%0A      %7D),%0A    %7D,%0A  %7D)
+%0Aimport ActionTypes from '../actionTypes'%0A%0Aconst initialState = %7B%0A  isLoading: false,%0A  resourceListItems: %5B%5D,
 %0A%7D%0A%0A
@@ -235,23 +235,41 @@
 ase 
-LOCATION_CHANGE
+ActionTypes.RESOURCE_LIST_REQUEST
 :%0A  
@@ -303,20 +303,17 @@
     
-errorMessage
+isLoading
 : %7B 
@@ -322,10 +322,12 @@
 et: 
-''
+true
  %7D,%0A
@@ -370,18 +370,16 @@
 IST_
-REQUEST: %7B
+SUCCESS:
 %0A   
@@ -396,152 +396,114 @@
 date
-ResourceList(state, action, true)%0A  %7D%0A  case ActionTypes.RESOURCE_LIST_SUCCESS: %7B%0A    return updateResourceList(state, action, false, false)%0A  %7D
+(state, %7B%0A      isLoading: %7B $set: false %7D,%0A      resourceListItems: %7B $set: action.payload.data %7D,%0A    %7D)
 %0A  c
@@ -540,18 +540,16 @@
 FAILURE:
- %7B
 %0A    ret
@@ -562,46 +562,58 @@
 date
-ResourceList(state, action, false)%0A  %7D
+(state, %7B%0A      isLoading: %7B $set: false %7D,%0A    %7D)
 %0A  d

Tidy reducer