feedReducer.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { actionClearPromiseForName } from '../../../actions'
  2. // import { actionClearDataUserType } from '../profileUserPage/profileUserReducer'
  3. // import { actionProfilePageDataTypeUser } from '../profileUserPage/profileUserReducer'
  4. export const feedReducer = (
  5. state = {},
  6. { skip, type, newPosts = [], postId, postsFeed, postsFeedCount,newPostsFeedCount, newResult },
  7. ) => {
  8. const types = {
  9. 'ADD-POSTS': () => {
  10. return {
  11. ...state,
  12. postsFeed: state?.postsFeed ? [...state.postsFeed, ...newPosts] : [...newPosts],
  13. // postsFeed: [...postsFeed || [], ...newPosts],
  14. postsFeedCount: postsFeedCount ? postsFeedCount : newPostsFeedCount
  15. // postsFeed: postsFeed ? [...postsFeed, ...newPosts] : [...newPosts],
  16. }
  17. },
  18. 'COUNT': () => {
  19. return {
  20. ...state,
  21. postsFeedCount: postsFeedCount,
  22. }
  23. },
  24. 'POSTS': () => {
  25. return {
  26. ...state,
  27. postsFeed: postsFeed ,
  28. }
  29. },
  30. 'CLEAR-POSTS': () => ({
  31. postsFeed: [],
  32. postsFeedCount: 0,
  33. }),
  34. 'ADD-LIKE-POSTS': () => ({
  35. ...state,
  36. postsFeed: postsFeed?.map((p) =>
  37. p._id === postId ? (p = { ...p, likes: [...newResult] }) : p,
  38. ),
  39. }),
  40. 'DELETE-LIKE-POSTS': () => ({
  41. ...state,
  42. postsFeed: postsFeed?.map((p) =>
  43. p._id === postId ? (p = { ...p, likes: [...newResult] }) : p,
  44. ),
  45. }),
  46. 'ADD-COMMENT-POSTS': () => ({
  47. ...state,
  48. postsFeed: postsFeed?.map((p) =>
  49. p._id === postId ? (p = { ...p, comments: [...newResult] }) : p,
  50. ),
  51. }),
  52. }
  53. if (type in types) {
  54. return types[type]()
  55. }
  56. return state
  57. }
  58. // export const actionFullClearFeedPosts = () => (dispatch) => {
  59. // return dispatch(actionClearFeedPosts())
  60. // }
  61. export const actionClearFeedPosts = () =>
  62. ({ type: 'CLEAR-POSTS' })
  63. //type
  64. export const actionAddLikePostInTape = (postId) =>
  65. ({ type: 'ADD-LIKE-POSTS', postId })
  66. export const actionDeleteLikePostInTape = (likeId, postId) => ({
  67. type: 'DELETE-LIKE-POSTS',
  68. likeId,
  69. postId,
  70. })
  71. export const actionAddCommentPostInTape = (postId, newResult) => ({
  72. type: 'ADD-COMMENT-POSTS',
  73. postId,
  74. newResult,
  75. })
  76. export const actionFeedType = (newPosts, newPostsFeedCount) =>
  77. ({ type: 'ADD-POSTS', newPosts,newPostsFeedCount })
  78. export const actionFeedTypeCount = (postsFeedCount) =>
  79. ({ type: 'COUNT', postsFeedCount })