Parcourir la source

Update function addAnswers for tree-comments

LenDoc il y a 2 ans
Parent
commit
a9b1ce2da6
2 fichiers modifiés avec 13 ajouts et 17 suppressions
  1. 13 12
      src/redux/reducers/postReducer.js
  2. 0 5
      src/redux/reducers/userProfileReducer.js

+ 13 - 12
src/redux/reducers/postReducer.js

@@ -1,26 +1,27 @@
 import { PlusOutlined } from '@ant-design/icons'
 import { actionOnePost } from '../../actions'
 
-const addAnswers = (comments, id, newResult, field) =>
+const addAnswers = (comments, commentId, newResult) =>
 comments.map(comment => {
-    if (comment._id === id) {
-        return { ...comment, [field]: newResult }
-    } else if (comment?.answers?.length) {
+  if (comment._id === commentId)
+  {
+        return { ...comment, 'answers': newResult }
+  }
+  else if (comment?.answers?.length) {
         return {
             ...comment,
-            answers: addAnswers(comment.answers, id, newResult, field)
+            answers: addAnswers(comment.answers, commentId, newResult)
         }
-    } else {
+  }
+  else {
         return { ...comment }
-    }
+  }
+  
 })
 
-
-
-
 export const postReducer = (
     state = {},
-    { skip, type, newPosts = [], onePost,newResult, commentId, answers,postsFeed, postsFeedCount },
+    { type, onePost,newResult, commentId},
   ) => {
   const types = {
     'POST': () => {
@@ -54,7 +55,7 @@ export const postReducer = (
       onePost: ({
         ...state?.onePost,
         comments:  addAnswers(state.onePost.comments,
-          commentId, newResult, 'answers')
+          commentId, newResult)
       }),
     })
   

+ 0 - 5
src/redux/reducers/userProfileReducer.js

@@ -8,7 +8,6 @@ export const userProfileReducer = (
         return {
           ...state,
           aboutUser,
-          // allPosts,
         }
       },
 
@@ -16,17 +15,13 @@ export const userProfileReducer = (
         return {
           ...state,
           allPosts,
-          // allPosts,
         }
       },
       'ADD-USER-POSTS': () => {
         return {
           ...state,
           allPosts: state?.allPosts ? [...state.allPosts, ...newPosts] : [...newPosts],
-          // postsFeed: [...postsFeed || [], ...newPosts],
           countPosts: countPosts ? countPosts : newPostsCount
-      
-          // allPosts,
         }
       },