1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- import { useEffect, useState } from "react";
- import io from 'socket.io-client';
- import { connect } from "react-redux";
- import Chat from "../components/chatWindow";
- import ChatsList from "../components/chatsList";
- import ConnectChatForm from "../components/chatForm";
- import ConnectChatEditForm from "../components/chatEditForm";
- import { ConnectProfile } from "../components/profileBar";
- import {
- actionAddChat, actionAddChatBack, actionAddMSG, actionAddMSGBack,
- actionEditMSGback, actionFullGetChats, actionFullGetMessages,
- actionGetUserInfo, actionUploadFile
- } from "../actions";
- const Main = ({ match: { params: { _id } }, userID, chats, getUserInfo, getChat, getMessages, addChat, addMSG, editMSG, sendMSG, addFile }) => {
- let [isEdit, setIsEdit] = useState(false)
- let onInfoHandler = () => {
- setIsEdit(!isEdit)
- }
- let chat_id = _id.split(".")[1]
- useEffect(() => {
- getUserInfo(userID)
- getChat(userID)
- const socket = io("ws://chat.fs.a-level.com.ua")
- socket.emit("jwt", localStorage.authToken)
- socket.on("jwt_ok", data => console.log("С JWT все норм", data))
- socket.on("jwt_fail", error => console.log("С JWT траблы", error))
- socket.on("connect", () => {
- // getChat(userID)
- console.log("Законектились, сокет: ", socket.id)
- });
- socket.on("connect_error", error => console.log("ошибка конекта", error));
- socket.on("reconnect_attempt", () => console.log("пробуем реконектнуть"));
- socket.on("disconnect", () => console.log("дисконект", socket.id));
- socket.on('chat', chat => {
- addChat(chat._id, chat.title, chat.createdAt, chat.lastModified, chat.owner, chat.avatar, chat.messages, chat.members)
- })
- socket.on('msg', msg => {
- addMSG(true, msg.chat._id, msg._id, msg.text, msg.createdAt, msg.owner, msg.media, msg.replyTo)
- })
- }, [])
- useEffect(() => {
- chat_id && getMessages(chat_id, 0)
- }, [_id, chat_id, getMessages])
- return (
- <>
- <ConnectProfile />
- <main>
- <ChatsList />
- {_id === "chat" && <ConnectChatForm />}
- {chats[chat_id] &&
- <Chat chat_id={chat_id}
- chat_title={chats[chat_id].title}
- chat_avatar={chats[chat_id].avatar?.url}
- user_id={userID}
- messages={chats[chat_id].messages || []}
- onUpload={addFile}
- onSend={sendMSG}
- onMSGEdit={editMSG}
- onScrollTopComplete={getMessages}
- onInfo={onInfoHandler} />}
- {isEdit && chats[chat_id] && <ConnectChatEditForm chat_id={chat_id} chat={chats[chat_id]} />}
- </main>
- </>
- )
- }
- export const ConnectMain = connect(state => ({
- userID: state.auth.payload.sub.id,
- chats: state.chat,
- }),
- {
- getUserInfo: actionGetUserInfo,
- getChat: actionFullGetChats,
- getMessages: actionFullGetMessages,
- addChat: actionAddChat,
- addMSG: actionAddMSG,
- editMSG: actionEditMSGback,
- sendMSG: actionAddMSGBack,
- newChat: actionAddChatBack,
- addFile: actionUploadFile
- })(Main)
|