ChatEditing.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import React, { useState } from "react";
  2. import { connect } from "react-redux";
  3. import { useDropzone } from "react-dropzone";
  4. import { Link, useParams } from "react-router-dom";
  5. import Button from "react-bootstrap/esm/Button";
  6. import { Input, InputDebounce } from "../helpers/UserSearch";
  7. import { CUserSearch } from "../helpers/UserSearch";
  8. import { removeUserChat } from "../actions";
  9. import { CSearchByLogin } from '../helpers/UserSearch'
  10. import { actionSetChatInfo } from "../actions";
  11. import { CChatAvatar } from "../components/Avatar";
  12. const ChatEditing = ({ chatId, onChat, myProfile, create, onLeave }) => {
  13. let { _id } = useParams();
  14. const [chat] = useState();
  15. const [img, setImg] = useState(null);
  16. const [newTitle, setNewTitle] = useState(chatId[_id]?.title || "");
  17. const [members, setMembers] = useState(chat?.members || [myProfile]);
  18. const { getRootProps, getInputProps, isDragActive } = useDropzone({
  19. accept: "image/*",
  20. maxFiles: 1,
  21. onDrop: (acceptedFiles) => {
  22. setImg(acceptedFiles[0]);
  23. },
  24. });
  25. function prepareMembers(members) {
  26. const newMembers = [];
  27. for (const member of members) {
  28. if (create) {
  29. if (member._id !== myProfile?._id) {
  30. newMembers.push({ _id: member._id });
  31. }
  32. } else {
  33. newMembers.push({ _id: member._id });
  34. }
  35. }
  36. return newMembers;
  37. }
  38. return (
  39. <div className="newChatContainer">
  40. <div {...getRootProps({ className: "dropZoneStyle" })}>
  41. <input {...getInputProps()} />
  42. {isDragActive ? (
  43. <p className="dropZoneStyleBr">Drop the files here ...</p>
  44. ) : (
  45. <CChatAvatar
  46. _id={_id}
  47. text="upload chat avatar"
  48. className="profileStyle"
  49. />
  50. )}
  51. </div>
  52. <div className="profileContainer">
  53. <div className="loginNickSetting">
  54. <div className="inputContainer">
  55. <label for="loginInput">
  56. Enter the new name of the chat:{" "}
  57. </label>
  58. <input
  59. className="form-control-editing form-control"
  60. id="loginInput"
  61. type="text"
  62. value={newTitle}
  63. onChange={(e) => {
  64. setNewTitle(e.target.value);
  65. }}
  66. />
  67. </div>
  68. <div className="inputContainer">
  69. <CSearchByLogin/>
  70. </div>
  71. <div className="df">
  72. <div className="df"></div>
  73. <Button
  74. variant="primary"
  75. className="buttonSetting"
  76. onClick={() => {
  77. onChat(
  78. "media",
  79. img,
  80. newTitle,
  81. prepareMembers(members),
  82. _id
  83. );
  84. }}
  85. >
  86. <Link
  87. to="/changesdonechats"
  88. style={{
  89. color: "white",
  90. textDecoration: "none",
  91. }}
  92. >
  93. Apply Changes
  94. </Link>
  95. </Button>
  96. <Button variant="danger" onClick={() => onLeave(_id)}>
  97. <Link
  98. to="/changesdonechats"
  99. style={{
  100. color: "white",
  101. textDecoration: "none",
  102. }}
  103. >
  104. Remove chat
  105. </Link>
  106. </Button>
  107. <Link to={"/main/" + _id} className="changepasLink">
  108. Back to chat
  109. </Link>
  110. </div>
  111. </div>
  112. </div>
  113. </div>
  114. );
  115. };
  116. export const CChatEditing = connect(
  117. (state) => ({
  118. myProfile: state.promise.myProfile?.payload || {},
  119. chatId: state.chats,
  120. }),
  121. { onChat: actionSetChatInfo, onLeave: removeUserChat }
  122. )(ChatEditing);