Przeglądaj źródła

added socket actions

Ivar 2 lat temu
rodzic
commit
470246bc4b

+ 3 - 14
src/actions/chatsActions.js

@@ -49,17 +49,7 @@ const actionUpdateChatAvatar = (mediaId, chatId) => (
     actionPromise('uploadFile', gql(`mutation uploadFile($media: MediaInput) {  
         MediaUpsert(media: $media) {
             _id
-            url
-            type
-            originalFileName     
-            owner {
-               _id
-               login
-               nick
-               avatar {
-                  url
-               }
-            }
+            url   
         }
     }`, { media: { _id: mediaId, chatAvatars: {_id: chatId} } }
     ))
@@ -68,13 +58,12 @@ const actionUpdateChatAvatar = (mediaId, chatId) => (
 
 export const actionSetChatInfo = (name, file, title, members, chatId) => (
    async (dispatch) => {
-
       let chat = await dispatch(actionUpdateChat(title, members, chatId))         
 
       if (file && chat._id) {
         let fileObj = await dispatch(actionUploadFile(name, file))
-         console.log(fileObj)
-         await dispatch(actionUpdateChatAvatar(fileObj?._id, chat._id))
+         let chatAvatar = await dispatch(actionUpdateChatAvatar(fileObj?._id, chat._id))
+         await dispatch(actionChatOne({_id: chat._id, avatar: chatAvatar}))
       } 
    }
 )

+ 2 - 1
src/actions/index.js

@@ -19,6 +19,7 @@ import {
    actionMsgsCount,
    actionSendMsg,
    actionGetAllLastMsg,
+   actionGetMsgById,
 
    actionFullMsgsByChat,
 } from './msgActions'
@@ -31,7 +32,6 @@ import {
 
 
 
-
 export {
    actionFullLogout,
    actionFullLogin, 
@@ -52,6 +52,7 @@ export {
    actionMsgsCount,
    actionSendMsg,
    actionGetAllLastMsg,
+   actionGetMsgById,
 
    actionFullMsgsByChat,
 }

+ 47 - 0
src/actions/msgActions.js

@@ -160,6 +160,53 @@ export const actionMsgsCount = (chatId) => (
 
 
 
+
+export const actionGetMsgById = (msgId) => (
+   actionPromise('msgById', gql(`query msgById($q: String) {
+      MessageFindOne (query: $q){
+         _id
+         createdAt
+         owner {
+            _id
+            login
+            nick
+            avatar {
+               url
+            }
+         }
+         text
+         chat {
+            _id
+         }
+         media {
+            _id
+            url
+            type
+            originalFileName            
+         }
+
+         forwardWith {
+            _id
+         }
+         replies {
+            _id
+         }
+
+         replyTo {
+            _id
+         }
+         forwarded {
+            _id
+         }
+      }     
+   }`, { 
+         q: JSON.stringify([ { _id: msgId } ])
+      }
+   ))
+)
+
+
+
 const actionUpdateMsg = (chatId, text, media, msgId) => (
    actionPromise('updateMsg', gql(`mutation updateMsg($msg: MessageInput) {
       MessageUpsert(message: $msg) {

+ 14 - 6
src/reducers/store.js

@@ -9,12 +9,13 @@ import { actionAboutMe } from './findUserActions'
 import { 
    actionMsgOne, 
    actionChatOne, 
-   actionChatLeft 
+   actionChatLeft
 } from './chatsReducer'
 
 import {   
+   actionGetMsgById,
    actionGetChatById, 
-   actionFullLogout
+   actionFullLogout,
 } from '../actions'
 
 import msgSound from '../assets/msgSound.ogg'
@@ -56,7 +57,7 @@ function playAudio(audio) {
 
 
 socket.on('msg', async (msg) => { 
-   console.log('пришло смс')
+   console.log('пришло смс', msg)
    
    const state = store.getState()
    const myId = state.auth.payload?.sub?.id
@@ -69,11 +70,14 @@ socket.on('msg', async (msg) => {
 
    await store.dispatch(actionMsgOne(msg)) 
 
+   let msgFull = await store.dispatch(actionGetMsgById(msg._id))
+   await store.dispatch(actionMsgOne(msgFull)) 
+
    let chatUpdated = await store.dispatch(actionGetChatById(chatId))
    await store.dispatch(actionChatOne(chatUpdated))
 })
 
-socket.on('chat', (chat) => { 
+socket.on('chat', async (chat) => { 
    console.log('нас добавили в чат')
 
    const state = store.getState()
@@ -85,9 +89,12 @@ socket.on('chat', (chat) => {
    }
 
    store.dispatch(actionChatOne(chat)) 
+
+   let chatFull = await store.dispatch(actionGetChatById(chat._id))
+   await store.dispatch(actionChatOne(chatFull))
 })
 
-socket.on('chat_left', (chat) => { 
+socket.on('chat_left', async (chat) => { 
    console.log('нас выкинули из чата')
 
    const state = store.getState()
@@ -100,7 +107,8 @@ socket.on('chat_left', (chat) => {
    }
    store.dispatch(actionChatOne(chat)) 
 
-
+   let chatFull = await store.dispatch(actionGetChatById(chat._id))
+   await store.dispatch(actionChatOne(chatFull))
 })