ChatMsgsPage.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 } 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. const [msgsBlock, setMsgsBlock] = useState(0);
  39. console.log(files, "FILES");
  40. let { _id } = useParams();
  41. useEffect(() => setFiles([]) , [getChat[_id]])
  42. useEffect(() => {
  43. chatMsgs(_id, msgsBlock, msgsCount);
  44. }, [_id]);
  45. const onDelFiles = (i) => {
  46. setFiles(
  47. files.filter((el, index) => files[index].lastModified !== files[i].lastModified)
  48. );
  49. };
  50. const [text, setText] = useState("");
  51. const [msgs] = useState(getChat);
  52. const oneChatMsgs = getChat[_id]?.messages;
  53. return (
  54. <>
  55. <div className="chatBlock">
  56. <Link to={"/chatediting/" + getChat[_id]?._id}>
  57. <div className="displayFixed">
  58. {getChat[_id]?.avatar?.url ? (
  59. <img
  60. className="forChatHeader"
  61. src={backURL + getChat[_id]?.avatar?.url}
  62. />
  63. ) : (
  64. <AvatarStubHeader
  65. login={
  66. getChat[_id]?.title !== null
  67. ? getChat[_id]?.title
  68. : "chat without title"
  69. }
  70. />
  71. )}
  72. <h5>{getChat[_id]?.title}</h5>
  73. <h6>Members: {getChat[_id]?.members.length}</h6>
  74. </div>
  75. </Link>
  76. <div className="chatContainer">
  77. <ul className="msgsContainer">
  78. {oneChatMsgs === undefined ? (
  79. <h2 className="noMsg">No messages</h2>
  80. ) : (
  81. oneChatMsgs.map((msg) => (
  82. <div
  83. className={
  84. msg?.owner?.nick === nickOwner
  85. ? "msgLi"
  86. : "msgOther"
  87. }
  88. key={Math.random()}
  89. >
  90. <div className="msgBlock">
  91. {msg?.owner?.avatar?.url ===
  92. undefined ? (
  93. <div
  94. className="avatarStubChat1"
  95. key={Math.random()}
  96. ></div>
  97. ) : (
  98. <img
  99. src={
  100. backURL +
  101. msg?.owner.avatar?.url
  102. }
  103. className="smallForChat1"
  104. key={Math.random()}
  105. />
  106. )}
  107. <h5
  108. style={{ paddingTop: "5px" }}
  109. key={Math.random()}
  110. >
  111. {msg?.owner?.nick === null
  112. ? msg?.owner?.login
  113. : msg?.owner?.nick}
  114. </h5>
  115. </div>
  116. {msg?.media?.map((oneMsg) => (
  117. <img
  118. className="msgMedia"
  119. src={backURL + oneMsg.url}
  120. key={Math.random()}
  121. />
  122. ))}
  123. <li key={Math.random}>{msg?.text}</li>
  124. </div>
  125. ))
  126. )}
  127. </ul>
  128. </div>
  129. <div>
  130. {oneChatMsgs === getChat[_id] ? (
  131. <></>
  132. ) : (
  133. <div>
  134. <div className="preloaderContainer">
  135. {files.map((file, i) => (
  136. <div className="preloaderBlock">
  137. <img src={file?.url} className="preload" key={file.lastModified}/>
  138. <button onClick={() => onDelFiles(i)} key={i}>
  139. x
  140. </button>
  141. </div>
  142. ))}
  143. </div>
  144. <div className="sendMsgBlock">
  145. <div className="sendBlock">
  146. <div
  147. {...getRootProps({
  148. className: "dropZoneStyle2",
  149. })}
  150. >
  151. <input {...getInputProps()} />
  152. <img
  153. src="https://img.icons8.com/ios-filled/344/folder-invoices--v2.png"
  154. className="sandFile"
  155. />
  156. </div>
  157. <textarea
  158. name="inputName"
  159. placeholder="Write a message..."
  160. rows="2"
  161. value={text}
  162. onChange={(e) => {
  163. setText(e.target.value);
  164. }}
  165. />
  166. </div>
  167. <Button
  168. onClick={() => {
  169. sendMsg(
  170. getChat[_id]?._id,
  171. text,
  172. "media",
  173. files
  174. );
  175. setText("");
  176. setFiles([]);
  177. }}
  178. >
  179. Send a message
  180. </Button>
  181. </div>
  182. </div>
  183. )}
  184. </div>
  185. </div>
  186. </>
  187. );
  188. };
  189. export const CChatMsgs = connect(
  190. (state) => ({
  191. getChat: state.chats,
  192. nickOwner: store.getState().promise.myProfile.payload?.nick,
  193. }),
  194. {
  195. chatMsgs: actionFullMsgsByChat,
  196. sendMsg: actionSendMsg,
  197. }
  198. )(ChatMsgs);