You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
909 B
32 lines
909 B
import { Reducer } from 'redux'
|
|
import merge from 'lodash/merge'
|
|
|
|
import { EntitiesActions } from '../actions/entities'
|
|
import { EntitiesState } from '../types'
|
|
|
|
const initialState: EntitiesState = {}
|
|
|
|
const reducer: Reducer<EntitiesState, EntitiesActions> = (state = initialState, action) => {
|
|
switch (action.type) {
|
|
case 'ENTITIES_SET_ENTITY': {
|
|
const { type, entity } = action.payload
|
|
const collection = state[type] || {}
|
|
const existing = collection[entity.id] || {}
|
|
|
|
return {
|
|
...state,
|
|
[type]: {
|
|
...collection,
|
|
[entity.id]: merge(existing, entity),
|
|
},
|
|
}
|
|
}
|
|
case 'ENTITIES_SET_ENTITIES': {
|
|
return merge(state, action.payload.entities)
|
|
}
|
|
default:
|
|
return state
|
|
}
|
|
}
|
|
|
|
export default reducer
|