You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.9 KiB

  1. import {
  2. FOLLOWERS_FETCH_SUCCESS,
  3. FOLLOWERS_EXPAND_SUCCESS,
  4. FOLLOWING_FETCH_SUCCESS,
  5. FOLLOWING_EXPAND_SUCCESS
  6. } from '../actions/accounts';
  7. import { SUGGESTIONS_FETCH_SUCCESS } from '../actions/suggestions';
  8. import {
  9. REBLOGS_FETCH_SUCCESS,
  10. FAVOURITES_FETCH_SUCCESS
  11. } from '../actions/interactions';
  12. import Immutable from 'immutable';
  13. const initialState = Immutable.Map({
  14. followers: Immutable.Map(),
  15. following: Immutable.Map(),
  16. suggestions: Immutable.List(),
  17. reblogged_by: Immutable.Map(),
  18. favourited_by: Immutable.Map()
  19. });
  20. const normalizeList = (state, type, id, accounts, next) => {
  21. return state.setIn([type, id], Immutable.Map({
  22. next,
  23. items: Immutable.List(accounts.map(item => item.id))
  24. }));
  25. };
  26. const appendToList = (state, type, id, accounts, next) => {
  27. return state.updateIn([type, id], map => {
  28. return map.set('next', next).update('items', list => list.push(...accounts.map(item => item.id)));
  29. });
  30. };
  31. export default function userLists(state = initialState, action) {
  32. switch(action.type) {
  33. case FOLLOWERS_FETCH_SUCCESS:
  34. return normalizeList(state, 'followers', action.id, action.accounts, action.next);
  35. case FOLLOWERS_EXPAND_SUCCESS:
  36. return appendToList(state, 'followers', action.id, action.accounts, action.next);
  37. case FOLLOWING_FETCH_SUCCESS:
  38. return normalizeList(state, 'following', action.id, action.accounts, action.next);
  39. case FOLLOWING_EXPAND_SUCCESS:
  40. return appendToList(state, 'following', action.id, action.accounts, action.next);
  41. case SUGGESTIONS_FETCH_SUCCESS:
  42. return state.set('suggestions', Immutable.List(action.accounts.map(item => item.id)));
  43. case REBLOGS_FETCH_SUCCESS:
  44. return state.setIn(['reblogged_by', action.id], Immutable.List(action.accounts.map(item => item.id)));
  45. case FAVOURITES_FETCH_SUCCESS:
  46. return state.setIn(['favourited_by', action.id], Immutable.List(action.accounts.map(item => item.id)));
  47. default:
  48. return state;
  49. }
  50. };