123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import Chat from "../components/chatWindow";
- import ChatsList from "../components/chatsList";
- import { useEffect } from "react";
- import io from 'socket.io-client';
- import { connect } from "react-redux";
- import { actionAddChat, actionAddChatBack, actionAddMSG, actionAddMSGBack, actionEditChat, actionEditMSG, actionFullEditChat, actionFullGetChats, actionFullGetMessages, actionUploadFile, actionUserSearch } from "../actions";
- import ConnectChatForm from "../components/chatForm";
- import { ChatEditForm } from "../components/chatEditForm";
- //все в куче
- const Main = ({ match: { params: { _id } }, userID, chats, getChat, getMessages, addChat, editChat, addMSG, editMSG, sendMSG, search, userSearch, changeChat, addFile }) => {
- let chat_id = _id.split(".")[1]
- useEffect(() => {
- getChat(userID)
- console.log("монтируем")
- 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('msg', msg => {
- console.log("это пришло по сокету (сообщение)", msg)
- if (chats && chats[msg.chat._id] && chats[msg.chat._id]["messages"].length) {
- let check = chats[msg.chat._id]["messages"].filter(({ _id }) => _id === msg._id).length
- console.log(check)
- if (check) {
- editMSG(msg.chat._id, msg._id, msg.text, msg.media)
- } else {
- addMSG(msg.chat._id, msg._id, msg.text, msg.createdAt, msg.owner, msg.media)
- }
- }
- })
- socket.on('chat', chat => {
- console.log("это пришло по сокету (чат)", chat)
- let check = Object.keys(chats).filter(id => id === chat._id).length
- console.log(check)
- if (check) {
- editChat(chat._id, chat.title, chat.avatar, chat.members)
- } else {
- addChat(chat._id, chat.title, chat.createdAt, chat.lastModified, chat.owner, chat.avatar, chat.messages, chat.members)
- }
- })
- return () => {
- console.log("размонтировали")
- }
- }, [])
- useEffect(() => {
- chat_id && getMessages(chat_id)
- }, [_id])
- return (
- <main>
- <ChatsList chats={chats} />
- {_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} />}
- {chats[chat_id] && <ChatEditForm chat_id={chat_id}
- chat={chats[chat_id]}
- searchState={search}
- onUserSearch={userSearch}
- onChangeChat={changeChat}
- onUpload={addFile} />}
- </main>
- )
- }
- export const ConnectMain = connect(state => ({
- userID: state.auth.payload.sub.id, chats: state.chat,
- search: state.promise?.userSearch?.payload?.data?.UserFind
- }),
- {
- getChat: actionFullGetChats,
- getMessages: actionFullGetMessages,
- addChat: actionAddChat,
- editChat: actionEditChat,
- addMSG: actionAddMSG,
- editMSG: actionEditMSG,
- sendMSG: actionAddMSGBack,
- userSearch: actionUserSearch,
- newChat: actionAddChatBack,
- changeChat: actionFullEditChat,
- addFile: actionUploadFile
- })(Main)
|