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.

47 lines
1.4 KiB

  1. import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
  2. import {
  3. LIST_ADDER_RESET,
  4. LIST_ADDER_SETUP,
  5. LIST_ADDER_LISTS_FETCH_REQUEST,
  6. LIST_ADDER_LISTS_FETCH_SUCCESS,
  7. LIST_ADDER_LISTS_FETCH_FAIL,
  8. LIST_EDITOR_ADD_SUCCESS,
  9. LIST_EDITOR_REMOVE_SUCCESS,
  10. } from '../actions/lists';
  11. const initialState = ImmutableMap({
  12. accountId: null,
  13. lists: ImmutableMap({
  14. items: ImmutableList(),
  15. loaded: false,
  16. isLoading: false,
  17. }),
  18. });
  19. export default function listAdderReducer(state = initialState, action) {
  20. switch(action.type) {
  21. case LIST_ADDER_RESET:
  22. return initialState;
  23. case LIST_ADDER_SETUP:
  24. return state.withMutations(map => {
  25. map.set('accountId', action.account.get('id'));
  26. });
  27. case LIST_ADDER_LISTS_FETCH_REQUEST:
  28. return state.setIn(['lists', 'isLoading'], true);
  29. case LIST_ADDER_LISTS_FETCH_FAIL:
  30. return state.setIn(['lists', 'isLoading'], false);
  31. case LIST_ADDER_LISTS_FETCH_SUCCESS:
  32. return state.update('lists', lists => lists.withMutations(map => {
  33. map.set('isLoading', false);
  34. map.set('loaded', true);
  35. map.set('items', ImmutableList(action.lists.map(item => item.id)));
  36. }));
  37. case LIST_EDITOR_ADD_SUCCESS:
  38. return state.updateIn(['lists', 'items'], list => list.unshift(action.listId));
  39. case LIST_EDITOR_REMOVE_SUCCESS:
  40. return state.updateIn(['lists', 'items'], list => list.filterNot(item => item === action.listId));
  41. default:
  42. return state;
  43. }
  44. };