2019-07-02 08:10:25 +01:00
|
|
|
import { GROUP_RELATIONSHIPS_FETCH_SUCCESS, GROUP_JOIN_SUCCESS, GROUP_LEAVE_SUCCESS } from '../actions/groups';
|
|
|
|
import { Map as ImmutableMap, fromJS } from 'immutable';
|
|
|
|
|
|
|
|
const normalizeRelationship = (state, relationship) => state.set(relationship.id, fromJS(relationship));
|
|
|
|
|
|
|
|
const normalizeRelationships = (state, relationships) => {
|
2020-11-25 21:22:37 +00:00
|
|
|
relationships.forEach(relationship => {
|
|
|
|
state = normalizeRelationship(state, relationship);
|
|
|
|
});
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-11-25 21:22:37 +00:00
|
|
|
return state
|
2019-07-02 08:10:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const initialState = ImmutableMap();
|
|
|
|
|
|
|
|
export default function group_relationships(state = initialState, action) {
|
|
|
|
switch(action.type) {
|
|
|
|
case GROUP_JOIN_SUCCESS:
|
|
|
|
case GROUP_LEAVE_SUCCESS:
|
|
|
|
return normalizeRelationship(state, action.relationship);
|
|
|
|
case GROUP_RELATIONSHIPS_FETCH_SUCCESS:
|
|
|
|
return normalizeRelationships(state, action.relationships);
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|