Browse Source

reducer fixed

Ivar 2 years ago
parent
commit
69b409804c
1 changed files with 7 additions and 5 deletions
  1. 7 5
      src/reducers/chatsReducer.js

+ 7 - 5
src/reducers/chatsReducer.js

@@ -57,13 +57,15 @@
         
           const chatId = payload[0]?.chat?._id
 
-          // console.log(chatId, state[chatId]?.messages, payload)
-
           const msgState = state[chatId]?.messages || []
           
-          for (const newMsg of payload || []) {    
-              if (!msgState.find(oldMsg => oldMsg._id === newMsg._id)) {
-                msgState.push(newMsg)
+          for (const newMsg of payload || []) {   
+              const currIndex = msgState.findIndex(oldMsg => oldMsg._id === newMsg._id)
+              
+              if (currIndex === -1) {
+                msgState.push(newMsg)                
+              } else {
+                msgState[currIndex] = newMsg
               }
           }