2020-01-23 16:00:13 -05:00
|
|
|
import {
|
|
|
|
ANNOUNCEMENTS_FETCH_REQUEST,
|
|
|
|
ANNOUNCEMENTS_FETCH_SUCCESS,
|
|
|
|
ANNOUNCEMENTS_FETCH_FAIL,
|
|
|
|
ANNOUNCEMENTS_UPDATE,
|
|
|
|
ANNOUNCEMENTS_REACTION_UPDATE,
|
|
|
|
ANNOUNCEMENTS_REACTION_ADD_REQUEST,
|
|
|
|
ANNOUNCEMENTS_REACTION_ADD_FAIL,
|
|
|
|
ANNOUNCEMENTS_REACTION_REMOVE_REQUEST,
|
|
|
|
ANNOUNCEMENTS_REACTION_REMOVE_FAIL,
|
2020-01-25 10:35:33 -05:00
|
|
|
ANNOUNCEMENTS_TOGGLE_SHOW,
|
2020-01-26 14:07:26 -05:00
|
|
|
ANNOUNCEMENTS_DELETE,
|
2020-01-23 16:00:13 -05:00
|
|
|
} from '../actions/announcements';
|
2020-01-25 10:35:33 -05:00
|
|
|
import { Map as ImmutableMap, List as ImmutableList, Set as ImmutableSet, fromJS } from 'immutable';
|
2020-01-23 16:00:13 -05:00
|
|
|
|
|
|
|
const initialState = ImmutableMap({
|
|
|
|
items: ImmutableList(),
|
|
|
|
isLoading: false,
|
2020-01-25 10:35:33 -05:00
|
|
|
show: true,
|
|
|
|
unread: ImmutableSet(),
|
2020-01-23 16:00:13 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
const updateReaction = (state, id, name, updater) => state.update('items', list => list.map(announcement => {
|
|
|
|
if (announcement.get('id') === id) {
|
|
|
|
return announcement.update('reactions', reactions => {
|
2020-01-26 14:07:26 -05:00
|
|
|
const idx = reactions.findIndex(reaction => reaction.get('name') === name);
|
|
|
|
|
|
|
|
if (idx > -1) {
|
|
|
|
return reactions.update(idx, reaction => updater(reaction));
|
2020-01-23 16:00:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return reactions.push(updater(fromJS({ name, count: 0 })));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return announcement;
|
|
|
|
}));
|
|
|
|
|
|
|
|
const updateReactionCount = (state, reaction) => updateReaction(state, reaction.announcement_id, reaction.name, x => x.set('count', reaction.count));
|
|
|
|
|
|
|
|
const addReaction = (state, id, name) => updateReaction(state, id, name, x => x.set('me', true).update('count', y => y + 1));
|
|
|
|
|
|
|
|
const removeReaction = (state, id, name) => updateReaction(state, id, name, x => x.set('me', false).update('count', y => y - 1));
|
|
|
|
|
2020-01-25 10:35:33 -05:00
|
|
|
const addUnread = (state, items) => {
|
2020-01-26 14:07:26 -05:00
|
|
|
if (state.get('show')) {
|
|
|
|
return state;
|
|
|
|
}
|
2020-01-25 10:35:33 -05:00
|
|
|
|
|
|
|
const newIds = ImmutableSet(items.map(x => x.get('id')));
|
|
|
|
const oldIds = ImmutableSet(state.get('items').map(x => x.get('id')));
|
2020-01-26 14:07:26 -05:00
|
|
|
|
2020-01-25 10:35:33 -05:00
|
|
|
return state.update('unread', unread => unread.union(newIds.subtract(oldIds)));
|
|
|
|
};
|
|
|
|
|
2020-01-26 14:07:26 -05:00
|
|
|
const sortAnnouncements = list => list.sortBy(x => x.get('starts_at') || x.get('published_at'));
|
|
|
|
|
|
|
|
const updateAnnouncement = (state, announcement) => {
|
|
|
|
const idx = state.get('items').findIndex(x => x.get('id') === announcement.get('id'));
|
|
|
|
|
|
|
|
state = addUnread(state, [announcement]);
|
|
|
|
|
|
|
|
if (idx > -1) {
|
|
|
|
// Deep merge is used because announcements from the streaming API do not contain
|
|
|
|
// personalized data about which reactions have been selected by the given user,
|
|
|
|
// and that is information we want to preserve
|
|
|
|
return state.update('items', list => sortAnnouncements(list.update(idx, x => x.mergeDeep(announcement))));
|
|
|
|
}
|
|
|
|
|
|
|
|
return state.update('items', list => sortAnnouncements(list.unshift(announcement)));
|
|
|
|
};
|
|
|
|
|
2020-01-23 16:00:13 -05:00
|
|
|
export default function announcementsReducer(state = initialState, action) {
|
|
|
|
switch(action.type) {
|
2020-01-25 10:35:33 -05:00
|
|
|
case ANNOUNCEMENTS_TOGGLE_SHOW:
|
|
|
|
return state.withMutations(map => {
|
|
|
|
if (!map.get('show')) map.set('unread', ImmutableSet());
|
|
|
|
map.set('show', !map.get('show'));
|
|
|
|
});
|
2020-01-23 16:00:13 -05:00
|
|
|
case ANNOUNCEMENTS_FETCH_REQUEST:
|
|
|
|
return state.set('isLoading', true);
|
|
|
|
case ANNOUNCEMENTS_FETCH_SUCCESS:
|
|
|
|
return state.withMutations(map => {
|
2020-01-25 10:35:33 -05:00
|
|
|
const items = fromJS(action.announcements);
|
2020-01-26 14:07:26 -05:00
|
|
|
|
2020-01-25 10:35:33 -05:00
|
|
|
map.set('unread', ImmutableSet());
|
|
|
|
map.set('items', items);
|
2020-01-23 16:00:13 -05:00
|
|
|
map.set('isLoading', false);
|
2020-01-26 14:07:26 -05:00
|
|
|
|
|
|
|
addUnread(map, items);
|
2020-01-23 16:00:13 -05:00
|
|
|
});
|
|
|
|
case ANNOUNCEMENTS_FETCH_FAIL:
|
|
|
|
return state.set('isLoading', false);
|
|
|
|
case ANNOUNCEMENTS_UPDATE:
|
2020-01-26 14:07:26 -05:00
|
|
|
return updateAnnouncement(state, fromJS(action.announcement));
|
2020-01-23 16:00:13 -05:00
|
|
|
case ANNOUNCEMENTS_REACTION_UPDATE:
|
|
|
|
return updateReactionCount(state, action.reaction);
|
|
|
|
case ANNOUNCEMENTS_REACTION_ADD_REQUEST:
|
|
|
|
case ANNOUNCEMENTS_REACTION_REMOVE_FAIL:
|
|
|
|
return addReaction(state, action.id, action.name);
|
|
|
|
case ANNOUNCEMENTS_REACTION_REMOVE_REQUEST:
|
|
|
|
case ANNOUNCEMENTS_REACTION_ADD_FAIL:
|
|
|
|
return removeReaction(state, action.id, action.name);
|
2020-01-26 14:07:26 -05:00
|
|
|
case ANNOUNCEMENTS_DELETE:
|
|
|
|
return state.update('unread', set => set.delete(action.id)).update('items', list => {
|
|
|
|
const idx = list.findIndex(x => x.get('id') === action.id);
|
|
|
|
|
|
|
|
if (idx > -1) {
|
|
|
|
return list.delete(idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
});
|
2020-01-23 16:00:13 -05:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|