SendMsg.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import React, { useState, useEffect } from 'react'
  2. import Button from 'react-bootstrap/esm/Button'
  3. import { connect } from 'react-redux'
  4. // import { MsgDropZone } from './MsgDropZone'
  5. import { actionSendMsg } from '../actions'
  6. import { backURL } from '../constants'
  7. const SendingField = ({ chatId, onSend, msg }) => {
  8. const [text, setText] = useState(msg?.text || '')
  9. const [files, setFiles] = useState(
  10. msg?.media.map((mediaFile) => ({
  11. ...mediaFile,
  12. url: backURL + mediaFile.url,
  13. })) || []
  14. )
  15. const [msgId, setMsgId] = useState(msg?._id || null)
  16. useEffect(() => {
  17. setText(msg?.text || '')
  18. setFiles(
  19. msg?.media.map((mediaFile) => ({
  20. ...mediaFile,
  21. url: backURL + mediaFile.url,
  22. })) || []
  23. )
  24. setMsgId(msg?._id || null)
  25. }, [msg])
  26. return (
  27. <div className="sendingField">
  28. <div className="buttonSendBox">
  29. <div className="sendBlock">
  30. <img src="https://img.icons8.com/ios-filled/344/folder-invoices--v2.png" className="sandFile"/>
  31. <textarea placeholder="Write a message..." rows="2" value={text} onChange={(e) => setText(e.target.value)}/>
  32. </div>
  33. <Button
  34. endIcon={"@"}
  35. onClick={() => {
  36. ;(text.match(/^\s*$/) && files.length === 0) ||
  37. onSend(chatId, text.trim(), 'media', files, msgId)
  38. setText('')
  39. setFiles([])
  40. setMsgId()
  41. }}
  42. >Send a message</Button>
  43. <Button
  44. className="buttonSend"
  45. variant="contained"
  46. endIcon={"@"}
  47. onClick={() => {
  48. ;(text.match(/^\s*$/) && files.length === 0) ||
  49. onSend(chatId, text.trim(), 'media', files, msgId)
  50. setText('')
  51. setFiles([])
  52. setMsgId()
  53. }}
  54. >
  55. Отправить
  56. </Button>
  57. </div>
  58. </div>
  59. )
  60. }
  61. export const CSendingField = connect(null, { onSend: actionSendMsg })(
  62. SendingField
  63. )