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.

39 lines
1.2 KiB

  1. import {
  2. FAVOURITED_STATUSES_FETCH_SUCCESS,
  3. FAVOURITED_STATUSES_EXPAND_SUCCESS
  4. } from '../actions/favourites';
  5. import Immutable from 'immutable';
  6. const initialState = Immutable.Map({
  7. favourites: Immutable.Map({
  8. next: null,
  9. loaded: false,
  10. items: Immutable.List()
  11. })
  12. });
  13. const normalizeList = (state, listType, statuses, next) => {
  14. return state.update(listType, listMap => listMap.withMutations(map => {
  15. map.set('next', next);
  16. map.set('loaded', true);
  17. map.set('items', Immutable.List(statuses.map(item => item.id)));
  18. }));
  19. };
  20. const appendToList = (state, listType, statuses, next) => {
  21. return state.update(listType, listMap => listMap.withMutations(map => {
  22. map.set('next', next);
  23. map.set('items', map.get('items').push(...statuses.map(item => item.id)));
  24. }));
  25. };
  26. export default function statusLists(state = initialState, action) {
  27. switch(action.type) {
  28. case FAVOURITED_STATUSES_FETCH_SUCCESS:
  29. return normalizeList(state, 'favourites', action.statuses, action.next);
  30. case FAVOURITED_STATUSES_EXPAND_SUCCESS:
  31. return appendToList(state, 'favourites', action.statuses, action.next);
  32. default:
  33. return state;
  34. }
  35. };