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