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.

87 lines
2.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. PINNED_STATUSES_FETCH_SUCCESS,
  11. } from 'flavours/glitch/actions/pin_statuses';
  12. import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
  13. import {
  14. FAVOURITE_SUCCESS,
  15. UNFAVOURITE_SUCCESS,
  16. PIN_SUCCESS,
  17. UNPIN_SUCCESS,
  18. } from 'flavours/glitch/actions/interactions';
  19. const initialState = ImmutableMap({
  20. favourites: ImmutableMap({
  21. next: null,
  22. loaded: false,
  23. items: ImmutableList(),
  24. }),
  25. pins: ImmutableMap({
  26. next: null,
  27. loaded: false,
  28. items: ImmutableList(),
  29. }),
  30. });
  31. const normalizeList = (state, listType, statuses, next) => {
  32. return state.update(listType, listMap => listMap.withMutations(map => {
  33. map.set('next', next);
  34. map.set('loaded', true);
  35. map.set('isLoading', false);
  36. map.set('items', ImmutableList(statuses.map(item => item.id)));
  37. }));
  38. };
  39. const appendToList = (state, listType, statuses, next) => {
  40. return state.update(listType, listMap => listMap.withMutations(map => {
  41. map.set('next', next);
  42. map.set('isLoading', false);
  43. map.set('items', map.get('items').concat(statuses.map(item => item.id)));
  44. }));
  45. };
  46. const prependOneToList = (state, listType, status) => {
  47. return state.update(listType, listMap => listMap.withMutations(map => {
  48. map.set('items', map.get('items').unshift(status.get('id')));
  49. }));
  50. };
  51. const removeOneFromList = (state, listType, status) => {
  52. return state.update(listType, listMap => listMap.withMutations(map => {
  53. map.set('items', map.get('items').filter(item => item !== status.get('id')));
  54. }));
  55. };
  56. export default function statusLists(state = initialState, action) {
  57. switch(action.type) {
  58. case FAVOURITED_STATUSES_FETCH_REQUEST:
  59. case FAVOURITED_STATUSES_EXPAND_REQUEST:
  60. return state.setIn(['favourites', 'isLoading'], true);
  61. case FAVOURITED_STATUSES_FETCH_FAIL:
  62. case FAVOURITED_STATUSES_EXPAND_FAIL:
  63. return state.setIn(['favourites', 'isLoading'], false);
  64. case FAVOURITED_STATUSES_FETCH_SUCCESS:
  65. return normalizeList(state, 'favourites', action.statuses, action.next);
  66. case FAVOURITED_STATUSES_EXPAND_SUCCESS:
  67. return appendToList(state, 'favourites', action.statuses, action.next);
  68. case FAVOURITE_SUCCESS:
  69. return prependOneToList(state, 'favourites', action.status);
  70. case UNFAVOURITE_SUCCESS:
  71. return removeOneFromList(state, 'favourites', action.status);
  72. case PINNED_STATUSES_FETCH_SUCCESS:
  73. return normalizeList(state, 'pins', action.statuses, action.next);
  74. case PIN_SUCCESS:
  75. return prependOneToList(state, 'pins', action.status);
  76. case UNPIN_SUCCESS:
  77. return removeOneFromList(state, 'pins', action.status);
  78. default:
  79. return state;
  80. }
  81. };