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.

57 lines
2.0 KiB

  1. import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
  2. import {
  3. PINNED_ACCOUNTS_EDITOR_RESET,
  4. PINNED_ACCOUNTS_FETCH_REQUEST,
  5. PINNED_ACCOUNTS_FETCH_SUCCESS,
  6. PINNED_ACCOUNTS_FETCH_FAIL,
  7. PINNED_ACCOUNTS_EDITOR_SUGGESTIONS_READY,
  8. PINNED_ACCOUNTS_EDITOR_SUGGESTIONS_CLEAR,
  9. PINNED_ACCOUNTS_EDITOR_SUGGESTIONS_CHANGE,
  10. ACCOUNT_PIN_SUCCESS,
  11. ACCOUNT_UNPIN_SUCCESS,
  12. } from '../actions/accounts';
  13. const initialState = ImmutableMap({
  14. accounts: ImmutableMap({
  15. items: ImmutableList(),
  16. loaded: false,
  17. isLoading: false,
  18. }),
  19. suggestions: ImmutableMap({
  20. value: '',
  21. items: ImmutableList(),
  22. }),
  23. });
  24. export default function listEditorReducer(state = initialState, action) {
  25. switch(action.type) {
  26. case PINNED_ACCOUNTS_EDITOR_RESET:
  27. return initialState;
  28. case PINNED_ACCOUNTS_FETCH_REQUEST:
  29. return state.setIn(['accounts', 'isLoading'], true);
  30. case PINNED_ACCOUNTS_FETCH_FAIL:
  31. return state.setIn(['accounts', 'isLoading'], false);
  32. case PINNED_ACCOUNTS_FETCH_SUCCESS:
  33. return state.update('accounts', accounts => accounts.withMutations(map => {
  34. map.set('isLoading', false);
  35. map.set('loaded', true);
  36. map.set('items', ImmutableList(action.accounts.map(item => item.id)));
  37. }));
  38. case PINNED_ACCOUNTS_EDITOR_SUGGESTIONS_CHANGE:
  39. return state.setIn(['suggestions', 'value'], action.value);
  40. case PINNED_ACCOUNTS_EDITOR_SUGGESTIONS_READY:
  41. return state.setIn(['suggestions', 'items'], ImmutableList(action.accounts.map(item => item.id)));
  42. case PINNED_ACCOUNTS_EDITOR_SUGGESTIONS_CLEAR:
  43. return state.update('suggestions', suggestions => suggestions.withMutations(map => {
  44. map.set('items', ImmutableList());
  45. map.set('value', '');
  46. }));
  47. case ACCOUNT_PIN_SUCCESS:
  48. return state.updateIn(['accounts', 'items'], list => list.unshift(action.relationship.id));
  49. case ACCOUNT_UNPIN_SUCCESS:
  50. return state.updateIn(['accounts', 'items'], list => list.filterNot(item => item === action.relationship.id));
  51. default:
  52. return state;
  53. }
  54. };