statuses.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import {
  2. REBLOG_REQUEST,
  3. REBLOG_FAIL,
  4. FAVOURITE_REQUEST,
  5. FAVOURITE_FAIL,
  6. UNFAVOURITE_SUCCESS,
  7. BOOKMARK_REQUEST,
  8. BOOKMARK_FAIL,
  9. } from '../actions/interactions';
  10. import {
  11. STATUS_MUTE_SUCCESS,
  12. STATUS_UNMUTE_SUCCESS,
  13. STATUS_REVEAL,
  14. STATUS_HIDE,
  15. STATUS_COLLAPSE,
  16. STATUS_TRANSLATE_SUCCESS,
  17. STATUS_TRANSLATE_UNDO,
  18. STATUS_FETCH_REQUEST,
  19. STATUS_FETCH_FAIL,
  20. } from '../actions/statuses';
  21. import { TIMELINE_DELETE } from '../actions/timelines';
  22. import { STATUS_IMPORT, STATUSES_IMPORT } from '../actions/importer';
  23. import { Map as ImmutableMap, fromJS } from 'immutable';
  24. const importStatus = (state, status) => state.set(status.id, fromJS(status));
  25. const importStatuses = (state, statuses) =>
  26. state.withMutations(mutable => statuses.forEach(status => importStatus(mutable, status)));
  27. const deleteStatus = (state, id, references) => {
  28. references.forEach(ref => {
  29. state = deleteStatus(state, ref, []);
  30. });
  31. return state.delete(id);
  32. };
  33. const initialState = ImmutableMap();
  34. export default function statuses(state = initialState, action) {
  35. switch(action.type) {
  36. case STATUS_FETCH_REQUEST:
  37. return state.setIn([action.id, 'isLoading'], true);
  38. case STATUS_FETCH_FAIL:
  39. return state.delete(action.id);
  40. case STATUS_IMPORT:
  41. return importStatus(state, action.status);
  42. case STATUSES_IMPORT:
  43. return importStatuses(state, action.statuses);
  44. case FAVOURITE_REQUEST:
  45. return state.setIn([action.status.get('id'), 'favourited'], true);
  46. case UNFAVOURITE_SUCCESS:
  47. return state.updateIn([action.status.get('id'), 'favourites_count'], x => Math.max(0, x - 1));
  48. case FAVOURITE_FAIL:
  49. return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'favourited'], false);
  50. case BOOKMARK_REQUEST:
  51. return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'bookmarked'], true);
  52. case BOOKMARK_FAIL:
  53. return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'bookmarked'], false);
  54. case REBLOG_REQUEST:
  55. return state.setIn([action.status.get('id'), 'reblogged'], true);
  56. case REBLOG_FAIL:
  57. return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'reblogged'], false);
  58. case STATUS_MUTE_SUCCESS:
  59. return state.setIn([action.id, 'muted'], true);
  60. case STATUS_UNMUTE_SUCCESS:
  61. return state.setIn([action.id, 'muted'], false);
  62. case STATUS_REVEAL:
  63. return state.withMutations(map => {
  64. action.ids.forEach(id => {
  65. if (!(state.get(id) === undefined)) {
  66. map.setIn([id, 'hidden'], false);
  67. }
  68. });
  69. });
  70. case STATUS_HIDE:
  71. return state.withMutations(map => {
  72. action.ids.forEach(id => {
  73. if (!(state.get(id) === undefined)) {
  74. map.setIn([id, 'hidden'], true);
  75. }
  76. });
  77. });
  78. case STATUS_COLLAPSE:
  79. return state.setIn([action.id, 'collapsed'], action.isCollapsed);
  80. case TIMELINE_DELETE:
  81. return deleteStatus(state, action.id, action.references);
  82. case STATUS_TRANSLATE_SUCCESS:
  83. return state.setIn([action.id, 'translation'], fromJS(action.translation));
  84. case STATUS_TRANSLATE_UNDO:
  85. return state.deleteIn([action.id, 'translation']);
  86. default:
  87. return state;
  88. }
  89. };