Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 5187 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6753 stevensc 1
import React, { useState } from 'react'
2
import { axios } from '../../utils'
5185 stevensc 3
import { useForm } from 'react-hook-form'
4
import SendIcon from '@mui/icons-material/Send'
5
import AttachFileIcon from '@mui/icons-material/AttachFile'
1748 stevensc 6
 
6753 stevensc 7
import SendFileModal from '../../chat/chat/personal-chat/send-file-modal/SendFileModal'
8
import { addNotification } from '../../redux/notification/notification.actions'
9
import { useDispatch } from 'react-redux'
1748 stevensc 10
 
5187 stevensc 11
const MessageBox = ({ onSend, sendUrl }) => {
6753 stevensc 12
  const [isShowFileModal, setIsShowFileModal] = useState(false)
13
  const [isSending, setIsSending] = useState(false)
14
 
5185 stevensc 15
  const { handleSubmit, register, reset } = useForm()
2655 stevensc 16
 
6753 stevensc 17
  const dispatch = useDispatch()
1748 stevensc 18
 
5187 stevensc 19
  const handleSend = (message) => {
20
    onSend(sendUrl, message)
5185 stevensc 21
    reset()
22
  }
2655 stevensc 23
 
6753 stevensc 24
  const toggleFileModal = () => {
25
    setIsShowFileModal(!isShowFileModal)
26
  }
27
 
28
  const sendFile = (file) => {
29
    setIsSending(true)
30
    const formData = new FormData()
31
    formData.append('file', file)
32
 
33
    axios
34
      .post(sendUrl, formData)
35
      .then(({ data: response }) => {
36
        const { success, data } = response
37
        if (!success) {
38
          const errorMessage =
39
            typeof data === 'string' ? data : 'Ha ocurrido un error'
40
          dispatch(addNotification({ style: 'success', msg: errorMessage }))
41
          return
42
        }
43
 
44
        toggleFileModal()
45
      })
46
      .finally(() => setIsSending(false))
47
  }
48
 
5185 stevensc 49
  return (
6753 stevensc 50
    <>
51
      <div className="chat__input-container">
52
        <form onSubmit={handleSubmit(handleSend)}>
53
          <button
54
            type="button"
55
            className="send_btn icon"
56
            onClick={toggleFileModal}
57
          >
58
            <AttachFileIcon />
59
          </button>
60
          <input
61
            type="text"
62
            name="message"
63
            className="chatInput"
64
            ref={register({ required: true })}
65
            placeholder={LABELS.WRITE_YOUR_MESSAGE_HERE}
66
          />
67
          <button type="submit" className="send_btn">
68
            <SendIcon />
69
          </button>
70
        </form>
71
      </div>
72
      <SendFileModal
73
        isShow={isShowFileModal}
74
        onHide={toggleFileModal}
75
        onComplete={sendFile}
76
        loading={isSending}
77
      />
78
    </>
5185 stevensc 79
  )
1748 stevensc 80
}
5185 stevensc 81
export default MessageBox