ChatMsgsPage.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import React, { useState, useEffect } from "react";
  2. import { connect } from "react-redux";
  3. import { Link, useParams } from "react-router-dom";
  4. import { actionFullMsgsByChat } from "../actions";
  5. import { store } from "../reducers";
  6. import Button from "react-bootstrap/esm/Button";
  7. import { backURL } from "../constants";
  8. import { AvatarStubHeader, color } from "../components/AvatarStub";
  9. import { actionSendMsg } from "../actions";
  10. import { useDropzone } from "react-dropzone";
  11. const ChatMsgs = ({
  12. chatMsgs = [],
  13. getChat,
  14. msgsCount = 20,
  15. sendMsg,
  16. msg,
  17. nickOwner,
  18. }) => {
  19. const [files, setFiles] = useState(
  20. msg?.media.map((mediaFile) => ({
  21. ...mediaFile,
  22. url: backURL + mediaFile.url,
  23. })) || []
  24. );
  25. const { getRootProps, getInputProps } = useDropzone({
  26. noKeyboard: true,
  27. onDrop: async (acceptedFiles) => {
  28. await setFiles([
  29. ...files,
  30. ...acceptedFiles.map((file) =>
  31. Object.assign(file, {
  32. url: URL.createObjectURL(file),
  33. })
  34. ),
  35. ])
  36. },
  37. })
  38. {console.log(files, "FILES")}
  39. const [msgsBlock, setMsgsBlock] = useState(0);
  40. let { _id } = useParams();
  41. useEffect(() => {
  42. chatMsgs(_id, msgsBlock, msgsCount);
  43. }, [_id]);
  44. const [text, setText] = useState("");
  45. const [msgs] = useState(getChat);
  46. const oneChatMsgs = getChat[_id]?.messages;
  47. return (
  48. <>
  49. <div className="chatBlock">
  50. <Link to={"/chatediting/" + getChat[_id]?._id}>
  51. <div className="displayFixed">
  52. {getChat[_id]?.avatar?.url ? (
  53. <img
  54. className="forChatHeader"
  55. src={backURL + getChat[_id]?.avatar?.url}
  56. />
  57. ) : (
  58. <AvatarStubHeader
  59. login={
  60. getChat[_id]?.title !== null
  61. ? getChat[_id]?.title
  62. : "chat without title"
  63. }
  64. color={color()}
  65. />
  66. )}
  67. <h5>{getChat[_id]?.title}</h5>
  68. <h6>Members: {getChat[_id]?.members.length}</h6>
  69. </div>
  70. </Link>
  71. <div className="chatContainer">
  72. <ul className="msgsContainer">
  73. {oneChatMsgs === undefined ? (
  74. <h2 className="noMsg">No messages</h2>
  75. ) : (
  76. oneChatMsgs.map((msg) => (
  77. <div
  78. className={
  79. msg?.owner?.nick === nickOwner
  80. ? "msgLi"
  81. : "msgOther"
  82. }
  83. key={Math.random()}
  84. >
  85. <div className="msgBlock">
  86. {msg?.owner?.avatar?.url === undefined ?
  87. <div className="avatarStubChat1" key={Math.random()}></div> :
  88. <img
  89. src={
  90. backURL + msg?.owner.avatar?.url
  91. }
  92. className="smallForChat1"
  93. key={Math.random()}
  94. /> }
  95. <h5 style={{ paddingTop: "5px" }} key={Math.random()}>
  96. {msg?.owner?.nick === null ? msg?.owner?.login : msg?.owner?.nick}
  97. </h5>
  98. </div>
  99. {msg?.media?.map((oneMsg) => (
  100. <img
  101. className="msgMedia"
  102. src={
  103. backURL + oneMsg.url
  104. }
  105. key={Math.random()}
  106. />
  107. ))}
  108. <li key={Math.random}>{msg?.text}</li>
  109. </div>
  110. ))
  111. )}
  112. </ul>
  113. </div>
  114. <div>
  115. {oneChatMsgs === getChat[_id] ? (
  116. <></>
  117. ) : (
  118. <div className="sendMsgBlock">
  119. <div className="sendBlock">
  120. <div
  121. {...getRootProps({
  122. className: "dropZoneStyle2",
  123. })}
  124. >
  125. <input {...getInputProps()} />
  126. <img
  127. src="https://img.icons8.com/ios-filled/344/folder-invoices--v2.png"
  128. className="sandFile"
  129. />
  130. </div>
  131. <textarea
  132. name="inputName"
  133. placeholder="Write a message..."
  134. rows="2"
  135. value={text}
  136. onChange={(e) => {
  137. setText(e.target.value);
  138. }}
  139. />
  140. </div>
  141. <Button
  142. onClick={() =>
  143. {
  144. sendMsg(
  145. getChat[_id]?._id,
  146. text,
  147. "media",
  148. files
  149. )
  150. setText("")
  151. setFiles([])
  152. }
  153. }
  154. >
  155. Send a message
  156. </Button>
  157. </div>
  158. )}
  159. </div>
  160. </div>
  161. </>
  162. );
  163. };
  164. export const CChatMsgs = connect(
  165. (state) => ({
  166. getChat: state.chats,
  167. nickOwner: store.getState().promise.myProfile.payload?.nick,
  168. }),
  169. {
  170. chatMsgs: actionFullMsgsByChat,
  171. sendMsg: actionSendMsg,
  172. }
  173. )(ChatMsgs);