ChatPage.jsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import React, { useState, useEffect } from 'react';
  2. import { Link, useParams } from 'react-router-dom';
  3. import { history } from '../history/';
  4. import '../App.css';
  5. import store from '../store';
  6. import { actionAboutMe } from '../store/actions/action-aboutMe'
  7. import backgroundChat from '../img/back.JPG'
  8. import { actionChatOne } from '../store/reducers/chat-reducer';
  9. export const ChatPage = ({ onCreateMsg, match: { params: { _id } }, chats, getData, userName }) => {
  10. const [chatList, setChatList] = useState(chats || [])
  11. const [messageList, setMessageList] = useState([])
  12. const [msgAdd, setMsgAdd] = useState("")
  13. const CreationMsg = () => {
  14. onCreateMsg(msgAdd)
  15. setMsgAdd('')
  16. }
  17. useEffect(() => {
  18. async function getDataFunc() {
  19. const res = await getData(_id)
  20. setChatList(res.chats)
  21. setMessageList(res.chats?.message)
  22. }
  23. getDataFunc()
  24. }, [_id])
  25. function handleClick(_id) {
  26. const filteredChatList = chatList.filter((item) => item._id === _id)
  27. console.log(filteredChatList)
  28. setMessageList(filteredChatList[0].messages)
  29. }
  30. return (
  31. <>
  32. <div className='chatPage'>
  33. <aside>
  34. <div className="row">
  35. <div className="col-12">
  36. <div id="list-example" className="list-group">
  37. <a href="/create-chat" className='linkToCreate'><h1>Создать чат</h1></a>
  38. <ul className='chatAside'>{chatList.map((item, index) =>
  39. <a className="list-group-item list-group-item-action">
  40. <Link to={`/chat/${item._id}`}>
  41. <li onClick={() => handleClick(item._id)} className='chatLi' key={index}>
  42. <h3>Chat: {item.title}</h3>
  43. {/* {item.lastMessage ? <h6>Last Message: {item.lastMessage.text}</h6> : []} */}
  44. </li>
  45. </Link>
  46. </a>
  47. )}
  48. </ul>
  49. </div>
  50. </div>
  51. </div>
  52. </aside>
  53. <main className='chatMain'>
  54. <div className='chatPrewiew'>
  55. <h4>select a chat to start messaging</h4>
  56. </div>
  57. </main>
  58. </div>
  59. </>
  60. )
  61. }