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.

75 lines
2.3 KiB

  1. import {
  2. FAVOURITED_STATUSES_FETCH_SUCCESS,
  3. FAVOURITED_STATUSES_EXPAND_SUCCESS,
  4. } from '../actions/favourites';
  5. import {
  6. PINNED_STATUSES_FETCH_SUCCESS,
  7. } from '../actions/pin_statuses';
  8. import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
  9. import {
  10. FAVOURITE_SUCCESS,
  11. UNFAVOURITE_SUCCESS,
  12. PIN_SUCCESS,
  13. UNPIN_SUCCESS,
  14. } from '../actions/interactions';
  15. const initialState = ImmutableMap({
  16. favourites: ImmutableMap({
  17. next: null,
  18. loaded: false,
  19. items: ImmutableList(),
  20. }),
  21. pins: ImmutableMap({
  22. next: null,
  23. loaded: false,
  24. items: ImmutableList(),
  25. }),
  26. });
  27. const normalizeList = (state, listType, statuses, next) => {
  28. return state.update(listType, listMap => listMap.withMutations(map => {
  29. map.set('next', next);
  30. map.set('loaded', true);
  31. map.set('items', ImmutableList(statuses.map(item => item.id)));
  32. }));
  33. };
  34. const appendToList = (state, listType, statuses, next) => {
  35. return state.update(listType, listMap => listMap.withMutations(map => {
  36. map.set('next', next);
  37. map.set('items', map.get('items').concat(statuses.map(item => item.id)));
  38. }));
  39. };
  40. const prependOneToList = (state, listType, status) => {
  41. return state.update(listType, listMap => listMap.withMutations(map => {
  42. map.set('items', map.get('items').unshift(status.get('id')));
  43. }));
  44. };
  45. const removeOneFromList = (state, listType, status) => {
  46. return state.update(listType, listMap => listMap.withMutations(map => {
  47. map.set('items', map.get('items').filter(item => item !== status.get('id')));
  48. }));
  49. };
  50. export default function statusLists(state = initialState, action) {
  51. switch(action.type) {
  52. case FAVOURITED_STATUSES_FETCH_SUCCESS:
  53. return normalizeList(state, 'favourites', action.statuses, action.next);
  54. case FAVOURITED_STATUSES_EXPAND_SUCCESS:
  55. return appendToList(state, 'favourites', action.statuses, action.next);
  56. case FAVOURITE_SUCCESS:
  57. return prependOneToList(state, 'favourites', action.status);
  58. case UNFAVOURITE_SUCCESS:
  59. return removeOneFromList(state, 'favourites', action.status);
  60. case PINNED_STATUSES_FETCH_SUCCESS:
  61. return normalizeList(state, 'pins', action.statuses, action.next);
  62. case PIN_SUCCESS:
  63. return prependOneToList(state, 'pins', action.status);
  64. case UNPIN_SUCCESS:
  65. return removeOneFromList(state, 'pins', action.status);
  66. default:
  67. return state;
  68. }
  69. };