|
@@ -12,8 +12,15 @@ import {
|
|
|
actionChatLeft
|
|
|
} from './chatsReducer'
|
|
|
|
|
|
+import {
|
|
|
+ actionGetChatById,
|
|
|
+ actionFullLogout
|
|
|
+} from '../actions'
|
|
|
+
|
|
|
+import msgSound from '../assets/msgSound.ogg'
|
|
|
// const { io } = require("socket.io-client");
|
|
|
|
|
|
+
|
|
|
export const store = createStore ( combineReducers({
|
|
|
// promise: localStoredReducer(promiseReducer, 'promise'),
|
|
|
// auth: localStoredReducer(authReducer, 'auth'),
|
|
@@ -30,29 +37,49 @@ store.dispatch(actionAboutMe())
|
|
|
store.subscribe(() => console.log(store.getState()))
|
|
|
|
|
|
|
|
|
+///////////////////////////////////////////////////////////////////
|
|
|
+
|
|
|
+export const socket = window.io("ws://chat.fs.a-level.com.ua")
|
|
|
+
|
|
|
+socket.on('jwt_ok', (data) => console.log(data))
|
|
|
+socket.on('jwt_fail', (error) => {
|
|
|
+ console.log(error)
|
|
|
+ store.dispatch(actionFullLogout())
|
|
|
+})
|
|
|
+
|
|
|
+socket.on('msg', async (msg) => {
|
|
|
+ console.log('пришло смс')
|
|
|
+
|
|
|
+ const state = store.getState()
|
|
|
+ const myId = state.auth.payload?.sub?.id
|
|
|
+ const ownerId = msg.owner?._id
|
|
|
+ if (myId !== ownerId) {
|
|
|
+ const inputMsgAudio = new Audio(msgSound)
|
|
|
+ inputMsgAudio.play()
|
|
|
+ }
|
|
|
+
|
|
|
+ const chatId = msg.chat?._id
|
|
|
+
|
|
|
+ await store.dispatch(actionMsgOne(msg))
|
|
|
|
|
|
-// export const socket = window.io("ws://chat.fs.a-level.com.ua")
|
|
|
+ if (chatId) {
|
|
|
+ let chatUpdated = await store.dispatch(actionGetChatById(chatId))
|
|
|
+ await store.dispatch(actionChatOne(chatUpdated))
|
|
|
+ }
|
|
|
+})
|
|
|
|
|
|
-// socket.on('jwt_ok', (data) => console.log(data))
|
|
|
-// socket.on('jwt_fail', (error) => console.log(error))
|
|
|
+socket.on('chat', (chat) => {
|
|
|
+ console.log('нас добавили в чат')
|
|
|
+ store.dispatch(actionChatOne(chat))
|
|
|
+})
|
|
|
|
|
|
-// socket.on('msg', (msg) => {
|
|
|
-// console.log('пришло смс')
|
|
|
-// store.dispatch(actionMsgOne(msg))
|
|
|
-// })
|
|
|
-// socket.on('chat', (chat) => {
|
|
|
-// store.dispatch(actionChatOne(chat))
|
|
|
-// })
|
|
|
-// socket.on('chat_left', (chat) => {
|
|
|
-// store.dispatch(actionChatLeft(chat))
|
|
|
-// })
|
|
|
+socket.on('chat_left', (chat) => {
|
|
|
+ console.log('нас выкинули из чата')
|
|
|
+ store.dispatch(actionChatLeft(chat))
|
|
|
+})
|
|
|
|
|
|
|
|
|
|
|
|
-// if (localStorage.authToken) {
|
|
|
-// socket.emit('jwt', localStorage.authToken)
|
|
|
-// }
|
|
|
-// socket.disconnect()
|
|
|
|
|
|
// combineReducers({cart: localStoredReducer(cartReducer, 'cart'),
|
|
|
// promise: localStoredReducer(promiseReducer, 'promise') })
|