ChatsAside.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 } 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. key={Math.random()}
  42. />
  43. )}
  44. <div className="chatLi" key={Math.random()}>
  45. <h5
  46. className="chatTitle"
  47. key={Math.random()}
  48. >
  49. {chat.title !== null
  50. ? chat.title
  51. : "chat without title"}
  52. </h5>
  53. <p key={Math.random()}>
  54. {chat.lastMessage?.text.length >
  55. parseInt("30")
  56. ? chat.lastMessage?.text.substr(
  57. 0,
  58. 30
  59. ) + "..."
  60. : chat.lastMessage?.text.substr(
  61. 0,
  62. 30
  63. )}
  64. </p>
  65. </div>
  66. </div>
  67. </Link>
  68. </>
  69. ))}
  70. <Link className="newChatButton" to="/newchat" key={Math.random}>
  71. +
  72. </Link>
  73. </div>
  74. </div>
  75. </>
  76. );
  77. };
  78. export const CChatsAside = connect(
  79. (state) => ({
  80. auth: state.auth,
  81. chats: Object.values(state.chats).filter((el) => el._id),
  82. }),
  83. { ownerChats: actionFullChatList }
  84. )(ChatsAside);