main.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { useEffect, useState } from "react";
  2. import io from 'socket.io-client';
  3. import { connect } from "react-redux";
  4. import Chat from "../components/chatWindow";
  5. import ChatsList from "../components/chatsList";
  6. import ConnectChatForm from "../components/chatForm";
  7. import ConnectChatEditForm from "../components/chatEditForm";
  8. import { ConnectProfile } from "../components/profileBar";
  9. import { ChatBar } from "../components/chatBar";
  10. import {
  11. actionAddChat, actionAddChatBack, actionAddMSG, actionAddMSGBack,
  12. actionEditMSGback, actionFullGetChats, actionFullGetMessages,
  13. actionGetUserInfo, actionUploadFile
  14. } from "../actions";
  15. import { Grid, Container, AppBar } from "@mui/material";
  16. const Main = ({ match: { params: { _id } }, userID, chats, isLoad, getUserInfo, getChat, getMessages, addChat, addMSG, editMSG, sendMSG, addFile }) => {
  17. let [isEdit, setIsEdit] = useState(false)
  18. let onInfoHandler = () => {
  19. setIsEdit(!isEdit)
  20. }
  21. let chat_id = _id.split(".")[1]
  22. useEffect(() => {
  23. getUserInfo(userID)
  24. getChat(userID)
  25. const socket = io("ws://chat.fs.a-level.com.ua")
  26. socket.emit("jwt", localStorage.authToken)
  27. socket.on("jwt_ok", data => console.log("С JWT все норм", data))
  28. socket.on("jwt_fail", error => console.log("С JWT траблы", error))
  29. socket.on("connect", () => {
  30. // getChat(userID)
  31. console.log("Законектились, сокет: ", socket.id)
  32. });
  33. socket.on("connect_error", error => console.log("ошибка конекта", error));
  34. socket.on("reconnect_attempt", () => console.log("пробуем реконектнуть"));
  35. socket.on("disconnect", () => console.log("дисконект", socket.id));
  36. socket.on('chat', chat => {
  37. addChat(chat._id, chat.title, chat.createdAt, chat.lastModified, chat.owner, chat.avatar, chat.messages, chat.members)
  38. })
  39. socket.on('msg', msg => {
  40. addMSG(true, msg.chat._id, msg._id, msg.text, msg.createdAt, msg.owner, msg.media, msg.replyTo)
  41. })
  42. }, [])
  43. useEffect(() => {
  44. chat_id && getMessages(chat_id, 0)
  45. }, [_id, chat_id, getMessages])
  46. return (
  47. <Grid container spacing={0} sx={{
  48. width: '100%',
  49. bgcolor: 'background.paper',
  50. position: 'relative',
  51. maxHeight: '95vh',
  52. }}>
  53. <Grid container lg="12" sx={{
  54. display: 'flex',
  55. alignItems: 'center',
  56. bgcolor: 'primary.main',
  57. }}>
  58. <Grid item lg="3">
  59. <ConnectProfile />
  60. </Grid>
  61. {chat_id && <Grid item lg="9" >
  62. <ChatBar
  63. chat_title={chats[chat_id]?.title || ""}
  64. onInfo={onInfoHandler} />
  65. </Grid>}
  66. </Grid>
  67. <Grid item lg="3">
  68. <ChatsList />
  69. </Grid>
  70. <Grid item lg={isEdit ? "6" : "9"} sx={{ bgcolor: '#c7deed', }}>
  71. {chats[chat_id] ?
  72. <Chat chat_id={chat_id}
  73. user_id={userID}
  74. messages={chats[chat_id].messages || []}
  75. isLoad={isLoad}
  76. onUpload={addFile}
  77. onSend={sendMSG}
  78. onMSGEdit={editMSG}
  79. onScrollTopComplete={getMessages}
  80. /> : <ConnectChatForm />}
  81. </Grid>
  82. {isEdit && chats[chat_id] && <Grid item lg="3">
  83. <ConnectChatEditForm chat_id={chat_id} chat={chats[chat_id]} />
  84. </Grid>}
  85. </Grid>
  86. )
  87. }
  88. export const ConnectMain = connect(state => ({
  89. userID: state.auth.payload.sub.id,
  90. chats: state.chat,
  91. isLoad: state.promise?.messages?.status
  92. }),
  93. {
  94. getUserInfo: actionGetUserInfo,
  95. getChat: actionFullGetChats,
  96. getMessages: actionFullGetMessages,
  97. addChat: actionAddChat,
  98. addMSG: actionAddMSG,
  99. editMSG: actionEditMSGback,
  100. sendMSG: actionAddMSGBack,
  101. newChat: actionAddChatBack,
  102. addFile: actionUploadFile
  103. })(Main)