postFeed-reducer.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import React from 'react'
  2. export const postsFeedReducer = (state = {}, { type, postId, 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. }
  30. },
  31. 'ADD-POST-LIKE': () => {
  32. return {
  33. ...state,
  34. posts: Array.isArray(posts)
  35. ? posts.map(p => p._id === postId ? p = { ...p, likes: [...newResult] } : p)
  36. : { ...state.posts, likes: [...newResult] },
  37. }
  38. },
  39. 'REMOVE-POST-LIKE': () => {
  40. return {
  41. ...state,
  42. posts: Array.isArray(posts)
  43. ? posts.map(p => p._id === postId ? p = { ...p, likes: [...newResult] } : p)
  44. : { ...state.posts, likes: [...newResult] },
  45. }
  46. },
  47. 'ADD-COMMENT': () => {
  48. return {
  49. ...state,
  50. posts: posts.map(p => p._id === postId ? { ...p, comments: [...newResult] } : p)
  51. }
  52. },
  53. 'UPDATE-FOLLOWERS': () => {
  54. return {
  55. ...state,
  56. userData: { ...state.userData, followers: [...newResult] }
  57. }
  58. }
  59. }
  60. if (type in types) {
  61. return types[type]()
  62. }
  63. return state
  64. }