2022-12-11 12:37:00 -08:00
|
|
|
import { Map as ImmutableMap } from 'immutable';
|
|
|
|
|
|
|
|
import {
|
|
|
|
GROUP_CREATE_SUCCESS,
|
2022-12-15 14:51:30 -08:00
|
|
|
GROUP_UPDATE_SUCCESS,
|
2022-12-11 12:37:00 -08:00
|
|
|
GROUP_DELETE_SUCCESS,
|
|
|
|
GROUP_RELATIONSHIPS_FETCH_SUCCESS,
|
|
|
|
} from 'soapbox/actions/groups';
|
|
|
|
import { normalizeGroupRelationship } from 'soapbox/normalizers';
|
|
|
|
|
|
|
|
import type { AnyAction } from 'redux';
|
|
|
|
import type { APIEntity } from 'soapbox/types/entities';
|
|
|
|
|
|
|
|
type GroupRelationshipRecord = ReturnType<typeof normalizeGroupRelationship>;
|
|
|
|
type APIEntities = Array<APIEntity>;
|
|
|
|
|
|
|
|
type State = ImmutableMap<string, GroupRelationshipRecord>;
|
|
|
|
|
|
|
|
const normalizeRelationships = (state: State, relationships: APIEntities) => {
|
|
|
|
relationships.forEach(relationship => {
|
|
|
|
state = state.set(relationship.id, normalizeGroupRelationship(relationship));
|
|
|
|
});
|
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function groupRelationships(state: State = ImmutableMap(), action: AnyAction) {
|
|
|
|
switch (action.type) {
|
|
|
|
case GROUP_CREATE_SUCCESS:
|
2022-12-15 14:51:30 -08:00
|
|
|
case GROUP_UPDATE_SUCCESS:
|
2022-12-11 12:37:00 -08:00
|
|
|
return state.set(action.group.id, normalizeGroupRelationship({ id: action.group.id, member: true, requested: false, role: 'admin' }));
|
|
|
|
case GROUP_DELETE_SUCCESS:
|
|
|
|
return state.delete(action.id);
|
|
|
|
case GROUP_RELATIONSHIPS_FETCH_SUCCESS:
|
|
|
|
return normalizeRelationships(state, action.relationships);
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|