postReducer.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { PlusOutlined } from '@ant-design/icons'
  2. import { actionOnePost } from '../../actions'
  3. const addAnswers = (comments, commentId, newResult) =>
  4. comments.map(comment => {
  5. if (comment._id === commentId)
  6. {
  7. return { ...comment, 'answers': newResult }
  8. }
  9. else if (comment?.answers?.length) {
  10. return {
  11. ...comment,
  12. answers: addAnswers(comment.answers, commentId, newResult)
  13. }
  14. }
  15. else {
  16. return { ...comment }
  17. }
  18. })
  19. export const postReducer = (
  20. state = {},
  21. { type, onePost,newResult, commentId},
  22. ) => {
  23. const types = {
  24. 'POST': () => {
  25. return {
  26. ...state,
  27. onePost,
  28. }
  29. },
  30. 'CLEAR_ONE_POST': () => {
  31. return {
  32. ...state,
  33. onePost: {},
  34. }
  35. },
  36. 'CHANGE_LIKE': () => {
  37. return {
  38. ...state,
  39. onePost: ({ ...state?.onePost, likes: [...newResult] })
  40. }
  41. },
  42. 'ADD_COMMENT': () => {
  43. return {
  44. ...state,
  45. onePost: ({ ...state?.onePost, comments: [...newResult] })
  46. }
  47. },
  48. 'ANSWERS-COMMENT': () => ({
  49. ...state,
  50. onePost: ({
  51. ...state?.onePost,
  52. comments: addAnswers(state.onePost.comments,
  53. commentId, newResult)
  54. }),
  55. })
  56. }
  57. if (type in types) {
  58. return types[type]()
  59. }
  60. return state
  61. }