Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 5916 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
5344 stevensc 1
/* eslint-disable react/prop-types */
5916 stevensc 2
import React, { useRef, useState } from 'react'
3
import { axios } from '../../utils'
4
import { useDispatch } from 'react-redux'
5
import { addNotification } from '../../redux/notification/notification.actions'
6
 
5344 stevensc 7
import AttachFileIcon from '@mui/icons-material/AttachFile'
8
import SentimentVerySatisfiedRoundedIcon from '@mui/icons-material/SentimentVerySatisfiedRounded'
9
import Emojione from '../../chat/chat/personal-chat/emojione/Emojione'
6007 stevensc 10
import SendFileModal from '../../chat/chat/personal-chat/send-file-modal/SendFileModal'
5344 stevensc 11
 
12
const TextBox = ({
13
  uploadUrl = '',
14
  isNotSeen = false,
15
  markRead = () => false,
5916 stevensc 16
  onSend,
5344 stevensc 17
}) => {
5916 stevensc 18
  const [isShowFileModal, setIsShowFileModal] = useState(false)
5344 stevensc 19
  const [showEmojiTab, setShowEmojiTab] = useState(false)
5916 stevensc 20
  const [loading, setLoading] = useState(false)
5344 stevensc 21
  const textAreaEl = useRef(null)
22
 
5916 stevensc 23
  const dispatch = useDispatch()
5344 stevensc 24
 
5916 stevensc 25
  const handleShowEmojiTab = () => {
26
    setShowEmojiTab(!showEmojiTab)
27
  }
5344 stevensc 28
 
5916 stevensc 29
  const toggleFileModal = () => {
30
    setIsShowFileModal(!isShowFileModal)
31
  }
32
 
5344 stevensc 33
  const handleClickEmoji = (e) => {
34
    const shortname = e.currentTarget.dataset.shortname
35
    const currentText = textAreaEl.current.value
36
    const cursorPosition = textAreaEl.current.selectionStart
37
    const textBehind = currentText.substring(0, cursorPosition)
38
    const textForward = currentText.substring(cursorPosition)
39
    const unicode = emojione.shortnameToUnicode(shortname)
40
    textAreaEl.current.value = `${textBehind}${unicode}${textForward}`
41
    textAreaEl.current.focus()
42
    textAreaEl.current.setSelectionRange(
43
      cursorPosition + unicode.length,
44
      cursorPosition + unicode.length
45
    )
46
  }
47
 
48
  const onKeyDown = async (e) => {
49
    if (e.key !== 'Enter') return false
50
    e.preventDefault()
51
    const message = e.target.value
52
    await onSend(message)
53
    e.target.value = ''
54
    setShowEmojiTab(false)
55
  }
56
 
5916 stevensc 57
  const sendFile = (file) => {
58
    setLoading(true)
59
    const formData = new FormData()
60
    formData.append('file', file)
61
 
62
    axios
63
      .post(uploadUrl, formData)
64
      .then(({ data: response }) => {
65
        const { success, data } = response
66
        if (!success) {
67
          const errorMessage =
68
            typeof data === 'string' ? data : 'Ha ocurrido un error'
69
          dispatch(addNotification({ style: 'success', msg: errorMessage }))
70
          return
71
        }
72
 
73
        toggleFileModal()
74
      })
75
      .finally(() => setLoading(false))
76
  }
77
 
5344 stevensc 78
  return (
5916 stevensc 79
    <>
80
      <div className="chat-text_box">
81
        <button className="btn" onClick={toggleFileModal}>
82
          <AttachFileIcon />
83
        </button>
84
        <button className="btn" onClick={handleShowEmojiTab}>
85
          <SentimentVerySatisfiedRoundedIcon />
86
        </button>
87
        <textarea
88
          placeholder={CHAT_LABELS.WRITE_A_MESSAGE}
89
          onKeyDown={onKeyDown}
90
          ref={textAreaEl}
91
          onFocus={() => isNotSeen && markRead()}
92
        />
93
        <div
94
          className="emojione-container"
95
          style={{ display: showEmojiTab ? 'block' : 'none' }}
96
        >
97
          <Emojione onClickEmoji={handleClickEmoji} />
98
        </div>
99
      </div>
100
      <SendFileModal
101
        isShow={isShowFileModal}
102
        onHide={toggleFileModal}
103
        onComplete={sendFile}
104
        loading={loading}
105
      />
106
    </>
5344 stevensc 107
  )
108
}
109
 
110
export default TextBox