postFeed-reducer.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import React from 'react'
  2. export const postsFeedReducer = (state = {}, { type, findId, newResult, userData = {}, count = null }) => {
  3. const { posts } = state
  4. const types = {
  5. 'ADD-POSTS-FEED': () => {
  6. return {
  7. ...state,
  8. posts: !!posts ? [...posts, ...newResult] : [...newResult],
  9. count
  10. }
  11. },
  12. 'GET-POST': () => {
  13. return { ...state, posts: { ...newResult } }
  14. },
  15. 'ADD-PROFILE-DATA': () => {
  16. return {
  17. ...state,
  18. posts: !!posts ? [...posts, ...newResult] : [...newResult],
  19. userData,
  20. count
  21. }
  22. },
  23. 'REMOVE-POSTS-FEED': () => {
  24. return {
  25. ...state,
  26. posts: [],
  27. userData: {},
  28. count: 0,
  29. subComments: {}
  30. }
  31. },
  32. 'ADD-POST-LIKE': () => {
  33. return {
  34. ...state,
  35. posts: Array.isArray(posts)
  36. ? posts.map(p => p._id === findId ? p = { ...p, likes: [...newResult] } : p)
  37. : { ...state.posts, likes: [...newResult] },
  38. }
  39. },
  40. 'REMOVE-POST-LIKE': () => {
  41. return {
  42. ...state,
  43. posts: Array.isArray(posts)
  44. ? posts.map(p => p._id === findId ? p = { ...p, likes: [...newResult] } : p)
  45. : { ...state.posts, likes: [...newResult] },
  46. }
  47. },
  48. 'ADD-COMMENT': () => {
  49. return {
  50. ...state,
  51. posts: { ...state.posts, comments: [...newResult] }
  52. }
  53. },
  54. 'UPDATE-SUBCOMMENT': () => {
  55. return {
  56. ...state,
  57. subComments: { ...state?.subComments, ...{ ['subComments#' + findId]: [...newResult] } }
  58. }
  59. },
  60. 'ADD-LIKE-COMMENT': () => {
  61. return {
  62. ...state,
  63. posts: {
  64. ...state.posts,
  65. comments: posts.comments.map(c => c._id === findId ? c = { ...c, likes: [...newResult] } : c)
  66. }
  67. }
  68. },
  69. 'REMOVE-LIKE-COMMENT': () => {
  70. return {
  71. ...state,
  72. posts: {
  73. ...state.posts,
  74. comments: posts.comments.map(c => c._id === findId ? c = { ...c, likes: [...newResult] } : c)
  75. }
  76. }
  77. },
  78. 'UPDATE-FOLLOWERS': () => {
  79. return {
  80. ...state,
  81. userData: { ...state.userData, followers: [...newResult] }
  82. }
  83. }
  84. }
  85. if (type in types) {
  86. return types[type]()
  87. }
  88. return state
  89. }