Explorar o código

fix file sending reducer

serg1557733 hai 1 ano
pai
achega
e9d57662c6

+ 0 - 2
backend/app.js

@@ -124,8 +124,6 @@ app.post('/avatar', async (req, res) =>  {
 })
 
 app.post('/files', async (req, res) =>  {
-    console.log(req)
-
     if (!req.files || Object.keys(req.files).length === 0) {
         return res.status(400).json('No files were uploaded.');
     }  

+ 0 - 1
frontend/src/components/chatPage/messageForm/MessegaForm.jsx

@@ -121,7 +121,6 @@ export const MessageForm = () => {
                             <div style={{'display': 'flex', 'alignItems': 'center'}} >
 
                                 <a href={SERVER_URL + '/' +item.file} download> 
-                                {console.log(SERVER_URL + '/' +item.file)}
                                     <Button
                                         variant="contained" 
                                         component="label"

+ 24 - 20
frontend/src/reducers/messageReducer.js

@@ -34,7 +34,7 @@ export const editMessageHandler = (state, data) => {
 };
 
 export const fileMessage = createAsyncThunk(
-    'messageReducer/fileMessage',
+    'messageReducer/fileMessageStatus',
     async (files) => {
         const token = localStorage.getItem('token')
         try {
@@ -48,25 +48,24 @@ export const fileMessage = createAsyncThunk(
             }
             formData.append('token', token)
             const response = await axios.post(POST_FILES_URL, formData,{
-                // onUploadProgress: (progress) => {
-                //     const {loaded, total} = progress;
-                //     const persentage = Math.floor(loaded * 100 / total);
-                // },
+                onUploadProgress: (progress) => {
+                    const {loaded, total} = progress;
+                    const loadStatus = Math.floor(loaded * 100 / total);
+                },
                     headers: {
                       "Content-type": "multipart/form-data",
                     }
-                  });
-                  
+                  })
+                  ;
             return await response;
             
         } catch (err) {
-            console.log('error messageReducer thunk', err)
+            console.log('Error messageReducer thunk', err)
                 return err?.message
             
             }
         })
 
-
         
 const messageReducerSlice = createSlice({
     name: 'messageReducer',
@@ -84,18 +83,23 @@ const messageReducerSlice = createSlice({
            
         },
         sendMessage: (state, action) => sendMessageToSocket(state, action.payload),
-        clearMessage: (state) => {state.message = ''},
-        extraReducers: (bilder) => 
-            bilder
-            .addCase(fileMessage.fulfilled, (state, action) => {
-                state.files = action.payload.data?.files
-                
-        })
-            .addCase(fileMessage.rejected, (state, action) => {
-                console.log('post file rejected', action.payload)
-        })
-        
+        clearMessage: (state) => {state.message = ''}
+
     },
+    extraReducers: (bilder) => {
+    bilder
+    .addCase(fileMessage.fulfilled, (state, action) => {
+        console.log('send', action)
+        state.files = action.payload.data?.files
+            
+    })  
+    .addCase(fileMessage.pending, (state, action) => {
+        state.persentage = action.percentage;
+        console.log('pending', action)
+    })
+    .addCase(fileMessage.rejected, (state, action) => {
+            console.log('post file rejected', action.payload)
+    })}
 });
 
 const {actions, reducer} = messageReducerSlice;