posts.js 3.1 KB

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