feedReducer.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 actionAllClearFeed = () => async (dispatch) => {
  62. Promise.all([
  63. await dispatch(actionClearPromiseForName('postsFeed')),
  64. await dispatch(actionClearPromiseForName('postsFeedCount')),
  65. await dispatch(actionClearFeedPosts())
  66. ])
  67. }
  68. export const actionClearFeedPosts = () =>
  69. ({ type: 'CLEAR-POSTS' })
  70. //type
  71. export const actionAddLikePostInTape = (postId) =>
  72. ({ type: 'ADD-LIKE-POSTS', postId })
  73. export const actionDeleteLikePostInTape = (likeId, postId) => ({
  74. type: 'DELETE-LIKE-POSTS',
  75. likeId,
  76. postId,
  77. })
  78. export const actionAddCommentPostInTape = (postId, newResult) => ({
  79. type: 'ADD-COMMENT-POSTS',
  80. postId,
  81. newResult,
  82. })
  83. export const actionFeedType = (newPosts, newPostsFeedCount) =>
  84. ({ type: 'ADD-POSTS', newPosts,newPostsFeedCount })
  85. export const actionFeedTypeCount = (postsFeedCount) =>
  86. ({ type: 'COUNT', postsFeedCount })