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.

37 lines
894 B

  1. import {
  2. LIST_FETCH_SUCCESS,
  3. LIST_FETCH_FAIL,
  4. LISTS_FETCH_SUCCESS,
  5. LIST_CREATE_SUCCESS,
  6. LIST_UPDATE_SUCCESS,
  7. LIST_DELETE_SUCCESS,
  8. } from '../actions/lists';
  9. import { Map as ImmutableMap, fromJS } from 'immutable';
  10. const initialState = ImmutableMap();
  11. const normalizeList = (state, list) => state.set(list.id, fromJS(list));
  12. const normalizeLists = (state, lists) => {
  13. lists.forEach(list => {
  14. state = normalizeList(state, list);
  15. });
  16. return state;
  17. };
  18. export default function lists(state = initialState, action) {
  19. switch(action.type) {
  20. case LIST_FETCH_SUCCESS:
  21. case LIST_CREATE_SUCCESS:
  22. case LIST_UPDATE_SUCCESS:
  23. return normalizeList(state, action.list);
  24. case LISTS_FETCH_SUCCESS:
  25. return normalizeLists(state, action.lists);
  26. case LIST_DELETE_SUCCESS:
  27. case LIST_FETCH_FAIL:
  28. return state.set(action.id, false);
  29. default:
  30. return state;
  31. }
  32. };