gab-social/app/javascript/gabsocial/reducers/groups.js

34 lines
842 B
JavaScript
Raw Normal View History

2019-07-02 08:10:25 +01:00
import {
GROUP_FETCH_SUCCESS,
GROUP_FETCH_FAIL,
GROUPS_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)
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-04-02 04:17:21 +01:00
return state.set(action.id, 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
}