OpenChatPage.jsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import React, { useState, useEffect } from 'react';
  2. import { Link, useParams } from 'react-router-dom';
  3. import io from 'socket.io-client';
  4. import { history } from '../history/';
  5. import '../App.css';
  6. import store from '../store';
  7. import { actionAboutMe } from '../store/actions/action-aboutMe'
  8. import backgroundChat from '../img/back.JPG'
  9. import { useRef } from 'react';
  10. export const OpenChatPage = ({ onCreateMsg, match: { params: { _id } }, chats, getData, userName }) => {
  11. const [chatList, setChatList] = useState(chats || [])
  12. const [messageList, setMessageList] = useState([])
  13. const [chatInfo, setChatInfo] = useState([])
  14. const [msgAdd, setMsgAdd] = useState("")
  15. useEffect(() => {
  16. if (chats) {
  17. const activeChatId = window.location.pathname.split('/')[2]
  18. let activeChat = chats.filter((item) => item._id === activeChatId);
  19. console.log('sadasdasd', activeChat)
  20. setMessageList(activeChat[0].messages)
  21. }
  22. }, [chats])
  23. useEffect(() => {
  24. if (chats) {
  25. const activeChatId = window.location.pathname.split('/')[2]
  26. let activeChat = chats.filter((item) => item._id === activeChatId);
  27. console.log('mem', activeChat)
  28. setChatInfo(activeChat[0].members)
  29. }
  30. }, [chats])
  31. const CreationMsg = () => {
  32. onCreateMsg(msgAdd)
  33. setMsgAdd('')
  34. }
  35. useEffect(() => {
  36. async function getDataFunc() {
  37. const res = await getData(_id)
  38. setChatList(res.chats)
  39. setMessageList(res.chats?.message)
  40. setChatInfo(res.chats?.members)
  41. }
  42. getDataFunc()
  43. }, [_id])
  44. function handleClick(_id) {
  45. const filteredChatList = chatList.filter((item) => item._id === _id)
  46. console.log(filteredChatList)
  47. setMessageList(filteredChatList[0].messages)
  48. }
  49. function chatInfoClick(_id) {
  50. const filteredChatList = chatList.filter((item) => item._id === _id)
  51. console.log(filteredChatList)
  52. setChatInfo(filteredChatList[0].members)
  53. }
  54. return (
  55. <>
  56. <div className='chatPage'>
  57. <aside>
  58. <div className="row">
  59. <div className="col-12">
  60. <div id="list-example" className="list-group">
  61. <a href="/create-chat" className='linkToCreate'><h1>Создать чат</h1></a>
  62. <ul className='chatAside'>{chatList.map((item, index) =>
  63. <a className="list-group-item list-group-item-action">
  64. <Link to={`/chat/${item._id}/info`}> <h3 onClick={() => chatInfoClick(item._id)}>Инфо </h3> </Link>
  65. <Link to={`/chat/${item._id}`}>
  66. <li onClick={() => handleClick(item._id)} className='chatLi' key={index}>
  67. <h3>Chat: {item.title}</h3>
  68. {/* {item.lastMessage ? <h6>Last Message: {item.lastMessage.text}</h6> : []} */}
  69. </li>
  70. </Link>
  71. </a>
  72. )}
  73. </ul>
  74. </div>
  75. </div>
  76. </div>
  77. </aside>
  78. <main className='chatMain'>
  79. <div className="mb-3 sendMsg">
  80. <input
  81. type="text"
  82. value={msgAdd}
  83. onChange={(e) => setMsgAdd(e.target.value)}
  84. className="form-control"
  85. id="exampleInputEmail1"
  86. aria-describedby="emailHelp" />
  87. <button type="submit" className="btn btn-primary" onClick={CreationMsg}>Отправить</button>
  88. </div>
  89. {messageList && messageList.length > 0 &&
  90. <ul className='msgUl'>{messageList.map((item1, index) =>
  91. <li className='msgLi' key={index}>
  92. <h4><p className='msgOwner'>{item1.owner.login}</p> <p>{item1.text}</p></h4>
  93. </li>
  94. )}
  95. </ul>
  96. }
  97. </main>
  98. </div>
  99. </>
  100. )
  101. }