postReducer.js 944 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { addAnswers } from "../../actions/types/commentTypes"
  2. export const postReducer = (
  3. state = {},
  4. { type, onePost,newResult, commentId},
  5. ) => {
  6. const types = {
  7. 'POST': () => {
  8. return {
  9. ...state,
  10. onePost,
  11. }
  12. },
  13. 'CLEAR_ONE_POST': () => {
  14. return {
  15. ...state,
  16. onePost: {},
  17. }
  18. },
  19. 'CHANGE_LIKE': () => {
  20. return {
  21. ...state,
  22. onePost: ({ ...state?.onePost, likes: [...newResult] })
  23. }
  24. },
  25. 'ADD_COMMENT': () => {
  26. return {
  27. ...state,
  28. onePost: ({ ...state?.onePost, comments: [...newResult] })
  29. }
  30. },
  31. 'ANSWERS-COMMENT': () => ({
  32. ...state,
  33. onePost: ({
  34. ...state?.onePost,
  35. comments: addAnswers(state?.onePost?.comments,
  36. commentId, newResult)
  37. }),
  38. })
  39. }
  40. if (type in types) {
  41. return types[type]()
  42. }
  43. return state
  44. }