diff options
author | Eugen Rochko <eugen@zeonfederated.com> | 2017-06-11 17:07:35 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-11 17:07:35 +0200 |
commit | 47bf7a8047ce59b899d147e4483168f9852eeb7c (patch) | |
tree | 79dd51ed84b1ed350cd754c8fca988dd6718a93a /app/javascript/mastodon/reducers/contexts.js | |
parent | 85d405c810a3bfc146c7901d770444419f32e46f (diff) |
Fix #3665 - Refactor timelines reducer (#3686)
* Move ancestors/descendants out of timelines reducer * Refactor timelines reducer All types of timelines now have a flat structure and use the same reducer functions and actions * Reintroduce some missing behaviours * Fix wrong import in reports * Fix includes typo * Fix issue related to "next" pagination in timelines and notifications * Fix bug with timeline's initial state, expandNotifications
Diffstat (limited to 'app/javascript/mastodon/reducers/contexts.js')
-rw-r--r-- | app/javascript/mastodon/reducers/contexts.js | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/app/javascript/mastodon/reducers/contexts.js b/app/javascript/mastodon/reducers/contexts.js new file mode 100644 index 000000000..8a24f5f7a --- /dev/null +++ b/app/javascript/mastodon/reducers/contexts.js @@ -0,0 +1,43 @@ +import { CONTEXT_FETCH_SUCCESS } from '../actions/statuses'; +import { TIMELINE_DELETE } from '../actions/timelines'; +import Immutable from 'immutable'; + +const initialState = Immutable.Map({ + ancestors: Immutable.Map(), + descendants: Immutable.Map(), +}); + +const normalizeContext = (state, id, ancestors, descendants) => { + const ancestorsIds = ancestors.map(ancestor => ancestor.get('id')); + const descendantsIds = descendants.map(descendant => descendant.get('id')); + + return state.withMutations(map => { + map.setIn(['ancestors', id], ancestorsIds); + map.setIn(['descendants', id], descendantsIds); + }); +}; + +const deleteFromContexts = (state, id) => { + state.getIn(['descendants', id], Immutable.List()).forEach(descendantId => { + state = state.updateIn(['ancestors', descendantId], Immutable.List(), list => list.filterNot(itemId => itemId === id)); + }); + + state.getIn(['ancestors', id], Immutable.List()).forEach(ancestorId => { + state = state.updateIn(['descendants', ancestorId], Immutable.List(), list => list.filterNot(itemId => itemId === id)); + }); + + state = state.deleteIn(['descendants', id]).deleteIn(['ancestors', id]); + + return state; +}; + +export default function contexts(state = initialState, action) { + switch(action.type) { + case CONTEXT_FETCH_SUCCESS: + return normalizeContext(state, action.id, Immutable.fromJS(action.ancestors), Immutable.fromJS(action.descendants)); + case TIMELINE_DELETE: + return deleteFromContexts(state, action.id); + default: + return state; + } +}; |