bigbuffet-rw/app/soapbox/reducers/group_relationships.js

28 lines
857 B
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
import { Map as ImmutableMap, fromJS } from 'immutable';
2022-01-10 14:01:24 -08:00
import { GROUP_RELATIONSHIPS_FETCH_SUCCESS, GROUP_JOIN_SUCCESS, GROUP_LEAVE_SUCCESS } from '../actions/groups';
2020-04-14 11:44:40 -07:00
2020-03-27 13:59:38 -07:00
const normalizeRelationship = (state, relationship) => state.set(relationship.id, fromJS(relationship));
2020-04-14 11:44:40 -07:00
2020-03-27 13:59:38 -07:00
const normalizeRelationships = (state, relationships) => {
2020-04-14 11:44:40 -07:00
relationships.forEach(relationship => {
state = normalizeRelationship(state, relationship);
});
return state;
2020-03-27 13:59:38 -07:00
};
2020-04-14 11:44:40 -07:00
2020-03-27 13:59:38 -07:00
const initialState = ImmutableMap();
2020-04-14 11:44:40 -07:00
2020-03-27 13:59:38 -07:00
export default function group_relationships(state = initialState, action) {
2020-04-14 11:44:40 -07:00
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;
}
2021-08-03 12:22:51 -07:00
}