list_editor.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
  2. import {
  3. LIST_CREATE_REQUEST,
  4. LIST_CREATE_FAIL,
  5. LIST_CREATE_SUCCESS,
  6. LIST_UPDATE_REQUEST,
  7. LIST_UPDATE_FAIL,
  8. LIST_UPDATE_SUCCESS,
  9. LIST_EDITOR_RESET,
  10. LIST_EDITOR_SETUP,
  11. LIST_EDITOR_TITLE_CHANGE,
  12. LIST_ACCOUNTS_FETCH_REQUEST,
  13. LIST_ACCOUNTS_FETCH_SUCCESS,
  14. LIST_ACCOUNTS_FETCH_FAIL,
  15. LIST_EDITOR_SUGGESTIONS_READY,
  16. LIST_EDITOR_SUGGESTIONS_CLEAR,
  17. LIST_EDITOR_SUGGESTIONS_CHANGE,
  18. LIST_EDITOR_ADD_SUCCESS,
  19. LIST_EDITOR_REMOVE_SUCCESS,
  20. } from '../actions/lists';
  21. const initialState = ImmutableMap({
  22. listId: null,
  23. isSubmitting: false,
  24. isChanged: false,
  25. title: '',
  26. accounts: ImmutableMap({
  27. items: ImmutableList(),
  28. loaded: false,
  29. isLoading: false,
  30. }),
  31. suggestions: ImmutableMap({
  32. value: '',
  33. items: ImmutableList(),
  34. }),
  35. });
  36. export default function listEditorReducer(state = initialState, action) {
  37. switch(action.type) {
  38. case LIST_EDITOR_RESET:
  39. return initialState;
  40. case LIST_EDITOR_SETUP:
  41. return state.withMutations(map => {
  42. map.set('listId', action.list.get('id'));
  43. map.set('title', action.list.get('title'));
  44. map.set('isSubmitting', false);
  45. });
  46. case LIST_EDITOR_TITLE_CHANGE:
  47. return state.withMutations(map => {
  48. map.set('title', action.value);
  49. map.set('isChanged', true);
  50. });
  51. case LIST_CREATE_REQUEST:
  52. case LIST_UPDATE_REQUEST:
  53. return state.withMutations(map => {
  54. map.set('isSubmitting', true);
  55. map.set('isChanged', false);
  56. });
  57. case LIST_CREATE_FAIL:
  58. case LIST_UPDATE_FAIL:
  59. return state.set('isSubmitting', false);
  60. case LIST_CREATE_SUCCESS:
  61. case LIST_UPDATE_SUCCESS:
  62. return state.withMutations(map => {
  63. map.set('isSubmitting', false);
  64. map.set('listId', action.list.id);
  65. });
  66. case LIST_ACCOUNTS_FETCH_REQUEST:
  67. return state.setIn(['accounts', 'isLoading'], true);
  68. case LIST_ACCOUNTS_FETCH_FAIL:
  69. return state.setIn(['accounts', 'isLoading'], false);
  70. case LIST_ACCOUNTS_FETCH_SUCCESS:
  71. return state.update('accounts', accounts => accounts.withMutations(map => {
  72. map.set('isLoading', false);
  73. map.set('loaded', true);
  74. map.set('items', ImmutableList(action.accounts.map(item => item.id)));
  75. }));
  76. case LIST_EDITOR_SUGGESTIONS_CHANGE:
  77. return state.setIn(['suggestions', 'value'], action.value);
  78. case LIST_EDITOR_SUGGESTIONS_READY:
  79. return state.setIn(['suggestions', 'items'], ImmutableList(action.accounts.map(item => item.id)));
  80. case LIST_EDITOR_SUGGESTIONS_CLEAR:
  81. return state.update('suggestions', suggestions => suggestions.withMutations(map => {
  82. map.set('items', ImmutableList());
  83. map.set('value', '');
  84. }));
  85. case LIST_EDITOR_ADD_SUCCESS:
  86. return state.updateIn(['accounts', 'items'], list => list.unshift(action.accountId));
  87. case LIST_EDITOR_REMOVE_SUCCESS:
  88. return state.updateIn(['accounts', 'items'], list => list.filterNot(item => item === action.accountId));
  89. default:
  90. return state;
  91. }
  92. };