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.

59 lines
2.2 KiB

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