ChatsAside.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import React, { useEffect, useState } from "react";
  2. import { connect } from "react-redux";
  3. import { actionFullChatList, actionChatsCount } from "../actions";
  4. import { backURL } from "../constants";
  5. import { Link } from "react-router-dom";
  6. import { AvatarStub, color } from "../components/AvatarStub";
  7. const ChatsAside = ({ chats = [], auth, ownerChats }) => {
  8. const [chatBlock, setChatBlock] = useState(0);
  9. useEffect(() => {
  10. const userId = auth.payload?.sub?.id;
  11. if (userId) {
  12. ownerChats(userId);
  13. }
  14. return function cleanUp() {};
  15. }, [chatBlock]);
  16. return (
  17. <>
  18. <div className="chatsContainer" key={Math.random}>
  19. <div>
  20. {chats.map((chat, _id) => (
  21. <>
  22. <Link
  23. className="chatsList"
  24. to={`/main/${chat._id}`}
  25. key={Math.random}
  26. >
  27. <div className="chatsCard" key={Math.random()}>
  28. {chat.avatar?.url ? (
  29. <img
  30. key={Math.random()}
  31. className="smallForChat"
  32. src={backURL + chat.avatar?.url}
  33. />
  34. ) : (
  35. <AvatarStub
  36. login={
  37. chat.title !== null
  38. ? chat.title
  39. : "chat without title"
  40. }
  41. color={color()}
  42. key={Math.random()}
  43. />
  44. )}
  45. <div className="chatLi" key={Math.random()}>
  46. <h5
  47. className="chatTitle"
  48. key={Math.random()}
  49. >
  50. {chat.title !== null
  51. ? chat.title
  52. : "chat without title"}
  53. </h5>
  54. <p key={Math.random()}>
  55. {chat.lastMessage?.text.length >
  56. parseInt("30")
  57. ? chat.lastMessage?.text.substr(
  58. 0,
  59. 30
  60. ) + "..."
  61. : chat.lastMessage?.text.substr(
  62. 0,
  63. 30
  64. )}
  65. </p>
  66. </div>
  67. </div>
  68. </Link>
  69. </>
  70. ))}
  71. <Link className="newChatButton" to="/newchat" key={Math.random}>
  72. +
  73. </Link>
  74. </div>
  75. </div>
  76. </>
  77. );
  78. };
  79. export const CChatsAside = connect(
  80. (state) => ({
  81. auth: state.auth,
  82. chats: Object.values(state.chats).filter((el) => el._id),
  83. }),
  84. { ownerChats: actionFullChatList }
  85. )(ChatsAside);