logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

list_editor.js (2852B)


  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. title: '',
  25. accounts: ImmutableMap({
  26. items: ImmutableList(),
  27. loaded: false,
  28. isLoading: false,
  29. }),
  30. suggestions: ImmutableMap({
  31. value: '',
  32. items: ImmutableList(),
  33. }),
  34. });
  35. export default function listEditorReducer(state = initialState, action) {
  36. switch(action.type) {
  37. case LIST_EDITOR_RESET:
  38. return initialState;
  39. case LIST_EDITOR_SETUP:
  40. return state.withMutations(map => {
  41. map.set('listId', action.list.get('id'));
  42. map.set('title', action.list.get('title'));
  43. map.set('isSubmitting', false);
  44. });
  45. case LIST_EDITOR_TITLE_CHANGE:
  46. return state.set('title', action.value);
  47. case LIST_CREATE_REQUEST:
  48. case LIST_UPDATE_REQUEST:
  49. return state.set('isSubmitting', true);
  50. case LIST_CREATE_FAIL:
  51. case LIST_UPDATE_FAIL:
  52. return state.set('isSubmitting', false);
  53. case LIST_CREATE_SUCCESS:
  54. case LIST_UPDATE_SUCCESS:
  55. return state.withMutations(map => {
  56. map.set('isSubmitting', false);
  57. map.set('listId', action.list.id);
  58. });
  59. case LIST_ACCOUNTS_FETCH_REQUEST:
  60. return state.setIn(['accounts', 'isLoading'], true);
  61. case LIST_ACCOUNTS_FETCH_FAIL:
  62. return state.setIn(['accounts', 'isLoading'], false);
  63. case LIST_ACCOUNTS_FETCH_SUCCESS:
  64. return state.update('accounts', accounts => accounts.withMutations(map => {
  65. map.set('isLoading', false);
  66. map.set('loaded', true);
  67. map.set('items', ImmutableList(action.accounts.map(item => item.id)));
  68. }));
  69. case LIST_EDITOR_SUGGESTIONS_CHANGE:
  70. return state.setIn(['suggestions', 'value'], action.value);
  71. case LIST_EDITOR_SUGGESTIONS_READY:
  72. return state.setIn(['suggestions', 'items'], ImmutableList(action.accounts.map(item => item.id)));
  73. case LIST_EDITOR_SUGGESTIONS_CLEAR:
  74. return state.update('suggestions', suggestions => suggestions.withMutations(map => {
  75. map.set('items', ImmutableList());
  76. map.set('value', '');
  77. }));
  78. case LIST_EDITOR_ADD_SUCCESS:
  79. return state.updateIn(['accounts', 'items'], list => list.unshift(action.accountId));
  80. case LIST_EDITOR_REMOVE_SUCCESS:
  81. return state.updateIn(['accounts', 'items'], list => list.filterNot(item => item === action.accountId));
  82. default:
  83. return state;
  84. }
  85. };