posts.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import postReducer, { PATCH_POST, hydrate as hydratePost } from 'misago/reducers/post';
  2. export const APPEND_POSTS = 'APPEND_POSTS';
  3. export const SELECT_POST = 'SELECT_POST';
  4. export const DESELECT_POST = 'DESELECT_POST';
  5. export const DESELECT_POSTS = 'DESELECT_POSTS';
  6. export const LOAD_POSTS = 'LOAD_POSTS';
  7. export const UNLOAD_POSTS = 'UNLOAD_POSTS';
  8. export const UPDATE_POSTS = 'UPDATE_POSTS';
  9. export function select(post) {
  10. return {
  11. type: SELECT_POST,
  12. post
  13. };
  14. }
  15. export function deselect(post) {
  16. return {
  17. type: DESELECT_POST,
  18. post
  19. };
  20. }
  21. export function deselectAll() {
  22. return {
  23. type: DESELECT_POSTS,
  24. };
  25. }
  26. export function hydrate(json) {
  27. return Object.assign({}, json, {
  28. results: json.results.map(hydratePost),
  29. isLoaded: true,
  30. isBusy: false,
  31. isSelected: false
  32. });
  33. }
  34. export function load(newState, hydrated=false) {
  35. return {
  36. type: LOAD_POSTS,
  37. state: hydrated ? newState : hydrate(newState)
  38. };
  39. }
  40. export function append(newState, hydrated=false) {
  41. return {
  42. type: APPEND_POSTS,
  43. state: hydrated ? newState : hydrate(newState)
  44. };
  45. }
  46. export function unload() {
  47. return {
  48. type: UNLOAD_POSTS
  49. };
  50. }
  51. export function update(newState) {
  52. return {
  53. type: UPDATE_POSTS,
  54. update: newState
  55. };
  56. }
  57. export default function posts(state={}, action=null) {
  58. switch (action.type) {
  59. case SELECT_POST:
  60. const selectedPosts = state.results.map((post) => {
  61. if (post.id == action.post.id) {
  62. return Object.assign({}, post, {
  63. isSelected: true
  64. });
  65. } else {
  66. return post;
  67. }
  68. });
  69. return Object.assign({}, state, {
  70. results: selectedPosts
  71. });
  72. case DESELECT_POST:
  73. const deseletedPosts = state.results.map((post) => {
  74. if (post.id == action.post.id) {
  75. return Object.assign({}, post, {
  76. isSelected: false
  77. });
  78. } else {
  79. return post;
  80. }
  81. });
  82. return Object.assign({}, state, {
  83. results: deseletedPosts
  84. });
  85. case DESELECT_POSTS:
  86. const deseletedAllPosts = state.results.map((post) => {
  87. return Object.assign({}, post, {
  88. isSelected: false
  89. });
  90. });
  91. return Object.assign({}, state, {
  92. results: deseletedAllPosts
  93. });
  94. case APPEND_POSTS:
  95. let results = state.results.slice();
  96. const resultsIds = state.results.map((post) => {
  97. return post.id;
  98. });
  99. action.state.results.map((post) => {
  100. if (resultsIds.indexOf(post.id) === -1) {
  101. results.push(post);
  102. }
  103. });
  104. return Object.assign({}, action.state, {
  105. results
  106. });
  107. case LOAD_POSTS:
  108. return action.state;
  109. case UNLOAD_POSTS:
  110. return Object.assign({}, state, {
  111. isLoaded: false,
  112. });
  113. case UPDATE_POSTS:
  114. return Object.assign({}, state, action.update);
  115. case PATCH_POST:
  116. const reducedPosts = state.results.map((post) => {
  117. return postReducer(post, action);
  118. });
  119. return Object.assign({}, state, {
  120. results: reducedPosts
  121. });
  122. default:
  123. return state;
  124. }
  125. }