logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

status_lists.js (2502B)


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