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.

116 lines
3.9 KiB

  1. import {
  2. FAVOURITED_STATUSES_FETCH_REQUEST,
  3. FAVOURITED_STATUSES_FETCH_SUCCESS,
  4. FAVOURITED_STATUSES_FETCH_FAIL,
  5. FAVOURITED_STATUSES_EXPAND_REQUEST,
  6. FAVOURITED_STATUSES_EXPAND_SUCCESS,
  7. FAVOURITED_STATUSES_EXPAND_FAIL,
  8. } from 'flavours/glitch/actions/favourites';
  9. import {
  10. BOOKMARKED_STATUSES_FETCH_REQUEST,
  11. BOOKMARKED_STATUSES_FETCH_SUCCESS,
  12. BOOKMARKED_STATUSES_FETCH_FAIL,
  13. BOOKMARKED_STATUSES_EXPAND_REQUEST,
  14. BOOKMARKED_STATUSES_EXPAND_SUCCESS,
  15. BOOKMARKED_STATUSES_EXPAND_FAIL,
  16. } from 'flavours/glitch/actions/bookmarks';
  17. import {
  18. PINNED_STATUSES_FETCH_SUCCESS,
  19. } from 'flavours/glitch/actions/pin_statuses';
  20. import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
  21. import {
  22. FAVOURITE_SUCCESS,
  23. UNFAVOURITE_SUCCESS,
  24. BOOKMARK_SUCCESS,
  25. UNBOOKMARK_SUCCESS,
  26. PIN_SUCCESS,
  27. UNPIN_SUCCESS,
  28. } from 'flavours/glitch/actions/interactions';
  29. const initialState = ImmutableMap({
  30. favourites: ImmutableMap({
  31. next: null,
  32. loaded: false,
  33. items: ImmutableList(),
  34. }),
  35. bookmarks: ImmutableMap({
  36. next: null,
  37. loaded: false,
  38. items: ImmutableList(),
  39. }),
  40. pins: ImmutableMap({
  41. next: null,
  42. loaded: false,
  43. items: ImmutableList(),
  44. }),
  45. });
  46. const normalizeList = (state, listType, statuses, next) => {
  47. return state.update(listType, listMap => listMap.withMutations(map => {
  48. map.set('next', next);
  49. map.set('loaded', true);
  50. map.set('isLoading', false);
  51. map.set('items', ImmutableList(statuses.map(item => item.id)));
  52. }));
  53. };
  54. const appendToList = (state, listType, statuses, next) => {
  55. return state.update(listType, listMap => listMap.withMutations(map => {
  56. map.set('next', next);
  57. map.set('isLoading', false);
  58. map.set('items', map.get('items').concat(statuses.map(item => item.id)));
  59. }));
  60. };
  61. const prependOneToList = (state, listType, status) => {
  62. return state.update(listType, listMap => listMap.withMutations(map => {
  63. map.set('items', map.get('items').unshift(status.get('id')));
  64. }));
  65. };
  66. const removeOneFromList = (state, listType, status) => {
  67. return state.update(listType, listMap => listMap.withMutations(map => {
  68. map.set('items', map.get('items').filter(item => item !== status.get('id')));
  69. }));
  70. };
  71. export default function statusLists(state = initialState, action) {
  72. switch(action.type) {
  73. case FAVOURITED_STATUSES_FETCH_REQUEST:
  74. case FAVOURITED_STATUSES_EXPAND_REQUEST:
  75. return state.setIn(['favourites', 'isLoading'], true);
  76. case FAVOURITED_STATUSES_FETCH_FAIL:
  77. case FAVOURITED_STATUSES_EXPAND_FAIL:
  78. return state.setIn(['favourites', 'isLoading'], false);
  79. case FAVOURITED_STATUSES_FETCH_SUCCESS:
  80. return normalizeList(state, 'favourites', action.statuses, action.next);
  81. case FAVOURITED_STATUSES_EXPAND_SUCCESS:
  82. return appendToList(state, 'favourites', action.statuses, action.next);
  83. case BOOKMARKED_STATUSES_FETCH_REQUEST:
  84. case BOOKMARKED_STATUSES_EXPAND_REQUEST:
  85. return state.setIn(['bookmarks', 'isLoading'], true);
  86. case BOOKMARKED_STATUSES_FETCH_FAIL:
  87. case BOOKMARKED_STATUSES_EXPAND_FAIL:
  88. return state.setIn(['bookmarks', 'isLoading'], false);
  89. case BOOKMARKED_STATUSES_FETCH_SUCCESS:
  90. return normalizeList(state, 'bookmarks', action.statuses, action.next);
  91. case BOOKMARKED_STATUSES_EXPAND_SUCCESS:
  92. return appendToList(state, 'bookmarks', action.statuses, action.next);
  93. case FAVOURITE_SUCCESS:
  94. return prependOneToList(state, 'favourites', action.status);
  95. case UNFAVOURITE_SUCCESS:
  96. return removeOneFromList(state, 'favourites', action.status);
  97. case BOOKMARK_SUCCESS:
  98. return prependOneToList(state, 'bookmarks', action.status);
  99. case UNBOOKMARK_SUCCESS:
  100. return removeOneFromList(state, 'bookmarks', action.status);
  101. case PINNED_STATUSES_FETCH_SUCCESS:
  102. return normalizeList(state, 'pins', action.statuses, action.next);
  103. case PIN_SUCCESS:
  104. return prependOneToList(state, 'pins', action.status);
  105. case UNPIN_SUCCESS:
  106. return removeOneFromList(state, 'pins', action.status);
  107. default:
  108. return state;
  109. }
  110. };