Browse Source

fix editing messages, fix array users back

serg1557733 1 year ago
parent
commit
159183f677

+ 17 - 9
backend/app.js

@@ -62,7 +62,6 @@ app.post('/login', async (req, res) => {
 
     try {
         const {userName,password} = req.body;
-        console.log(userName,password)
         if (!isValidUserName(userName)){
             return res.status(400).json({message: 'Invalid username'})
         }
@@ -206,19 +205,27 @@ app.post('/files', async (req, res) =>  {
 
 io.use( async (socket, next) => {
     const token = socket.handshake.auth.token; 
-    const sockets = await io.fetchSockets();
    
     if(!token) {
-        console.log('socket')
+        console.log('socket disconnect')
         socket.disconnect();
         return;
     }
-
+    
+    const sockets = await io.fetchSockets();
+    const socketUsers = [];
     const usersOnline = [];
     sockets.map((sock) => {
-        usersOnline.push(sock.user);
+        console.log(sock.user.id)
+        socketUsers.map(item => {
+            if (item.id !== sock.user) {
+                usersOnline.push(sock.user);
+            }
+        })
+        
     }) 
-
+    
+    console.log('fin arr', usersOnline)
 
    
     try {
@@ -231,7 +238,10 @@ io.use( async (socket, next) => {
             return;
         }
         socket.user = user;
-        const exist = sockets.find((current) => current.user.userName == socket.user.userName)
+        const exist = sockets.find(current => current.user.userName == socket.user.userName)
+
+        // console.log('exist ' , exist)
+
 
         if(exist) {  //&& !user.isAdmin  - add for two or more admins 
             console.log(exist.userName, 'exist twice entering...')   
@@ -293,7 +303,6 @@ io.on("connection", async (socket) => {
                 console.log('Message save to db error', error);   
             }
             const newMessages = await message.populate( {path:'user'})   
-            console.log(newMessages)
             io.emit('newmessage', newMessages);        
             // }
         // } 
@@ -378,7 +387,6 @@ io.on("connection", async (socket) => {
 
                 } catch (error) {
                     console.log(error)
-                    
                 }
             }) 
 

+ 3 - 0
frontend/src/components/chatPage/MessageEditorMenu.jsx.jsx

@@ -21,6 +21,9 @@ export const MessageEditorMenu = () => {
         <div>
             <button
              onClick={() => {
+                if (!message.message.length) {
+                    dispatch(deleteMessage({socket, messageId}))  
+                }
                 dispatch(editMessage({socket, editMessage: message, messageId }))
                 dispatch(editMessage({socket: null, editMessage: '', messageId: '' }))
                 

+ 4 - 2
frontend/src/components/chatPage/messageForm/MessegaForm.jsx

@@ -38,6 +38,9 @@ export const MessageForm = () => {
         if (!isEditing) {
             scrollToBottom((endMessages)) 
         }
+        if(newMessages.length > 0){
+            newMessages.map(i => console.log(i.userName === user.userName))
+        }
       }, [startMessages, newMessages]);
            
     const messages = startMessages.concat(newMessages)  
@@ -126,8 +129,7 @@ export const MessageForm = () => {
                                 <p style={{'marginLeft': '15px'}}  >{item.text}</p>  
                             </div>
                         : 
-                        <><p>{i}</p>  
-                            <p>{item.text}</p> </>
+                            <p>{item.text}</p>
                           
                            }
 

+ 1 - 6
frontend/src/reducers/messageReducer.js

@@ -21,21 +21,16 @@ export const sendMessageToSocket = (state, data) => {
     };
 
 export const deleteMessageHandler = (state, data) => {
-    data.socket.emit('deleteMessage', {messageId: data.messageId, token: data.socket.auth.token}); 
-              
-        
+    data.socket.emit('deleteMessage', {messageId: data.messageId, token: data.socket.auth.token});  
 };
 
     
 export const editMessageHandler = (state, data) => {
-   
     if(data.socket){
          data.socket.emit('editmessage', {messageNewText: data.editMessage.message, messageId: data.messageId, token: data.socket.auth.token}); //add backend functional later find by id and edit 
     }
 };
 
-
-
 export const fileMessage = createAsyncThunk(
     'messageReducer/fileMessage',
     async (files) => {