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 (
<>
{_id === "chat" && }
{chats[chat_id] &&
}
{isEdit && chats[chat_id] && }
>
)
}
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)