2019-07-02 03:10:25 -04:00
|
|
|
import {
|
|
|
|
GROUP_FETCH_SUCCESS,
|
|
|
|
GROUP_FETCH_FAIL,
|
|
|
|
GROUPS_FETCH_SUCCESS,
|
2020-09-24 16:21:16 -05:00
|
|
|
GROUPS_BY_CATEGORY_FETCH_SUCCESS,
|
|
|
|
GROUPS_BY_TAG_FETCH_SUCCESS,
|
2020-04-01 23:17:21 -04:00
|
|
|
} from '../actions/groups'
|
|
|
|
import { GROUP_UPDATE_SUCCESS } from '../actions/group_editor'
|
|
|
|
import { Map as ImmutableMap, fromJS } from 'immutable'
|
2019-07-02 03:10:25 -04:00
|
|
|
|
2020-04-01 23:17:21 -04:00
|
|
|
const initialState = ImmutableMap()
|
2019-07-02 03:10:25 -04:00
|
|
|
|
2020-04-01 23:17:21 -04:00
|
|
|
const normalizeGroup = (state, group) => state.set(group.id, fromJS(group))
|
2019-07-02 03:10:25 -04:00
|
|
|
|
|
|
|
const normalizeGroups = (state, groups) => {
|
|
|
|
groups.forEach(group => {
|
2020-04-01 23:17:21 -04:00
|
|
|
state = normalizeGroup(state, group)
|
|
|
|
})
|
2019-07-02 03:10:25 -04:00
|
|
|
|
2020-04-01 23:17:21 -04:00
|
|
|
return state
|
|
|
|
}
|
2019-07-02 03:10:25 -04:00
|
|
|
|
|
|
|
export default function groups(state = initialState, action) {
|
|
|
|
switch(action.type) {
|
|
|
|
case GROUP_FETCH_SUCCESS:
|
2019-07-17 21:56:06 +03:00
|
|
|
case GROUP_UPDATE_SUCCESS:
|
2020-04-01 23:17:21 -04:00
|
|
|
return normalizeGroup(state, action.group)
|
2020-09-24 16:21:16 -05:00
|
|
|
case GROUPS_BY_CATEGORY_FETCH_SUCCESS:
|
|
|
|
case GROUPS_BY_TAG_FETCH_SUCCESS:
|
2019-07-02 03:10:25 -04:00
|
|
|
case GROUPS_FETCH_SUCCESS:
|
2020-04-01 23:17:21 -04:00
|
|
|
return normalizeGroups(state, action.groups)
|
2019-07-02 03:10:25 -04:00
|
|
|
case GROUP_FETCH_FAIL:
|
2020-11-25 15:22:37 -06:00
|
|
|
return state.set(action.groupId, false)
|
2019-07-02 03:10:25 -04:00
|
|
|
default:
|
2020-04-01 23:17:21 -04:00
|
|
|
return state
|
2019-07-02 03:10:25 -04:00
|
|
|
}
|
2020-04-01 23:17:21 -04:00
|
|
|
}
|