MessageMediaModal.jsx 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { Button, TextField } from "@mui/material"
  2. import { useEffect, useRef, useState } from "react";
  3. import { useDropzone } from "react-dropzone";
  4. import { connect } from "react-redux";
  5. import {DropMediaItem, MemoizedDropMediaItem} from "../DropMediaItem/DropMediaItem";
  6. import { addUploadDate } from "../../helpers/addUploadDate";
  7. import SimpleBar from 'simplebar-react';
  8. import 'simplebar/dist/simplebar.min.css';
  9. import { DropMediaList, MediaBox, MediaModalHeader, MessageMediaFooter, MessageMediaModalWrapper } from "./MessageMediaModal.style"
  10. import { actionSendMessage } from "../../actions/actionsMessages";
  11. import { actionAddDraftMessage, actionDeleteDropMedia, actionSetDropMedia, actionSetInputMessageValue } from "../../actions/actionsForChats";
  12. const MessageMediaModal = ({chatId, handleClose, deleteMedia, deleteDraftMessage, chats, open, onload, sendMessage, setInputValue}) => {
  13. const media = chats[chatId].draft?.media || [];
  14. const inputValue = chats[chatId].draft?.mainInputValue?.value || '';
  15. const message = chats[chatId]?.draft?.mainInputValue?.message;
  16. const replyMessageId = message && message.hasOwnProperty('reply') ? message.reply?._id : null;
  17. // const forwardedMessageId = message && message.hasOwnProperty('forwarded') ? message.forwarded?._id : null;
  18. //remove media array
  19. useEffect(() => {
  20. open || deleteMedia(chatId);
  21. }, [open])
  22. const {acceptedFiles, getRootProps, getInputProps} = useDropzone({
  23. noDrag: true,
  24. multiple: true
  25. });
  26. useEffect(()=>{
  27. let files = addUploadDate(acceptedFiles)
  28. console.log(files)
  29. acceptedFiles[0] && onload(chatId, files)
  30. }, [acceptedFiles]);
  31. useEffect(() => {
  32. media.length == 0 && handleClose();
  33. }, [media])
  34. return(
  35. <MessageMediaModalWrapper>
  36. <MediaModalHeader>{media.length} {media.length === 1 ? "file" : "files"} selected</MediaModalHeader>
  37. <MediaBox>
  38. <SimpleBar style={{ maxHeight: '400px'}}>
  39. <DropMediaList>
  40. {media.map((item) => <MemoizedDropMediaItem key={item.uploadDate} chatId={chatId} mediaItem={item}/>)}
  41. </DropMediaList>
  42. </SimpleBar>
  43. </MediaBox>
  44. <TextField
  45. maxRows={7}
  46. multiline
  47. sx={{width: "100%", mt: "30px"}}
  48. variant="standard"
  49. label="Caption"
  50. onChange = {(e) => setInputValue(chatId, e.target.value, 'mainInputValue')}
  51. value={inputValue}
  52. />
  53. <MessageMediaFooter>
  54. <div
  55. {...getRootProps({className: 'dropzone'})}>
  56. <input {...getInputProps()} />
  57. <Button variant="text">Add</Button>
  58. </div>
  59. <div>
  60. <Button variant="text" onClick={handleClose}>Cancel</Button>
  61. <Button
  62. variant="text"
  63. onClick={
  64. async () => {
  65. let val = await sendMessage(null, chatId, inputValue.replace(/^\s+|\s+$/g, ''), media, replyMessageId, null);
  66. val?.replyTo?._id && deleteDraftMessage(chatId, null)
  67. val && setInputValue(chatId, "", 'mainInputValue'); handleClose();
  68. }
  69. }
  70. >Send</Button>
  71. </div>
  72. </MessageMediaFooter>
  73. </MessageMediaModalWrapper>
  74. )
  75. }
  76. export default connect(state => ({
  77. chats: state.chats
  78. }),
  79. {
  80. deleteMedia: actionDeleteDropMedia,
  81. onload: actionSetDropMedia,
  82. sendMessage: actionSendMessage,
  83. setInputValue: actionSetInputMessageValue,
  84. deleteDraftMessage: actionAddDraftMessage
  85. })(MessageMediaModal);